Image watermarking (PHP script)

Pass your mouse pointer over the above, before and after demo image to see what your images could look like after "watermarking" with PHP.
Rather than being a tutorial (there are already plenty), this is a simple spoon feed remedy for those who want to protect the images on their web pages with a "watermark" or some sort of copyright notice. This script can be used to either watermark your images on the fly or to save the watermarked images to file in Jpeg format. The first step is make the "watermark" which will be used to overlay the images. Create a .png image with a transparent or opaque background (your choice) containing whatever text you would like to "watermark" your images with. If you don't know how to create this image a quick search using Google will return lots of tutorials. I made the following image using Adobe Photoshop.

If you are viewing this with Internet Explorer, you may notice that browser's inability to display the transparency of the png image format properly, but don't worry, PHP's built in GD image manipulation library suffers from no such deficiency.
The following assumes some knowledge of PHP even if that knowledge is limited. First, the image or images to be watermarked will either be stored in a non public directory and watermarked on the fly when they are requested or they can be watermarked and then be stored in a public directory ready to serve without any need for subsequent processing. Whichever method you choose this script can do both. Copy the following code and paste it into a file named: function_watermark.php which is to be saved in your root directory.

<?php
function watermark($SourceFile, $WatermarkFile, $SaveToFile = NULL)
{
    
$watermark = @imagecreatefrompng($WatermarkFile)
    or exit(
'Cannot open the watermark file.');
    
imageAlphaBlending($watermark, false);
    
imageSaveAlpha($watermark, true);
    
$image_string = @file_get_contents($SourceFile)
    or exit(
'Cannot open image file.');
    
$image = @imagecreatefromstring($image_string)
    or exit(
'Not a valid image format.');
    
$imageWidth=imageSX($image);
    
$imageHeight=imageSY($image);
    
$watermarkWidth=imageSX($watermark);
    
$watermarkHeight=imageSY($watermark);
    
$coordinate_X = ( $imageWidth - 5) - ( $watermarkWidth);
    
$coordinate_Y = ( $imageHeight - 5) - ( $watermarkHeight);
    
imagecopy($image, $watermark, $coordinate_X, $coordinate_Y,
        
0, 0, $watermarkWidth, $watermarkHeight);
    if(!(
$SaveToFile)) header('Content-Type: image/jpeg');
    
imagejpeg ($image, $SaveToFile, 100);
    
imagedestroy($image);
    
imagedestroy($watermark);
    if(!(
$SaveToFile)) exit;
}
?>

Now, how do you use the above script? It is not possible to send text and image data together from the same PHP file on the same http request so don't bother trying because it wont work. If you want to watermark your images on the fly and send them straight to the browser use the following code in your script but it is imperative not to send any output to the browser prior to sending the watermarked image.

<?php
// The image should be located in a non public directory
$image_location = 'path/to/image/file/imagename.ext';

// Locate the watermark file wherever you choose (remember PNG format)
$watermark_location = 'path/to/watermark.png';

// Include the watermarking function file
require_once($_SERVER['DOCUMENT_ROOT'] . '/function_watermark.php');

// Watermark the image and send it to the browser
watermark($image_location, $watermark_location);
?>

The second method is very similar to the above but instead of outputting the image to the browser and exiting the script, the script saves the image to file and could be used to loop through a number of images in one go. The only difference is that a third argument is added to the call to the watermark function and this argument contains the path and filename to which the image will be saved.

<?php
// The image should be located in a non public directory
$image_location = 'path/to/image/file/imagename.ext';

// Locate the watermark file wherever you choose (remember PNG format)
$watermark_location = 'path/to/watermark.png';

// Location where you want to save the created watermarked file
$save_watermarked_file_to = 'path/and/filename.jpg';

// Include the watermarking function file
require_once($_SERVER['DOCUMENT_ROOT'] . '/function_watermark.php');

// Watermark the image and save it to file
watermark($image_location, $watermark_location, $save_watermarked_file_to);
?>

If you have any contributions, additions, suggestions or modifications for this article please don't hesitate to send an email.