Search code examples
phplaravellaravel-5watermarkintervention

How to add watermark image in laravel 5.2 in intervention package?


I am using laravel 5.2 framework and i am using intervention package of laravel that runs fine for me. Now here i face one problem i don't know what i amd doing wrong. Please help:-

$myimage = Image::make(storage_path('app/images/test1.jpg'));
//Suppose $imyimage width is 3024 and height is 2016
$actualwidth = 3024;
$actualheight = 2016;

Now, when i tried these sizes 3024 * 2016 pixel then watermark is not visible while when i zoom the image then it is visible Now suppose i have width and height 1600*1027 pixel it shows me right in center without zooming I want want watermark in center in 3024*2016 pixel or any pixel with zooming the image.

$watermarkHeight =  Image::make(storage_path('watermark.png'))->height();
$watermarkWidth =  Image::make(storage_path('watermark.png'))->width();
$x = ($actualwidth - $watermarkWidth) / 2;
$y = ($actualheight - $watermarkHeight) / 2;
$img = Image::make(storage_path('app/images/test1.jpg'));
$img->insert(storage_path('watermark.png'), 'center',round($x),round($y));
$img->resize($actualwidth,$actualheight)->save(storage_path('app/images/watermark-test.jpg'));

Please Help me what i am doing wrong . Thanks in advance :)


Solution

  • If I understood right your question, here is solution (not tested)

    $watermark =  Image::make(storage_path('watermark.png'));
    $img = Image::make(storage_path('app/images/test1.jpg'));
    //#1
    $watermarkSize = $img->width() - 20; //size of the image minus 20 margins
    //#2
    $watermarkSize = $img->width() / 2; //half of the image size
    //#3
    $resizePercentage = 70;//70% less then an actual image (play with this value)
    $watermarkSize = round($img->width() * ((100 - $resizePercentage) / 100), 2); //watermark will be $resizePercentage less then the actual width of the image
    
    // resize watermark width keep height auto
    $watermark->resize($watermarkSize, null, function ($constraint) {
        $constraint->aspectRatio();
    });
    //insert resized watermark to image center aligned
    $img->insert($watermark, 'center');
    //save new image
    $img->save(storage_path('app/images/watermark-test.jpg'));