Laravel 图片处理库 Image Intervention 调整大小会丢失质量

7
在我的Laravel web应用中,我使用Intervention Image库。我保存了上传的图像的三个版本:'original''500_auto'和一个自定义大小的图像。
$image = Image::make(Input::file('file');

// Save the orignal image
$image->save($folder . 'original.' . $extension);

// Save 500_auto image
$image->resize(500, null, function($constraint) {
    $constraint->aspectRatio();
});
$image->save($folder . '500_auto.' . $extension, 100);

// Check if size is set
if (isset($config->images->width) && isset($config->images->height)) {
    // Assign values
    $width  = $config->images->width;
    $height = $config->images->height;
    // Create the custom thumb
    $image->resize($width, $height, function($constraint) {
        $constraint->aspectRatio();
    });
    $image->save($folder . $width . '_' . $height . '.' . $extension, 100);
}

Intervention 的驱动程序在配置中设置为 'gd'
'driver' => 'gd'

这是我正在上传的图片:original.jpg

Original image

这是使用配置设置精确匹配原始尺寸(1800 x 586)的自定义缩略图结果:1800_586.jpg

Resized image

如您所见,第二张图像在调整大小后存在大量的质量损失。我该如何解决这个问题?

2个回答

15

您首先将图像调整为小格式,然后将小图像调整为原始大小。如果您反转顺序,您将从原始大小 -> 原始大小 -> 小尺寸。

个人而言,我通常更喜欢重新执行每个新图像的Image::make()调用,以确保我不会在此过程中搞砸。


感谢您的帮助。非常合乎逻辑,它先调整大小到小版本,然后再调整大小到1800x586。我按照您提到的方法更新了该方法,每次调整大小之前都会重新调用Image::make()。 - kipzes
为每个调整大小创建新的Image :: make()是一个好方法。这就是导致调整大小函数中图像模糊的原因。 - Jay Pandya

10
您可以使用"backup()"方法保存对象的状态,使用"reset()"方法返回到备份状态:
// create an image
$img = Image::make('public/foo.jpg');

// backup status
$img->backup();

// perform some modifications
$img->resize(320, 240);
$img->invert();
$img->save('public/small.jpg');

// reset image (return to backup state)
$img->reset();

// perform other modifications
$img->resize(640, 480);
$img->invert();
$img->save('public/large.jpg');

此页面有更多信息: http://image.intervention.io/api/reset


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