Home >>Advance PHP Tutorial >PHP Dynamic Image Generation

PHP Dynamic Image Generation



PHP: Dynamic Image Generation

At first, have a look at the following script that creates CAPTCHA image.Next, we will explain each step in detail.
<?php

function createDynamic_image()

{

//Let's generate a totally random string using md5
$md5 = md5(rand(0,999)); 

//We don't need a 32 character long string so we trim it down to 5
$pass = substr($md5, 10, 5); 

//Set the image width and height
$width = 100;
$height = 20; 

//Create the image resource
$image = ImageCreate($width, $height);  

//We are making three colors, white, black and gray

$white = ImageColorAllocate($image, 255, 255, 255);

$black = ImageColorAllocate($image, 0, 0, 0);

$grey = ImageColorAllocate($image, 204, 204, 204);

//Make the background black
ImageFill($image, 0, 0, $black); 

//Add randomly generated string in white to the image
ImageString($image, 3, 30, 3, $pass, $white); 

//Throw in some lines to make it a little bit harder for any bots to break
ImageRectangle($image,0,0,$width-1,$height-1,$grey); 

imageline($image, 0, $height/2, $width, $height/2, $grey); 

imageline($image, $width/2, 0, $width/2, $height, $grey); 
 
//Tell the browser what kind of file is come in 
header("Content-Type: image/jpeg"); 

//Output the newly created image in jpeg format
ImageJpeg($image);
   
//Free up resources
ImageDestroy($image);

}
 
createDynamic_image();

?>
In the given above example we created an empty funcion createDynamic_image(). using rand(0,999) function generates a random number from 0 to 999. pass rand(0,999) inside md5(random(0,999)) it generate a totally random string. We don't need a 32 character long string so we trim it down to 5 string using substr($md5, 10, 5) function. if you want more than 5 character then increase the value of third parameter of substr(6). Create the image resource using ImageCreate($width, $height) in which pass height and width of generated image. Make three colors White, Black and Gray using ImageColorAllocate($image, 255, 255, 255), ImageColorAllocate($image, 0, 0, 0) and ImageColorAllocate($image, 204, 204, 204). Make the background black using ImageFill($image, 0, 0, $black) function and Generated image white color using ImageString($image, 3, 30, 3, $pass, $white). Create some lines to make it a little bit harder for any bots to break CAPTCHA easily. Tell the browser what kind of file is come in withe the help of header("Content-Type: image/jpeg") function. Display the new created image in JPEG format using ImageJpeg($image) function.

Use dynamically generated images on the other pages

Since the header can be sent only once and it is the only way to tell the browser that you are going to send image, it is not so easy to insert automatically generated images to the regular pages. There are three ways to do it:
  • The whole page can consist of the image.
  • You can save image in the file and then refer to it using <img> HTML descriptor.
  • You can put a script producing image in HTML descriptor, e.g.:
<img height="120" alt="Dynamically generated image" src="generate_dynamic_image.php" width="200">