Watermarking images with PHP
PHP, coupled with Apache rewriting, lets watermark "on the fly" and transparently, without modifying the original image.
# .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(gif|jpeg|jpg|png)$ watermark.php [QSA,NC]
<?php
// watermark.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();
?>





Transparent request
Request is internally and transparently redirected - URL stays the same.
Transparency
Alpha transparency support in PNG lets you get creative with the watermark. I used the trick on the Beyu.ru site - colored "brushstrokes" are generated dynamically using a transparent PNG mask.
Requirements
This code requires Apache mod_rewrite and GD graphic library version 2+ to be operational; all the Apache+PHP hostings I came across have that much.
Example files
Follow the link to download the example files used in this article.
Cached version
While this script is good for illustrating basic ideas, it has an important shortcoming. The watermarked image is generated with every request, placing additional load on the server. That additional burden is neglectable for low-load installation, but can grow for higher-traffic, image intensive services.
Good news is, I've made an updated version which uses caching.