A Simple PHP Watermark

With a recent requirement to protect some images with a non-destructive watermark on a Pixelpost implementation I came across the following fairly simple solution. Using PHP and mod rewrite, you can easily apply a transparent png watermark to images as they are served or "on the fly". The original image remains unmodified! You can easily change the watermark at any time by replacing the watermark image with a different image file.

There is however a downside of some additional load on the server, as each image has to be created each time it is viewed. You do also lose some quality in the image (but I have found the result to be quite acceptable).

To apply this to Pixelpost do the following:

Create a watermark named watermark.png and place it in the admin/ directory along with the watermark_wrapper.php file below.

<?php
// watermark_wrapper.php

// Path the the requested file
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

// Load the requested image
$image = imagecreatefromstring(file_get_contents($path));

$w = imagesx($image);
$h = imagesy($image);

// Load the watermark image
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);

// Merge watermark upon the original image
imagecopy($image, $watermark, $w-$ww, $h-$wh, 0, 0, $ww, $wh);

// Send the image
header('Content-type: image/jpeg');
imagejpeg($image);
exit();
?>
 

Next you will need to create a .htaccess file in the image/ directory with the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(gif|jpeg|jpg|png)$ /admin/watermark_wrapper.php [QSA,NC]

You can easily create a png watermark file with a transparent background using your favourite image editor. I have used the Gimp. I have found that the best watermarks are made of 2 separte text layers with a light colour (#e1e1e1) on top of a dark color (#000000) and slightly offset. This will show equally well on both light and dark images. I use "Comic Sans MS Bold" font.

Download the Pixelpost-Watermark.zip file with a sample watermark.png (and the original watermark.xcf - Gimp native file), .htaccess and watermark_wrapper.php.

If you update the following

If you update the following line of the script it will centre the watermark on the image:

// Merge watermark upon the original image
imagecopy($image, $watermark, (($w/2)-($ww/2)), $h-$wh, 0, 0, $ww, $wh);

Submitted by nzwhost on Thu, 2007-09-27 21:25.

Server upgrade

Aloha.
Since my bluehost server got upgraded to PHP5 the watermarking script is not working anymore. Trying to troubleshoot this but no luck so far. Grateful for help :)
http://www.denialofservice.de

Submitted by inDenial (not verified) on Thu, 2007-12-06 18:44.

Will Take a Look

Kia ora,

Thanks for the info!

I will take a look at this in the next few days and let you know once it is sorted out. I also look after a photo blog site which will be changing to PHP5 shortly.

Submitted by nzwhost on Thu, 2007-12-06 20:26.

PHP 5.2.4 Works Fine

inDenial,

I have just tested the script on a Pixelpost site running PHP 5.2.4 and it works fine.

Perhaps it has something to do with the way the new version of PHP was compiled on your server?

You can always create a file and call it info.php with the following code and point your web browser to it. This may give you some more clues to where the problem may be.

<?php phpinfo() ?>

Submitted by nzwhost on Sun, 2007-12-09 19:12.

Improved image quality

To improve image quality when writing the jpg file you can specify the quality of the jpg image. The default looks like about 75% quality, but if you change to 95% there is very little difference than the original image.

Change the section:

// Send the image
header('Content-type: image/jpeg');
imagejpeg($image);

to:

// Send the image
header('Content-type: image/jpeg');
imagejpeg($image,null,95);

Submitted by nzwhost on Sat, 2008-06-14 10:03.

The Watermark image is still

The Watermark image is still NOT centered but shown on the bottom.

How can I center the Watermark in the middle of the image?

Thx in advance

Submitted by Arames (not verified) on Sat, 2009-05-09 12:59.

Center

Center center

imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);

Submitted by Anonymous (not verified) on Sun, 2009-10-04 22:13.

finally!

I tried like 10 of these htaccess/php combinatiosn on the web and this was the only one that worked in my situation. thank you so much for posting it here.

Submitted by matt (not verified) on Wed, 2009-11-04 13:10.

Watermark only image-dir

Hi there

wow - great function - SUPER

Well, how can I tell the script only to watermark the images from a special directory.

Now I'll get ALL PixelPost-Admin-Template-Pics watermarked ... that's not what I want. My target i to watermarkt ONLY the pictures I want to show ...

I thing, I have to change this lines - but what and where?

// Path the the requested file
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

Submitted by Tobias (not verified) on Wed, 2010-02-10 12:02.

Everything ok ...

Problem solved ...

Submitted by Tobias (not verified) on Wed, 2010-02-10 20:32.