5

I've created a script that adds a watermark on top of an existing image using PHP. That works all good. I am able to position it on the top left, bottom left, top right, bottom right and centered. I haven't been able to figure out how to repeat the watermark if I wanted to.

I would like to do a repeating watermark like this image:

enter image description here

The code:

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    // creating a cut resource 
    $cut = imagecreatetruecolor($src_w, $src_h); 

    // copying relevant section from background to the cut resource 
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 

    // copying relevant section from watermark to the cut resource 
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

    // insert cut resource to destination image 
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
} 


$imagesource    = $image['file_path'];
$watermarkPath  = $settings['watermark'];
$filetype       = substr($imagesource,strlen($imagesource)-4,4);
$filetype       = strtolower($filetype);
$watermarkType  = substr($watermarkPath,strlen($watermarkPath)-4,4);
$watermarkType  = strtolower($watermarkType);

// Let's pretend that $watermark and $image are now GD resources.

$imagewidth         = imagesx($image);
$imageheight        = imagesy($image);  
$watermarkwidth     = imagesx($watermark);
$watermarkheight    = imagesy($watermark);

switch ($settings['watermark_location'])
{
    case "tl": //Top Left
        $startwidth     = 20;
        $startheight    = 20;
        break;
    case "bl": //Bottom Left
        $startwidth     = 20;
        $startheight    = (($imageheight - $watermarkheight) - 20);
        break;
    case "tr": //Top Right
        $startwidth     = (($imagewidth - $watermarkwidth) - 20);
        $startheight    = 20;
        break;
    case "br": //Bottom Right
        $startwidth     = (($imagewidth - $watermarkwidth) - 20);
        $startheight    = (($imageheight - $watermarkheight) - 20);
        break;
    case "middle": //Middle/center
        $startwidth     = (($imagewidth - $watermarkwidth) / 2);
        $startheight    = (($imageheight - $watermarkheight) / 2);
        break;
    case "repeat":
        // not sure what to do here
        break;
    default:
        $startwidth     = (($imagewidth - $watermarkwidth) / 2);
        $startheight    = (($imageheight - $watermarkheight) / 2);
}

imagecopymerge_alpha($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight,$settings['watermark_opacity']);
imagejpeg($image,NULL,90);
imagedestroy($image);
imagedestroy($watermark);               
Charles
  • 50,943
  • 13
  • 104
  • 142
Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • 1
    How about posting your code. Just a guess, but that might help. – Paul Dessert Feb 13 '12 at 23:12
  • We absolutely positively need to see the code you are already using so we can give you suggestions on how to make the watermark repeat. – Charles Feb 13 '12 at 23:20
  • posted a pastie..its a ton of code..just focus on the switch case about 70 lines down – Ronnie Feb 13 '12 at 23:21
  • 1
    It's not nearly as much code as you think it is. I've in-lined the relevant parts. – Charles Feb 13 '12 at 23:31
  • I found my answer here: http://stackoverflow.com/questions/7333077/how-can-i-repeat-a-watermark-throughout-a-requested-image-using-php – Ronnie Feb 13 '12 at 23:51
  • Here is some solution. This was found in this forum. http://stackoverflow.com/a/7379487/2345900 [1]: http://stackoverflow.com/a/7379487/2345900 – Sajitha Rathnayake Apr 30 '14 at 12:04
  • this link can help you https://stackoverflow.com/questions/32005595/how-do-i-to-make-watermark-image-with-php-and-gd-like-envato-photodune-preview-i – mohamad mazaheri May 07 '21 at 20:53
  • this link can help you https://stackoverflow.com/questions/32005595/how-do-i-to-make-watermark-image-with-php-and-gd-like-envato-photodune-preview-i – mohamad mazaheri May 07 '21 at 20:54

2 Answers2

2

I don't entirely know how your script works, but can't you just repeat adding watermarks at fixed intervals until you have covered the entire width of the image?

Amish Programmer
  • 2,051
  • 3
  • 19
  • 22
1

I think the imagesettile function could help:

http://php.net/manual/en/function.imagesettile.php

Look at the example on that page.

Anton Dieterle
  • 656
  • 8
  • 8