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”.

// 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);
}

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.

Advertisement

Themeforest

Advertisement

468x60_makemoney
Previous post

Dynamic thumbnailing with PHP and the GD library

Next post

Cropped Thumbnails using PHP and the GD Library

snipe

snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist. I'm the CTO at Mass Mosaic and the CEO of Grokability, Inc. in San Diego, CA. Tweet at me @snipeyhead or read more...

  • Elaine

    Well, shit! I just spent literally all day trying to figure this out from scratch and ended up with a crappier solution that took 4x more lines of code.

    This is so going in my Evernote file. 😛

    Thank you!

  • Elaine

    Well, shit! I just spent literally all day trying to figure this out from scratch and ended up with a crappier solution that took 4x more lines of code.

    This is so going in my Evernote file. 😛

    Thank you!

  • Dirk

    Thanks for the simple yet effective example.
    Two things:
    1. The curly bracket on line 33 causes an error, there’s no starting bracket 😉
    2. Some versions of GD has an error that causes the font file not to be loaded if it’s in the same directory as your script. To get past this, I used ../samedirectory/font.ttf

  • Dirk

    Thanks for the simple yet effective example.
    Two things:
    1. The curly bracket on line 33 causes an error, there’s no starting bracket 😉
    2. Some versions of GD has an error that causes the font file not to be loaded if it’s in the same directory as your script. To get past this, I used ../samedirectory/font.ttf

  • Guset

    This is exaclty what I was looking for. Very simple way to add shadow to the text. Great. Really helpful.