O'Reilly logo

Web Site Cookbook by Doug Addison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.6. Creating Watermarked Images on the Fly

Problem

You want to visibly mark images downloaded from your site with a logo or copyright information to dissuade people from using them as their own.

Solution

Use the PHP image-generating functions that are part of Thomas Boutell's GD Graphics Library to merge a transparent watermark image with your original downloadable images.

First, create the image you want to use as a watermark as a 24-bit transparent PNG. The opacity on the watermark image should be reduced to about 25 to 40 percent. The watermark image can be any size, but you may want to tweak it depending on the dimensions of the original images it will appear over.

Then upload the original images you want to watermark to your web server. If you want to control how visitors to your site download images from it, put the originals in a directory outside of the web root (see Recipe 1.7), and then define that location in your watermarking script.

Then upload the script that will open the watermark image and the protected, high-resolution original, merge the two, and output the new image file to the browser as a download.

Finally, add linked thumbnails to a gallery page of thumbnail images that when clicked will download the watermarked large version of the file (as shown in Figure 5-15):

	<a href="download.php.?src=dog_hires.jpg"><img src=" dog_thumbnail.jpg"
	   width="75" height="100" align="none"></a>

Figure 5-15. Watermarking images on the fly with PHP's image-handling functions allows you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required