Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I add an image watermark to only those images which are to be published in gallery using a function in functions.php? Images can be in .jpg, .png, .gif format.

I also found this snippet which serves the same but it watermarks all the uploaded images.

share|improve this question
    
Have you tried to alter that code? If so, post what you've done. –  s_ha_dum Feb 10 '13 at 17:04
add comment

1 Answer

up vote 0 down vote accepted

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 .htaccessfile. For our case, we had define 800x800 size in the media options.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.