Monthly Archives: June 2011

Transparent tiled watermark using GD with PHP

This is a basic method for dynamically adding tiled watermarks to images on your site.  It uses the .htaccess file to give the images to the PHP script which returns the image plus watermarking.

1) Create the image you want to be the watermark as a png with the opacity you want to be used (we’re not lowering the opacity later), and make sure to add extra space around the image according to how big you want the tiles.

2) Add this script to your site as watermark.php:


$watermark = imagecreatefrompng('/path/to/your/watermark.png');
$imagesrc = '/path/to/image/' . $_GET['src'];
$type = mime_content_type($imagesrc);
  case 'image/jpeg':
    $image = imagecreatefromjpeg($imagesrc);
  case 'image/png':
    $image = imagecreatefrompng($imagesrc);
  case 'image/gif':
    $image = imagecreatefromgif($imagesrc);
    $image = null;
  # set dimensions
  $image_width = imagesx($image);
  $image_height = imagesy($image);
  imagealphablending($watermark, TRUE);

  # create the tile and overlay

  # output image
  header('content-type: image/jpeg');

  #clean up


3) Add rewrite rules in .htaccess.  Notice the $_GET[‘src’] in the script above; we need to hand this variable to the script.  In this example, anything in my gallery folder, or with the filename starting with wmark- will be passed to the script:

RewriteRule ^(.*)wp-content/uploads/wmark-(.*) $1path/to/watermark.php?src=wp-content/uploads/wmark-$2
RewriteRule ^(.*)wp-content/uploads/image-gallery/main_gallery/(.*) $1path/to/watermark.php?src=wp-content/uploads/image-gallery/main_gallery/$2

# If you want to exclude a particular image (excludeme.jpg), add this ABOVE the other rules.  The [L] tells it to ignore other rules
RewriteRule ^wp-content/uploads/excludeme\.jpg$ - [L]

The script will only handle jpg/gif/png file types; if it’s possible that your rewrite rules will pass any other file types to the script, make sure to use (.*\.(gif|jpg|jpeg|png)) if it’s possible instead of just the basic (.*) in your rewrite rules.

WordPress Google Calendar Events plugin notes

I’ve just installed this awesome plugin onto a WP blog:

Had two issues I want to note:

1) Use the XML feed from Google, not the iCAL feed, or it will give errors

2) The timezone wasn’t working properly using default settings (it uses WP’s time); I realised this problem actually lies with WP – by default WP was using UTC+0 for my timezone (London), which is wrong, as now we’re on DST, which is UTC+1, changed it to London rather than the UTC offset and it’s working fine.
(Suprised I haven’t noticed this before, although I can’t actually think of any WP sites I’ve made where the post time is displayed – except this one at the moment, as I haven’t modified it at all yet – so it’s never been a problem.)