AboutBlogContact
Web DevelopmentMarch 12, 2003 3 min read 84Updated: May 3, 2026

The Visual Web: Generating Dynamic Graphics with PHP 4.3 and GD (2003)

AunimedaAunimeda
📋 Table of Contents

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

Read Also

The GraphQL Inflection Point: Moving Beyond REST in 2018aunimeda
Web Development

The GraphQL Inflection Point: Moving Beyond REST in 2018

Over-fetching and under-fetching are the silent killers of mobile performance. In 2018, we are adopting GraphQL to give our frontend developers the power to define exactly what data they need, and nothing more.

The TypeScript Tipping Point: Why We Stopped Shipping Raw JavaScript in 2018aunimeda
Web Development

The TypeScript Tipping Point: Why We Stopped Shipping Raw JavaScript in 2018

The days of 'undefined is not a function' are numbered. In 2018, we have officially moved all new agency projects to TypeScript. Here is why static typing is the best investment for long-term project health.

Stripe: Finally, a Payments API for Developersaunimeda
Web Development

Stripe: Finally, a Payments API for Developers

Accepting credit cards on the web has always been a nightmare of merchant accounts and clunky gateways. Stripe is here to fix that with a few lines of code.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Web Development

Get Consultation All articles