如何在干预包中添加laravel 5.2中的水印图像?

时间:2017-06-09 06:59:51

标签: php laravel laravel-5.2 watermark intervention

我正在使用laravel 5.2框架,我正在使用laravel的干预包对我运行正常。现在我在这里遇到一个问题,我不知道我做错了什么。请帮忙: -

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

现在,当我尝试这些尺寸3024 * 2016像素时,水印是不可见的,而当我缩放图像然后它是可见的 现在假设我有宽度和高度1600 * 1027像素,它显示我在中心没有缩放 我希望在3024 * 2016像素的中心或任何像素缩放图像时想要水印。

$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'));

请帮助我,我做错了什么。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,这里是解决方案(未经测试)

$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'));