phpimage-manipulationwatermark

Repeating watermark php


I am using Intervention Image image manipulation library this library in a project and I am stuck on adding watermark image all over the source image.

Is it possible to repeat the watermark image on all over the source image like in the following example?

stock-photo-79576145-old-room

I try the following code, but it doesn't work for me.

$thumbnail = $manager->make($name);
$watermark = $manager->make($watermarkSource);
$x = 0;

while ($x < $thumbnail->width()) {
    $y = 0;

    while($y < $thumbnail->height()) {
        $thumbnail->insert($watermarkSource, 'top-left', $x, $y);
        $y += $watermark->height();
    }

    $x += $watermark->width();
}

$thumbnail->save($name, 80);

Solution

  • I've just solved this problem via Intervention Image library using it in Laravel Framework. So here's code snippet.

    public function watermarkPhoto(String $originalFilePath,String $filePath2Save ){
    
        $watermark_path='photos/watermark.png';
        if(\File::exists($watermark_path)){
    
            $watermarkImg=Image::make($watermark_path);
            $img=Image::make($originalFilePath);
            $wmarkWidth=$watermarkImg->width();
            $wmarkHeight=$watermarkImg->height();
    
            $imgWidth=$img->width();
            $imgHeight=$img->height();
    
            $x=0;
            $y=0;
            while($y<=$imgHeight){
                $img->insert($watermark_path,'top-left',$x,$y);
                $x+=$wmarkWidth;
                if($x>=$imgWidth){
                    $x=0;
                    $y+=$wmarkHeight;
                }
            }
            $img->save($filePath2Save);
    
            $watermarkImg->destroy();
            $img->destroy(); //  to free memory in case you have a lot of images to be processed
        }
        return $filePath2Save;
    }
    

    If you use PHP version prior to 7 remove String type declaration from function arguments. just make it

        public function watermarkPhoto($originalFilePath, $filePath2Save ){....}
    

    Also if you are not using Laravel Framework and you don't have File class included just remove redundand check from function.

          if(\File::exists($watermark_path))
    

    So the simplest framework-agnostic function would be:

    function watermarkPhoto($originalFilePath, $filePath2Save ){
    
            $watermark_path='photos/watermark.png';
            $watermarkImg=Image::make($watermark_path);
            $img=Image::make($originalFilePath);
            $wmarkWidth=$watermarkImg->width();
            $wmarkHeight=$watermarkImg->height();
    
            $imgWidth=$img->width();
            $imgHeight=$img->height();
    
            $x=0;
            $y=0;
            while($y<=$imgHeight){
                $img->insert($watermark_path,'top-left',$x,$y);
                $x+=$wmarkWidth;
                if($x>=$imgWidth){
                    $x=0;
                    $y+=$wmarkHeight;
                }
            }
            $img->save($filePath2Save);
    
            $watermarkImg->destroy();
            $img->destroy();
    
        return $filePath2Save;
    }
    

    Also you need watermark image in png format with transparent background.