This is what I put into a watermark.php
file :
<?php
// loads a png, jpeg or gif image from the given file name
function imagecreatefromfile($image_path) {
// retrieve the type of the provided image file
list($width, $height, $image_type) = getimagesize($image_path);
// select the appropriate imagecreatefrom* function based on the determined
// image type
switch ($image_type)
{
case IMAGETYPE_GIF: return imagecreatefromgif($image_path); break;
case IMAGETYPE_JPEG: return imagecreatefromjpeg($image_path); break;
case IMAGETYPE_PNG: return imagecreatefrompng($image_path); break;
default: return ''; break;
}
}
// load source image to memory
$image = imagecreatefromfile($_GET['image']);
if (!$image) die('Unable to open image');
// load watermark to memory
$watermark = imagecreatefromfile($_GET['watermark']);
if (!$image) die('Unable to open watermark');
// calculate the position of the watermark in the output image (the
// watermark shall be placed in the lower right corner)
$watermark_pos_x = imagesx($image) - imagesx($watermark) - 8;
$watermark_pos_y = imagesy($image) - imagesy($watermark) - 10;
// merge the source image and the watermark
imagecopy($image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0,
imagesx($watermark), imagesy($watermark));
// output watermarked image to browser
header('Content-Type: image/jpeg');
imagejpeg($image, '', 100); // use best image quality (100)
// remove the images from memory
imagedestroy($image);
imagedestroy($watermark);
?>
then into a .htaccess
file :
RewriteEngine on
RewriteCond %{REQUEST_URI} (800x...|...x800)\.(png|jpe?g|gif)
RewriteRule ^([^tn].*\.(gif|jpg|png))$ /wp-content/uploads/watermark.php?image=$1&watermark=watermark.png [NC]
Place the watermark.php
+ .htaccess
+ watermark.png
into wp-content/uploads
folder (or any other place if you adapt the .htaccess
line).
Here the .htaccess grabs any request for jpg, png ou gif that contains 800x or x800 in the name (to apply watermark on big images only), redirect it to the watermark.php
script who add the watermark on the image.
This is a starter that you should adapt to your needs by defining the rules to apply in the .htaccess
file. For our case, we had define 800x800 size in the media options.