3

I'm trying to add watermark to all the images in a directory, let's say www.example.com/private. Some of these images have massive resolutions, while others are relatively normal so at the moment my watermark is working fine for the smaller images. Even by centering the watermark, I'm still leaving desirable sections of the bigger images vulnerable to cropping.

So my question is how would I go about writing a php script to repeat the watermark throughout the image, both vertically and horizontally? I don't really know enough about back-end development except that I know it's required to provide adequate watermarking protection, so I've been looking around on google and could only find this http://www.regardadesign.co.uk/blog/post/php-image-manipulation/15, which doesn't work.

So far I've placed the following .htaccess file into the /private directory:

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(gif|jpeg|jpg|png)$ /admin/watermark.php [QSA,NC]
</ifModule>"

And this is the script in watermark.php file:

<?php
ini_set('memory_limit','200M');
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$image = imagecreatefromstring(file_get_contents($path));
$w = imagesx($image);
$h = imagesy($image);
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
exit();
?>
TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
Ben
  • 315
  • 4
  • 19

1 Answers1

5

This is happening because you are inserting the watermark only once. If you repeat the watermark along the image area it will do the trick.

Replace your imagecopy line for this:

$img_paste_x = 0;
while($img_paste_x < $w){
    $img_paste_y = 0;
    while($img_paste_y < $h){
        imagecopy($image, $watermark, $img_paste_x, $img_paste_y, 0, 0, $ww, $wh);
        $img_paste_y += $wh;
    }
    $img_paste_x += $ww;
}
v42
  • 1,415
  • 14
  • 23
  • No, unfortunately this didn't work, it's still only place one copy of the watermark in the center and it's now just a solid black rectangle instead of my watermark image. – Ben Sep 11 '11 at 16:49
  • Yes this does repeat it and it's not a black rectangle anymore either. Although this is what I asked for, and you've scripted it approriately, on some images the watermark isn't showing, or at least isn't showing the whole watermark. Is there a way to get the center of the repeating watermark in the center of the image, or is that far too technical and impossible? – Ben Sep 11 '11 at 18:34
  • After you last updated your answer, the watermark was repeating without any space in between each copy of the watermark, so I increased the canvas size around the watermark with empty space to provide padding. They now appear fine on the larger images but it's the smaller (normal sized) images that only show half, or even less of the watermark. - I'm not sure of the best way to get around that problem. Could the padding be rendered by the script instead? Or have you any other ideas? – Ben Sep 12 '11 at 09:42
  • if the watermark is bigger than the image then resize the watermark to fit in your image using `imagecopyresized`. http://www.php.net/manual/en/function.imagecopyresized.php – v42 Sep 12 '11 at 13:08