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.
Original tutorial can be found at baglan.web.trm.
Latest Articles
About Us
NZ Web Hosting Internet Technolgies Blog.
Website design, website development, promotion and search engine optimisation.












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 imageimagecopy($image, $watermark, (($w/2)-($ww/2)), $h-$wh, 0, 0, $ww, $wh);
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
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.
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() ?>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 imageheader('Content-type: image/jpeg');
imagejpeg($image);
to:
// Send the imageheader('Content-type: image/jpeg');
imagejpeg($image,null,95);