使用Imagine在yii2中按比例调整图像大小

3

我有一个yii2的代码:

Image::thumbnail($path, 100, 100)->save($thumbnail, ['quality' => 50]);

我认为它会保持原始图像的纵横比来调整大小。

但实际上它只是创建了一个框...

可能出了什么问题?

3个回答

6
你可以像这样使用:

您可以这样使用:

use Imagine\Image\Box;

Image::frame($path)
->thumbnail(new Box(100, 100))
->save($thumbnail, ['quality' => 50]);

1
是的,那就是我刚做的!使用Imagine\Gd; 使用Imagine\Image\Box; 使用Imagine\Image\BoxInterface;...$photo = $imagine->open($path); $photo->thumbnail(new Box(1200, 1200))->save($path, ['quality' => 90]); - ps202

1
我在Yii2中这样使用,它很好地工作。它还保持了与宽度相关的完美纵横比。
use yii\imagine\Image;
use Imagine\Image\Box;

 ...

$imagine = Image::getImagine()
->open($resizeImagePath)
->thumbnail(new Box(120, 120))
->save($thumbnailImagePath, ['quality' => 90]);

0
我写了一段简单的代码来保持图像的纵横比。
use yii\imagine\Image;

.................................................................

public function doResize($imageLocation, $imageDestination, Array $options = null)
{
    $newWidth = $newHeight = 0;
    list($width, $height) = getimagesize($imageLocation);

    if(isset($options['newWidth']) || isset($options['newHeight']))
    {
        if(isset($options['newWidth']) && isset($options['newHeight']))
        {
            $newWidth = $options['newWidth'];
            $newHeight = $options['newHeight'];
        }

        else if(isset($options['newWidth']))
        {
            $deviationPercentage = (($width - $options['newWidth']) / (0.01 * $width)) / 100;

            $newWidth = $options['newWidth'];
            $newHeight = $height - ($height * $deviationPercentage);
        }

        else
        {
            $deviationPercentage = (($height - $options['newHeight']) / (0.01 * $height)) / 100;

            $newWidth = $width - ($width * $deviationPercentage);
            $newHeight = $options['newHeight'];
        }
    }

    else
    {
        // reduce image size up to 20% by default
        $reduceRatio = isset($options['reduceRatio']) ? $options['reduceRatio'] : 20;

        $newWidth = $width * ((100 - $reduceRatio) / 100);
        $newHeight = $height * ((100 - $reduceRatio) / 100);
    }

    return Image::thumbnail(
        $imageLocation, 
        (int) $newWidth, 
        (int) $newHeight
    )->save(
        $imageDestination,
        ['quality' => isset($options['quality']) ? $options['quality'] : 100]
    );
}

你可以像这样使用它:

(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'reduceRatio' => 50
]);

// or like this
(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'width' => 500,
]);

// or like this
(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'height' => 500,
]);

// and like this (but will break the aspect ratio)
(new TheClass)->doResize($imageLocation, $imageDestination, [
    'quality' => 70,
    'width' => 100
    'height' => 500,
]);

很酷,但如果你已经有盒子方法,为什么还要费心呢? - ps202
很酷,但你的例子有错误。 必须是: (new TheClass)->doResize($imageLocation, $imageDestination, [ 'quality' => 70, 'newWidth' => 100, 'newHeight' => 500, ]); - Gabriele Carbonai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接