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

Get Web Site Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.