Home arrow PHP arrow Page 5 - Generating Images on the Fly With PHP
PHP

Generating Images on the Fly With PHP


For developers like Divyesh, one of the most popular features of PHP is real-time image generation. That is, we can create a new image canvas, "paint" it, and either save it to a file, or send it directly to the browser. In this article, Divyesh will show you how to create an image in real-time with PHP.

Author Info:
By: Divyesh Jariwala
Rating: 4 stars4 stars4 stars4 stars4 stars / 69
December 13, 2001
TABLE OF CONTENTS:
  1. · Generating Images on the Fly With PHP
  2. · The GD image library
  3. · Creating our image
  4. · Displaying our image
  5. · Generating the random number
  6. · Changing the font of our random number
  7. · Conclusion

print this article
SEARCH DEVARTICLES

Generating Images on the Fly With PHP - Generating the random number
(Page 5 of 7 )

Generating our random number is very easy. We will simply create a function named get_random which will return a random number between 1 and the built-in PHP function, getrandmax(). Getrandmax() is an implementation specific function, and returns the largest seed value available on the current system. Our get_random function looks like this:

function get_random()

{

srand(time());

$max = getrandmax();

return rand(1,$max) + rand(1,$max) ;

}


To actually place any piece of textual data onto our image, we use the imagestring function. The imagestring function takes six parameters, as shown below:

int imagestring (int im, int font, int x, int y, string s, int col)
  1. im: A reference to an image canvas created using the imagecreate function
  2. font: An integer value specifying the number of the font to draw the text in. If font is 1, 2, 3, 4 or 5, then a built-in font is used
  3. x: The horizontal displacement from the left of the image where the text will be drawn from, measured in pixels.
  4. y: The vertical displacement from the top of the image where the text will be drawn from, measured in pixels.
  5. s: The text to be drawn on the image
  6. col: The color that the text will be painted. A reference to a color created using the imagecolorallocate function.
The following code will use our get_random function to generate a random number. It will also use the imagestring function to draw that number onto our image canvas:

$number = get_random();

Imagestring($img_number,9,30,15,$number,$black);


For your convenience, the complete PHP script to generate our image containing a random number is shown below:

<?php

//random_number.php

$img_number = imagecreate(100,50);

$white = imagecolorallocate($img_number,255,255,255);

$black = imagecolorallocate($img_number,0,0,0);

$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);

ImageRectangle($img_number,5,5,94,44,$black);

ImageRectangle($img_number,0,0,99,49,$black);



$number = get_random();

Imagestring($img_number,9,30,15,$number,$black);

header("Content-type: image/jpeg");

imagejpeg($img_number);

function get_random()

{

srand(time());

$max = getrandmax();

return rand(1,$max) + rand(1,$max) ;

}

?>


Copy the code shown above into random_numer.php, save the file, and load it into your web browser. You should be presented with an image similar to the one below (remember that we are using a random number, so yours will be different to mine):

Our complete image, which now contains a random number

Actually displaying the image as part of a HTML page is simple. Instead of specifying the src attribute of an image as a filename, you specify the name of our PHP script, like this:

<img src=”random_number.php”>

When the browser loads this image, it will call the random_number.php script, which would return our image as a stream of binary data. The browser would then write this binary data to the screen as the image.

One last thing I will discuss in this article is changing the font of the random number as it appears in our image. This step is optional, but demonstrates the flexibility of PHP and the GD library.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials