The Visual Web: Generating Dynamic Graphics with PHP 4.3 and GD
In 2003, the web is finally starting to look professional. Static text and pre-made buttons are no longer enough for a top-tier application. With the release of PHP 4.3.0, the GD library is now bundled by default, and it’s a total game-changer for web developers. We can now generate PNG, JPEG, and even GIF images (now that the LZW patent has expired) entirely on the fly.
Dynamic graphs, CAPTCHA codes, and real-time banner generation are now within reach of any PHP developer.
The Basic Image Lifecycle
The workflow is consistent: create a blank canvas, allocate colors, draw your shapes or text, and then stream the raw image data to the browser with the correct HTTP headers.
<?php
// Tell the browser this is a PNG image
header("Content-type: image/png");
// Create a 200x50 blank canvas
$im = @imagecreate(200, 50) or die("Cannot Initialize new GD image stream");
// Allocate colors: The first color allocated becomes the background
$background_color = imagecolorallocate($im, 255, 255, 255); // White
$text_color = imagecolorallocate($im, 233, 14, 91); // A nice 2003-era pink
// Draw a string onto the image
imagestring($im, 5, 5, 5, "PHP 4.3 is Live!", $text_color);
// Output the image and free memory
imagepng($im);
imagedestroy($im);
?>
TrueType Fonts for a Professional Look
imagestring() is fine for simple labels, but if you want your site to look modern, you need to use TrueType fonts (.ttf). PHP 4.3 handles this via imagettftext(), which provides anti-aliased, high-quality typography.
// Use a TrueType font for high-quality text
$font = '/usr/share/fonts/arial.ttf';
$black = imagecolorallocate($im, 0, 0, 0);
// Draw the text with a 12pt size and no rotation
imagettftext($im, 12, 0, 10, 20, $black, $font, "Anti-Aliased Text");
Server-Side Watermarking
A common use case in 2003 is protecting your site's photos. Instead of manually watermarking every image in Photoshop, we can do it automatically when the user requests the file.
// Load your original image
$main_img = imagecreatefromjpeg("photo.jpg");
$watermark = imagecreatefrompng("logo.png");
// Get dimensions
$m_width = imagesx($main_img);
$m_height = imagesy($main_img);
$w_width = imagesx($watermark);
$w_height = imagesy($watermark);
// Merge the watermark onto the bottom-right corner
imagecopy($main_img, $watermark, $m_width - $w_width - 10, $m_height - $w_height - 10, 0, 0, $w_width, $w_height);
// Output the watermarked result
imagejpeg($main_img);
Memory Management and Optimization
GD is a memory-hungry library. For a 1600x1200 image, PHP might need 15MB+ of RAM just for the buffer. If your memory_limit in php.ini is set to the default 8MB, your scripts will crash. Raise it to at least 16MB or 32MB for image processing.
Also, remember that generating images on every request is CPU-intensive. A professional developer will always cache the output to a temporary file on disk using imagepng($im, "cache/image_123.png") and serve the static file if it already exists.
Aunimeda develops websites and web applications for businesses - corporate sites, e-commerce, portals, and custom platforms.
Contact us to discuss your web project. See also: Web Development, E-commerce Development