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


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一下入参的尺寸

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值