Select Page

Digital watermarking with PHP and GD2

Digital watermarking with PHP and GD2



Digital watermarking is the process of possibly irreversibly embedding information into a digital signal. The signal may be audio, pictures or video, for example. If the signal is copied, then the information is also carried in the copy. In visible watermarking, the information is visible in the picture or video. Typically, the information is text or a logo which identifies the owner of the media. [Wikipedia]

If you decide to go with an online watermarking, you can let watermark picture on the fly using php and .htaccess (at the cost of additional CPU server resources).

Why Watermarking pictures?

There is a lot of reasons, among others:

  • To limit images stealing, advanced users will still be able to crop/blur your watermark through!
  • To drive more new users/returning visitors to your site, anybody using your images in a forums may want to visit your site if the watermark can be read (don’t use complex logo, except if you have a well known brand)
  • To advertise your online work,

This article is a follow up of a previous article presenting how to watermark picture offline (definitive watermarking) using ImageMagick bash script. A lot of steps described there still apply


  • Watermark pictures on the fly as soon as they are bigger than 500 pixels in width (PHP)
  • Watermark all or only pictures that are hot linked from outside your blog, for example in forums, other blogs (.htaccess setting)
  • Don’t watermark pictures hot linked from search images engine: Google images search or other (.htaccess setting)
  • Easily extensible to your needs: support multiple watermark pictures based on original image size.


create a file .htaccess with the following content, I recommend you to save it at the root of Joomla!® directory structure, you can merge this file with the one provided by Joomla!

# Uncomment next line to avoid that watermark apply on your
# site, replace with you Joomla! base
#rewritecond %{http_referer} !^
http://([^.]*[.]*)? [nc]

# Uncomment next line to avoid applying watermark in Google images
# search, copy as many lines as needed if you would like to also add Bing, Yahoo, etc…
#rewritecond %{http_referer} !^http://(www.)? [nc]

RewriteCond %{REQUEST_FILENAME} –f

# Path to the watermark.php script, and list of supported images
RewriteRule .(gif|jpeg|jpg|png)$ /images/watermark.php [QSA,NC]

watermark.php script

Save this script into Joomla!® /images directory, under /images/watermark.php

Decide at which size you want to protect your images, here starting at 500pixels width, I will apply a watermark overlay in the left corner of the image.

  $image = imagecreatefromstring(file_get_contents($path));
  $w = imagesx($image);
  if ($w > 500) {
    $h = imagesy($image);
    $watermark = imagecreatefrompng('watermark.png');
    $ww = imagesx($watermark);
    $wh = imagesy($watermark);
    imagecopy($image, $watermark, $w-$ww, $h-$wh, 0, 0, $ww, $wh);
  header('Content-type: image/jpeg');


It cost CPU and depending how you set your .htaccess may still allow users to steal images without watermark.

About The Author

Cédric Walter

I worked with various Insurances companies across Switzerland on online applications handling billion premium volumes. I love to continuously spark my creativity in many different and challenging open-source projects fueled by my great passion for innovation and blockchain technology.In my technical role as a senior software engineer and Blockchain consultant, I help to define and implement innovative solutions in the scope of both blockchain and traditional products, solutions, and services. I can support the full spectrum of software development activities, starting from analyzing ideas and business cases and up to the production deployment of the solutions.I'm the Founder and CEO of Disruptr GmbH.