Add a Watermark to a Bulk of Images

Over the years, I have had a lot of pictures that I’ve added to my picture gallery.  Today, I was adding more pictures and I thought – maybe I should start protecting my pictures with a watermark in case someone tried to use them on their site.

The gallery that I use allows for a watermark – but the watermark is only applied to those pictures that you add AFTER you enable the watermark feature.  Up until today, I have had the watermark feature disabled.  I figured that I could simply turn it on at any point, and using the GD library of PHP, the watermark would simply appear over all of the images.  Not true.

So I began looking for a way to add a watermark to a batch of pictures.  There were some programs out there that did it – and even some scripts in Gimp – but the ones I found only allowed you to add a text copyright watermark to the graphics.  The one I tried in GIMP actually used a map effect that made the text look embedded into the picture – but in some of the pictures, you could barely see the watermark.

Knowing that the picture galleries could add an image watermark to a picture when adding it to the site, I knew that there must be a way to add a watermark to a bulk of pictures.

Finally after doing some coding and work for a couple of hours, I found the solution.  Sure, it took a few hours to code this and get it working, but think of the amount of time it would take if I were to add the graphic watermark to over 1,500 pictures that I already have on my site.  It would take days!

After I wrote the code, it took less than ONE MINUTE to add the watermark to over 1,500 pictures on my hosting servers that have a 2.8 GHz processor.  Amazing.  So two hours and one minute to add a watermark to a large number of pictures – amazing.

So, here is the quick PHP script that you can add to your hosting directory and then run it from a browser.  There are a few variables that need to be changed in order to add a watermark to a mass number of pictures and I’ll cover those.

ini_set(‘display_errors’, 1);

function getDirectory( $path = ‘.’, $level = 0 )
    $ignore = array(‘.’, ‘..’,);
    // Directories to ignore when performing the watermark.
    $dh = @opendir( $path );
    // Open the directory to the handle $dh
    while( false !== ( $file = readdir( $dh ) ) )
        // Loop through the directory   
        if( !in_array( $file, $ignore ) )
            // Check that this file is not to be ignored
            if( is_dir( “$path/$file” ) )
                // This is a directory so we need to recurse into the directory
                getDirectory( “$path/$file”, ($level+1) );
                // We found a file – so now we need to ensure the file is a .jpg or a .JPG or we shouldn’t process it
                if(strpos($file, “.jpg”) !== false || strpos($file, “.JPG”) !== false)
                    // Print out the full path of the file that is having the watermark applied
                    echo “$path/$file<br />”;
                    $watermark = imagecreatefrompng(‘ENTER FULL PATH OF YOUR WATERMARK IMAGE‘);
                    $watermark_width = imagesx($watermark); 
                    $watermark_height = imagesy($watermark); 
                    $image = imagecreatetruecolor($watermark_width, $watermark_height); 
                    $image = imagecreatefromjpeg($path . ‘/’ . $file); 
                    $size = getimagesize($path . ‘/’ . $file); 
                    $dest_x = $size[0] – $watermark_width – 10; 
                    $dest_y = $size[1] – $watermark_height – 10; 
                    imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
                    imagejpeg($image, $path . ‘/’ . $file);
    closedir( $dh );
    // Close the directory handle

If you would rather download the code, simply do so by clicking this watermark.txt link.

So, there are two areas that you need to change that are highlighted in bold in the script above.

On the fourth line – you see it calls “getDirectory”.  In here, you need to put the directory where all of your images are stored at – which you want to have a watermark applied.

NOTE – All files with a .jpg or .JPG extension in the directory you specify – and any subdirectories – will have the watermark applied – so you may want to make a backup before hand!

So, if I have all of my files in /home/user72/images/gallery – the line would look like:


Now, there is one more setting you need to change – and that is the full path to the actual image that is your watermark.  Here is the line:

$watermark = imagecreatefrompng(‘ENTER FULL PATH OF YOUR WATERMARK IMAGE‘);

So, you need to enter the full path to the image that will be the watermark over each of the images.  So if my watermark file is:


I will then make the line:

$watermark = imagecreatefrompng(‘/home/user72/images/gallery/watermark/copyright-watermark.png’);

NOTE – All watermarks MUST be in PNG format – NOT GIF or JPG or any other file type for this to work.

After you have set those two settings and saved the file in your hosting account (ensure the file is named <file>.php), simply open your browser and go to your location where the file is stored.  So if I named the file watermark.php and put it right in the main html folder of my site – and my domain was just, I would open up a browser and type in “”.

Depending upon how many pictures you have and how much processing power your hosting provider gives you, it may take some time to run this script.  Like I said, I have full access to my hosting servers and I ran one of the processor cores at 100% for just under one minute to add a watermark to all of my 1,500+ images.

Once done, you should see a listing of all of the files that were watermarked.

That is all!  I hope this script is helpful to those looking to add a watermark to a group of pictures.

2 thoughts on “Add a Watermark to a Bulk of Images”

  1. I wasted a day trying to make the ImageMagick shell script work, later I tried this, I was stunned!

    Thanks a lot buddy!


Leave a Comment