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.
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="/books/2/2/1/html/2/ 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 to overlay usage information on images downloaded from your site, while preserving the unmarked original on the server for authorized use
I wrote this script to merge the file referred to by src with the watermark image. The script begins with some variable definitions:
<? $src_path="/path/to/hires/images"; $src_file = $src_path."/".$src; $wm_file = "watermark.png";
For this script, I've decided to save the images to be watermarked outside the web site's root directory. Placing the images in a directory outside the root ensures that visitors don't have any way to access your non-watermarked originals. Only the download.php script (which also applies the watermark) can access the files.
The $src_path variable defines the full server path to the images. By combining the filename in the variable $src with the value of $src_file, the script gets a file handle to pass to the GD library image functions. The watermark image is saved in same directory as the script, so no path is necessary in its file-handling variable ($wm_file).
The script uses the GD functions imagecreatefromjpeg( ) and imagecreatefrompng( ) to create image identifiers for the original and watermark images to be manipulated by the rest of the script:
$src_file = imagecreatefromjpeg($src_file); $wm_file = imagecreatefrompng($wm_file);
For information about using the GD function library for other image types, see the PHP web site manual page listed in the See Also section of this Recipe.
Passing the image identifiers to the functions imagesx( ) and imagesy( ) gives us the width and height of the original image and the watermark image:
$src_w=imagesx($src_file); $src_h=imagesy($src_file); $wm_w=imagesx($wm_file); $wm_h=imagesy($wm_file);
The imagecopy( ) function requires three pairs of numeric parameters to merge the watermark image ($wm_file) with the original image ($src_file): the placement coordinates ($output_x and $output_y) on the destination file, the starting coordinates (0,0) in the source file, and the size of the image to be placed ($wm_w, $wm_h):
$output_x = ( $src_w / 2 ) - ( $wm_w / 2 ); $output_y = ( $src_h / 2 ) - ( $wm_h / 2 ); imagecopy($src_file, $wm_file, $output_x, $output_y, 0, 0,$wm_w, $wm_h);
The PHP header( ) function sets the HTTP headers for content type, as well as the filename for the merged image, and the imagejpeg( ) function outputs the file to the browser:
header("Content-type: image/jpg"); header("Content-disposition: filename=".$src); imagejpeg ($src_file);
Finally, the script calls the imagedestroy( ) function twice to clear the image file handlers from memory:
imagedestroy($src_file); imagedestroy($wm_file); ?>
Recipe 6.11 discusses another use for the GD Graphics Library.
The PHP manual pages for the GD Graphics Library is online at http://www.php.net/manual/en/ref.image.php.