A Simple PHP Watermark

Keywords:
16 Sep 2007

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.

Not working

I have created a .htaccess file in the directory I want to watermark (images) and the php wrapper file but it wont work. What am I doing wrong? All I get is a blank image!

Submitted by Anonymous (not verified) on Tue, 2010-03-16 10:12.

change watermark to upper left

Not quite sure of the maths involved in calculating this. What's the operation to position the watermark to the top left corner? Incidentally, I've got a 30 pixel transparent canvas all around my PNG to keep it from abutting the image edge -- it looks very nice!

Cheers,

Submitted by seezee (not verified) on Wed, 2010-08-11 09:46.

Figured out the maths

This seems to work:

<?php
//imagecopy($image, $watermark, $w-$ww, $h-$wh, 0, 0, $ww, $wh);// place watermark in lower right corner
//imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);// center watermark
imagecopy($image, $watermark, (($w/96)-($ww/96)), (($h/96)-($wh/96)), 0, 0, $ww, $wh);// place watermark in upper left corner
?>

Submitted by seezee (not verified) on Thu, 2010-08-12 03:14.

other files

Hi,

I have vbulletin 4 and my image is stores like "1.attach"

the script works fine but the watermark don't put to the center or to the bottom. The watermark is truncated.

Can you help me please ?

Submitted by Nick (not verified) on Mon, 2010-08-16 21:02.

About Us

NZ Web Hosting Internet Technolgies Blog.

Website design, website development, promotion and search engine optimisation.