Use PHP, GD and .htaccess to Watermark All Images in a Directory

The goal here is to watermark all images in a certain directory, except for thumbnails or other selection. You can either do this on each file prior to placing on your webserver – which is probably wise for CPU load issues – but let’s just say you want to do this for all files served in a single directory dynamically, a gallery for example.

The first step is to create a .png file with transparency which holds your watermark image. For this exercise, I’ve created this image:

tbwm.png

(I’ve added the border to stand the image out from the background of the page).

Here is the original image we are going to test with:

boratwow.jpg

After we have our watermark and sample image, we need to write a php file to use PHP’s GD function to apply this image to our original image. The particular function we use is imagecopy(). Here is the code I am using, I name it w.php:

$basedir=”/home/user/public_html/com/gallery/”;
$watermarkimage=”tbwm.png”;

$file=basename($_GET['i']);

$image = $basedir.”/”.$file;
$watermark = $basedir.”/”.$watermarkimage;

$im = imagecreatefrompng($watermark);

$ext = substr($image, -3);

if (strtolower($ext) == “gif”) {
if (!$im2 = imagecreatefromgif($image)) {
echo “Error opening $image!”; exit;
}
} else if(strtolower($ext) == “jpg”) {
if (!$im2 = imagecreatefromjpeg($image)) {
echo “Error opening $image!”; exit;
}
} else if(strtolower($ext) == “png”) {
if (!$im2 = imagecreatefrompng($image)) {
echo “Error opening $image!”; exit;
}
} else {
die;
}
imagefilledrectangle($im2, 0 , (imagesy($im2))-(imagesy($im)) , imagesx($im2) , imagesy($im2) , imagecolorallocatealpha($im2, 0, 0, 0, 100) );
imagecopy($im2, $im, (imagesx($im2)-(imagesx($im))), (imagesy($im2))-(imagesy($im)), 0, 0, imagesx($im), imagesy($im));

$last_modified = gmdate(‘D, d M Y H:i:s T’, filemtime ($image));

header(“Last-Modified: $last_modified”);
header(“Content-Type: image/jpeg”);
imagejpeg($im2,NULL,95);
imagedestroy($im);
imagedestroy($im2);

?>

This file is placed in the images directory.

Also in the images, create an .htaccess file with the following code:

RewriteEngine on
RewriteRule ^([^thumb].*\.[jJ].*)$ /com/gallery/w.php?i=$1

This tells the web server that instead of serving jpg files out of this directory, that we should instead process the filename with w.php and then serve to the browser. It also adds in a clause that if it starts with thumb_, that it will not run on this file. This is so it does not run on thumbnails.

Here is the resulting image, with watermark! This is served right out of an image directory with no watermark on the original picture:

Borat with watermark from php

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Digg
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • StumbleUpon

3 Trackbacks

  1. By Rajeev Edmonds on July 13, 2008 at 5:50 am

    Blogger Tips: Use PHP, GD And .htaccess To Watermark All Images In A Directory ( Uploads) Of Your WP Blog http://snurl.com/2xoib

  2. By Dave Drager on August 6, 2009 at 10:52 am

    This post recently became popular on Stumbleupon: Use .htaccess and PHP to watermark all images in a directory – http://bit.ly/3wuuxG

  3. By Alessandro on January 1, 2010 at 8:55 pm

    Use #PHP, #GD and .htaccess to #Watermark All Images in a Directory http://bit.ly/7mDQC3

6 Comments

  1. Perfect! I had found a PHP script, but I’m glad I went to the next Google result and found this! Thanks much!

    Posted March 26, 2008 at 4:04 am | Permalink
  2. Allen

    Hi

    Could you please help me and let me know how can I use this cdoing with xcart website.

    I have tried it but it didn’t work can you help please.

    Many Thanks
    Allen

    Posted November 12, 2008 at 1:53 am | Permalink
  3. Hannes

    Fascinating site and well worth the visit. I will be backG

    Posted November 21, 2008 at 5:03 am | Permalink
  4. eon

    What I don’t get yet is how it’s ok to send headers when other content has already been output. Normally, that would cause a PHP error, but all these watermarking scripts seem to do it, so it must be ok (though I’ve not been able to get watermarking to work on our site).

    Posted November 25, 2008 at 10:37 am | Permalink
  5. John

    Hi, if my thumb images are like image_thumb.jpg .. how do i wirte in .htaccess?

    Thank you

    Posted December 3, 2008 at 7:20 pm | Permalink
  6. matt

    That php code you inserted wont work very well without a <?php in front of it.

    Posted November 3, 2009 at 2:53 am | Permalink