【问题标题】:How to add watermark image in laravel 5.2 in intervention package?如何在干预包的 laravel 5.2 中添加水印图像?
【发布时间】:2017-06-09 06:59:50
【问题描述】:

我正在使用 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'));

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

【问题讨论】:

  • 您的问题对我来说不清楚,您尝试在不同宽度/高度的图像上放置水印,并且您需要根据初始图像居中并调整此水印的大小? ,round($x),round($y) 你看不到它,因为这个值太高了。
  • 是的,完全是@Froxz,我希望在调整大小的图像上显示水印图像,该图像应该是可见的并且也在中心
  • 那么解决方案是什么@Froxz,无论大小是多少,我都希望图像居中且可见

标签: php laravel laravel-5.2 watermark intervention


【解决方案1】:

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

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

【讨论】:

  • 现在每张图片都非常接近,水印非常非常放大
  • @kunal 检查更新的答案有 3 种调整水印大小的方法,使用适合您需要的一种。
  • 如何维护调整大小或上传图片的dpi,请帮忙
  • 我希望你也应该回答这个问题stackoverflow.com/questions/44456351/…
猜你喜欢
  • 2016-12-19
  • 1970-01-01
  • 2017-01-06
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多