Creating a dynamic image with PHP

To create a dynamic image is easy with PHP and the GD library. Its also a fun way to make your forum banner stand out by having it change each time someone loads it.

For example, I used to have the following forum signature while I was playing EverQuest II:

Forum Signature
Forum Signature

The image is created by a PHP page and the line at the bottom is randomly chosen from a list. Each time this page is reloaded, the message changes. The list of messages is actually from EverQuest I, messages that was shown in-game when you were zoning.

My example only uses text, but you can of course use graphics too. You could create a base image in a paint package and then use PHP to load that image and use it as a canvas to drop on instead of starting out with a blank background.

Here is the PHP I used to create this image:

<?php

# Andre Miller
# http://www.andremiller.net/
#

$v = array(
"Adding Randomly Mispeled Words Into Text...",
"Adding Vanilla Flavor to Ice Giants...",
"Always Frisky Kerrans <AFK>...",
"Attaching beards to dwarves...",
"Buy:LurN Tu Tok liK Da OgUr iN Ayt DaYz by OG...",
"Checking Anti-Camp Radius...",
"Creating randomly generated feature...",
"DING!...",
"Does Anyone Actually Read This?...",
"Doing things you dont wanna know about...",
"Dusting off spell books...",
"Ensuring Everything Works Perfektly...",
"Ensuring Gnomes are still short...",
"Filling Halflings With Pie...",
"Have You Hugged An Iksar Today?...",
"Have You Tried Batwing Crunchies Cereal?...",
"Hiding Catnip From Vah Shir...",
"Hitting Your Keyboard Won't Make This Faster...",
"If You Squeeze Dark Elves You Don't Get Wine...",
"Karnors..... over 40 billion trains served...",
"Loading, Don't Wait If You Don't Want To...",
"Loading useless information...",
"Loading velious textures...",
"Looking For Graphics <LFG>...",
"Looking Up Barbarian Kilts...",
"Look Out!!  Behind You...",
"Now Spawning Fippy_Darkpaw_432,326,312...",
"Oiling Clockworks...",
"Polishing erudite foreheads...",
"Preparing to Spin You Around Rapidly...",
"Refreshing Death Touch Ammunition...",
"Ruining My Own Lands...",
"Sanding Wood Elves - Now 37% smoother...",
"Searching High Elf Robes...",
"Sharpening Claws...",
"Sharpening Swords...",
"Spawning Your_Characters01...",
"Starching High Elf Robes...",
"Stringing Bows...",
"Stupidificationing Ogres...",
"Teaching Snakes to Kick...",
"Told You It Wasn't Made of Cheese...",
"Warning: Half Elves Are Now .49999 Elves...",
"Whacking Trolls With Ugly Stick...",
"You Have Gotten Better At Loading! (8)..."
);

$message = $v[rand(0, sizeof($v)-1)];
header ("Content-type: image/png");
$img_handle = ImageCreate (350, 60) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 255, 255, 255);
imagecolortransparent ($img_handle, $back_color);
$color_1 = ImageColorAllocate ($img_handle, 255, 102, 51);
$color_2 = ImageColorAllocate ($img_handle, 1, 1, 1);
$color_3 = ImageColorAllocate ($img_handle, 51, 102, 255);
ImageLine($img_handle, 0, 1, 250, 1, $color_2);
ImageString ($img_handle, 2, 0,2, "Dalden - 70 Illusionist / 70 Provisioner", $color_1);
ImageString ($img_handle, 2, 0,12, "Leader of Aenigma (Guild level 49)", $color_1);
ImageString ($img_handle, 2, 0,22, "Euro-time guild on Crushbone", $color_1);
ImageString ($img_handle, 31, 0,35, "Loading Please Wait...", $color_2);
ImageString ($img_handle, 0, 0, 50, $message, $color_3);
ImagePng ($img_handle);
?>

To keep the image transparent, just make sure you don’t use $back_color for any of the text.

Save this php script on your server and use it as the URL of an image.

For example: <img src=”http://www.andremiller.net/projects/dynamic-php-image/dalden_banner.php”>

This will show up as an image in your browser:

Forum Signature
Forum Signature

2 thoughts on “Creating a dynamic image with PHP”

Leave a Reply