Dynamic Image Creation with PHP

The image functions of GD library is the easiest way to create dynamic image with text in PHP. In some situation, you need to create an image on the fly and write dynamic text to image. To generate a dynamic random image with PHP, GD library needs to be installed. In this tutorial, we will show you how to create dynamic image with PHP.

The example code creates a dynamic image with PHP based on the specified configuration. You can customize the image width, height, background color, text color, and text.

Create Dynamic Image with PHP

The following code generates a random dynamic image using PHP. The GD image functions are used to create dynamic image with PHP.

  • imagecreate() – Creates blank image resource of specified size (width and hwight).
  • imagecolorallocate() – Allocate a color for an image resource.
  • imagettfbbox() – Calculate the bounding box in pixels for TrueType text.
  • imagettftext() – Write text to the image using TrueType fonts.
  • imagepng() – Creates a PNG image.
  • imagedestroy() – Destroy an image resource.
// Specify font path
$font 'fonts/verdana.ttf';

// Text font size
$font_size 10;

// Get settings from URL
$setting = isset($_GET['s']) ? $_GET['s'] : "000_FFF_350_350";
$setting explode("_"$setting);

$img = array();

// Define image width, height, and color
switch($n count($setting)){
    case 
$n :
    case 
3:
        
$setting[3] = $setting[2];
    case 
4:
        
$img['width'] = (int) $setting[2];
        
$img['height'] = (int) $setting[3];
    case 
2:
        
$img['background'] = $setting[0];
        
$img['color'] = $setting[1];
        break;
    default:
        list(
$img['background'], $img['color'], $img['width'], $img['height']) = array('F''0'100100);
        break;
}
$background explode(",",hex2rgb($img['background']));
$textColorRgb explode(",",hex2rgb($img['color']));
$width = empty($img['width']) ? 100 $img['width'];
$height = empty($img['height']) ? 100 $img['height'];

// Get text from URL
$text = (string) isset($_GET['t']) ? urldecode($_GET['t']) : $width ." x "$height;

// Create the image resource 
$image = @imagecreate($width$height) or die("Cannot Initialize new GD image stream");

// Create image background
$background_color imagecolorallocate($image$background[0], $background[1], $background[2]);

// Grab the width & height of the text box
$bounding_box_size imagettfbbox($font_size0$font$text);
$text_width $bounding_box_size[2] - $bounding_box_size[0];
$text_height $bounding_box_size[7]-$bounding_box_size[1];

// Text x&y coordinates
$x ceil(($width $text_width) / 2);
$y ceil(($height $text_height) / 2);

// Define text color
$text_color imagecolorallocate($image$textColorRgb[0], $textColorRgb[1], $textColorRgb[2]);

// Write text to image
imagettftext($image$font_size0$x$y$text_color$font$text);

// Set the content type header - in this case image/png
header('Content-Type: image/png');

// Output the image
imagepng($image);

// Free up memory
imagedestroy($image);

// Convert color code to rgb
function hex2rgb($hex) {
    
$hex str_replace("#"""$hex);

    switch(
strlen($hex)){
        case 
1:
            
$hex $hex.$hex;
        case 
2:
            
$r hexdec($hex);
            
$g hexdec($hex);
            
$b hexdec($hex);
            break;
        case 
3:
            
$r hexdec(substr($hex,0,1).substr($hex,0,1));
            
$g hexdec(substr($hex,1,1).substr($hex,1,1));
            
$b hexdec(substr($hex,2,1).substr($hex,2,1));
            break;
        default:
            
$r hexdec(substr($hex,0,2));
            
$g hexdec(substr($hex,2,2));
            
$b hexdec(substr($hex,4,2));
            break;
    }

    
$rgb = array($r$g$b);
    return 
implode(","$rgb); 
}

Configure Options:

  • s – Specify the image background color, foreground color, width, and height in the query string. The settings should be separated by an underscore (_). (For example, 000_FFF_350_350)
  • t – Specify text that you want to write on the image. By default, the image width and height will be written over the image.

Usage:
Specify the URL of the dynamic image generation script with the configuration option in the src tag.

<img src="create_image.php?s=000_FFF_350_350&t=Dynamic Image" >

Conclusion

The dynamic image creation functionality is very useful when you want to create a random image on the fly for Captcha code. If you want to make the image transparent, don’t use the background color. You can easily extend our dynamic image generation code functionality as per your needs.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

4 Comments

  1. Woody Said...
  2. Fábio Santos Said...
  3. Waleed Aslam Said...
  4. Geoffrey Barnes Said...

Leave a reply

keyboard_double_arrow_up