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:


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

17 comments
  1. Pingback: Rajeev Edmonds
  2. 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

  3. 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

  4. 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).

  5. 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).

  6. Pingback: Dave Drager
  7. Pingback: Alessandro
  8. Pingback: NoiseGraphics
  9. Pingback: Jess

Comments are closed.

You May Also Like

Delete all directories more than a week old

This simple linux bash script will delete subdirectories in a directory based…

Set Operations Using Bash Commands

Found a great post over at good coders code, great reuse. This…

PHP Script for controlling APC PDU via SNMP

This one is neat – you can control those APC PDU Power…

Outlook 2003 or 2007 Won’t Save Hosted Exchange Password

For many people using hosted Exchange services, password saving problems could plague…