This code can be useful for a number of things, such as making dynamic banners or for adding a copyright type of watermark to photographs or artwork (as we do in snipe gallery). As usual, this will not work for gifs unless you have a version of gd that lets you do that (cuz the folks at Unisys are a bunch of mo-mos).
The example here is taken from the Godsmack sig generator, so it’s designed to create white text with a black drop shadow on a preformatted blank banner.
Note: Remember that PHP must be compiled with jpeg/png/gd support, AND that the font file must be uploaded to the server for this to work. For our purposes, we’ll assume you’re going to take this snippet and make it into its own file, which we’ll call “mkwatermark.php”.
[sourcecode language=’php’] // specify the file name – you can use a full path, or “../../” type stuff here// if the image is not in the same directory as this code file
$image = imagecreatefrompng(“gs-banner-sm.png”);
// specify the font size
$font_size = 14;
// in this case, the color is white, but you can replace the numbers with the RGB values
// of any color you want
$color = imagecolorallocate($image, 255,255,255);
// make our drop shadow color
$black = imagecolorallocate($image, 0,0,0);
// and now we do the overlay – the layers of text start top to bottom, so
// the drop shadow comes first
// $image – the base image file we specified above
// $font_size – Well duh. Its the size of the font
// 0 – the angle of the text – we don’t want an angle, so we leave it at 0
// 55 – pixels to the right from the leftmost part of the image
// 35 – pixels down from the top of the image
// $black – the color we defined above
// “../fonts/ARIALBD.TTF” – the location on the server that the font can be found
// “Test Text” – the text we’re overlaying – you can also use a variable here
ImageTTFText ($image, $font_size, 0, 56, 36, $black, “../fonts/ARIALBD.TTF”,”Test Text”);
// Now add the actual white text “on top”
ImageTTFText ($image, $font_size, 0, 55, 35, $color, “../fonts/ARIALBD.TTF”,”Test Text”);
header(“Content-type: image/png”);
imagepng($image);
imagedestroy($image);
}
[/sourcecode]
To print out the image, we would just have to wite the html as:
<img src="mkwatermark.php">
Note about Variables – If you are using any variables outside the file to determine what the code does (for example, making the text a variable as we do with the Godsmack sig generator), be sure to secure your code and check to be sure the user can’t do any damage to your system by entering harmful values.