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.

// watermark_wrapper.php

// Path the the requested file

// 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');

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!


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

Figured out the maths

This seems to work:

//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


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.

resize the watermark to % of image

Hi I use this script on my site and it works great.
The only thing is Thad my vendors upload images in different size and I wonder if it would possible to ad a line to the code to size the watermark I use to like
30 % of the image.
I don't know php maybe someone can Help?

Submitted by Wolfgang (not verified) on Thu, 2010-12-30 05:30.

size watermark % of image

Hi I use the script on my site and works great.
I wonder if it is possible to ad code so the watermark image I use re-sizes to 30 or 25 % of the image.
My vendors upload images in different sizes and this would take care of the problem.
Is this possible? I don't know php.
Any help appreciated.

Submitted by Wolfgang (not verified) on Thu, 2010-12-30 05:43.

I found a free online tool

I found a free online tool called watermark image much Watermarker to automate the entire process of image watermarks. If you're away from your PC and to download the software, just use this online version of the tool watermark free. It's fast and easy to use.

Submitted by William Smith (not verified) on Sat, 2011-06-18 17:39.

Water mark size

Why does watermark.png change size, goes smaller on larger images and larger on smaller images... how do I keep it the same size?


Submitted by Nick (not verified) on Wed, 2011-10-12 08:07.

Hmm, how did you do it? I

Hmm, how did you do it? I also want the images in /images directory to be watermarked, not the ones in /images/thumbs/ . Now it also watermarks the thumbnail images....

Submitted by Kuvat On (not verified) on Wed, 2012-02-22 00:47.