PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files in a variety of different image formats, including gif, png, jpg, wbmp, and xpm. Even more convenient, PHP can output image streams directly to a browser.
PHP’s GD library is a great tool to manipulate graphics on the fly. Use the ImageCreate() function and then use the ImagePng(), ImageJpeg(), or ImageGif function, depending on your system:
header( “Content-type: image/gif” );
$im = ImageCreate( 153, 20 ); // width, height
$red = ImageColorAllocate( $im, 255, 0, 0 );
ImageDestroy( $im ); // Free memory
If you run the preceding script, you should see something like the image on this page: http://programmabilities.com/php/image-create.php. Let’s go over this line by line:
Line 2: Print the header telling the Web browser thet this is a GIF image.
Line 3: Create a GD image stream that can be manipulated by any of the other GD functions. The format of this image is not determined until we tell GD that it is a GIF and print it out on line 9.
Line 5: Create the background color for the image and set it to red using the ImageColorAllocate() function.
Line 6: Create the foreground color for the image and set it to white by using the ImageColorAllocate() function.
Line 8: Draw a string to image $im using a built-in font at x-pixel location of 3 and a y-pixel location of 3. The contents of the string is “Programmabilities.com” and the color of the string is white, denoted by the variable $white.
Line 9: Convert the image to a GIF and output to the browser.
Line last: Destroy the memory allocated for the GIF. This is done when the script executes anyway.
Chief Programmabilities, Programmabilities.com