记一次图片压缩过程的BUG处理

来源:互联网 发布:python lambda *_ 编辑:程序博客网 时间:2024/05/22 02:20

2017-12-15 19:58:59[10][-][-][error][Imagine\Exception\OutOfBoundsException] 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' in /data/vendor/imagine/imagine/lib/Imagine/Gd/Image.php:128Stack trace:#0 /data/vendor/yiisoft/yii2-imagine/BaseImage.php(175): Imagine\Gd\Image->paste(Object(Imagine\Gd\Image), Object(Imagine\Image\Point))#1 /data/modules/ImageAction.php(73): yii\imagine\BaseImage::thumbnail('/data/ru...', 453.33333333333, 800)#2 [internal function]: app\modules\ImageAction->run()#3 /data/vendor/yiisoft/yii2/base/Action.php(92): call_user_func_array(Array, Array)#4 /data/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\Action->runWithParams(Array)#5 /data/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('image', Array)#6 /data/vendor/yiisoft/yii2/web/Application.php(84): yii\base\Module->runAction('risk/audit/imag...', Array)#7 /data/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))#8 /data/web/index.php(19): yii\base\Application->run()#9 {main}2017-12-15 19:58:59[][-][-][error][application] Exception Cannot paste image of the given size at the specified position, as it moves outside of the current image's box



------------------

题主在图片尺寸处理时没有经过intval,导致在】/vendor/yiisoft/yii2-imagine/BaseImage.php中

/** * Creates a thumbnail image. The function differs from `\Imagine\Image\ImageInterface::thumbnail()` function that * it keeps the aspect ratio of the image. * @param string $filename the image file path or path alias. * @param integer $width the width in pixels to create the thumbnail * @param integer $height the height in pixels to create the thumbnail * @param string $mode * @return ImageInterface */public static function thumbnail($filename, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND){    $box = new Box($width, $height);    $img = static::getImagine()->open(Yii::getAlias($filename)); .......    if ($size->getWidth() < $width) {        $startX = ceil($width - $size->getWidth()) / 2;    }    if ($size->getHeight() < $height) {        $startY = ceil($height - $size->getHeight()) / 2;    }    $thumb->paste($img, new Point($startX, $startY));    return $thumb;}


/vendor/imagine/imagine/lib/Imagine/Gd/Image.php

    /**     * {@inheritdoc}     */    final public function 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'            );        }.....    }

/vendor/imagine/imagine/lib/Imagine/Image/Box.php

/** * {@inheritdoc} */public function contains(BoxInterface $box, PointInterface $start = null){

return   $start->in($this) &&    $this->width >= $box->getWidth() + $start->getX() &&    $this->height >= $box->getHeight() + $start->getY();
}


    if ($size->getWidth() < $width) {        $startX = ceil($width - $size->getWidth()) / 2;    }    if ($size->getHeight() < $height) {        $startY = ceil($height - $size->getHeight()) / 2;    }
此处会出现$startX=0.5;

进而,导致在

contains
方法里返回false,从而抛异常。
解决方式:直接intval一下入参的尺寸

原创粉丝点击