If you frequent forums, you'd probably remember images in people's signatures that would display information about your computer. Such mysticism! This tutorial will teach you that, and more!
First off, you need to have the GD module installed. To check, use the following PHP code:
echo "<xmp> ";
print_r( gd_info() );
echo "</xmp> ";
Here's what my output looks like:
[GD Version] => bundled (2.0.15 compatible)
[FreeType Support] => 1
[FreeType Linkage] => with freetype
[T1Lib Support] =>
[GIF Read Support] => 1
[GIF Create Support] =>
[JPG Support] => 1
[PNG Support] => 1
[WBMP Support] => 1
[XBM Support] => 1
[JIS-mapped Japanese Font Support] =>
If it's not installed on your server, you can grab it here [http://www.boutell.com/gd/
The first step—perhaps the most difficult—is to create a PNG image that you want to insert text onto. Here's mine:
Now, I'm gonna get ahead of myself and show you the final product:
Pretty cool, huh? Now, here's the code:
Header ("Content-type: image/png");
$img_handle = imageCreateFromPNG("http://www.icemelon.com/images/tutorials/bannerboy.png");
$color = ImageColorAllocate ($img_handle, 100, 100, 100);
$ip = $_SERVER['REMOTE_ADDR'];
ImageString ($img_handle, 3, 10, 9, "Your IP: $ip", $color);
Surprisingly, the code is quite short. It's also extremely straightforward.
Break it down
» First, we send the header information to let your visitor's computer know we're sending a PNG file.
» Next, the imageCreateFromPNG
function creates your PNG image from the existing one I showed you first.
» Then we define a color (conveniently called $color) using ImageColorAllocate
. The last three parameters specificy the RGB (red green blue) values, respectively. Enter those as integers (or as hex values, proceeded by 0x—e.g. 0xFA).
» Since we are displaying IP addresses, $ip is set to the visitor's IP.
» Finally, we are ready to add the text to the image, using ImageString
. The three numerical parameters are defined as follows: font size, x-offset, and y-offset (respectively). For the font size, values 1 through 5 are predefined font sizes (1 being the smallest).
» The last two functions, ImagePng
, are self explanatory.
Save the script above as whatever.php and you can now reference it in HTML as an image:
Since it is
a PHP script, you can modify the script slightly to grab values passed in through the filename. Here are a few examples of that concept in action:
Try it out yourself:
For a plethora of related PHP functions, check out PHP.net's gd page
. Have fun with it!