在Yii2
和核心yii\imagine\Image
类中,当使用以下命令时:
Image::watermark('image.jpg', 'watermark.jpg')->save('image.jpg');
如果水印的维度,源图片的grower,返回这个错误:
Exception 'Imagine\Exception\OutOfBoundsException' with message 'Cannot paste image of the given size at the specified position, as it moves outside of the current image's box'
这很好。但是如何忽略这个错误,所以新的图像将被创建,但水印的一部分,即图像框外的部分被隐藏和删除。
发布于 2018-08-06 09:52:32
不能忽略使用该名称调用它们的Exceptions
,异常是从paste(ImageInterface $image, PointInterface $start);
方法抛出的
$size = $image->getSize();
if (!$this->getSize()->contains($size, $start)) {
throw new OutOfBoundsException('Cannot paste image of the given size at the specified position, as it moves outside of the current image\'s box');
}
在Image::watermark
函数中调用。它是受限制的,是不允许的,所以你不能忽略它。
您可以忽略/隐藏通知或警告,但您可以对异常执行catch
操作,并采取一些措施来修复它或向用户显示错误
try{
\yii\imagine\Image::watermark ( 'img/image.png' , 'img/logo.jpg' );
} catch (\Imagine\Exception\OutOfBoundsException $ex) {
Yii::$app->session->setFlash('error',$ex->getMessage()) ;
}
或者,如果发生异常,请调整水印图像的大小,使其小于基本图像,然后再次调用Image::watermark
。
EDIT
你可以做下面的事情,假设你有一个actionWatermark()
,你正在创建水印图像
public function actionWatermark() {
$image='image.jpg';
$watermark= 'watermark.jpg';
try {
//call watermark function
$this->watermark ($image,$watermark);
} catch ( \Imagine\Exception\OutOfBoundsException $ex ) {
$img = \yii\imagine\Image::getImagine ();
//get the base image dimensions
$size=$img->open ( $image )->getSize();
//resize the watermark image
$resized=\yii\imagine\Image::resize ( $watermark , $size->getWidth () , $size->getHeight (),TRUE);
//call again
$this->watermark ($image,$resized);
}
}
private function watermark($image,$watermark) {
\yii\imagine\Image::watermark ( $image , $watermark )->save ( 'image.jpg' );
}
https://stackoverflow.com/questions/51698580
复制相似问题