Laravel Image Intervention 调整大小质量损失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34353344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:49:42  来源:igfitidea点击:

Laravel Image Intervention resize quality loss

imagelaravelimage-resizingintervention

提问by kipzes

In my Laravel web-application I make use of the Intervention Image library. I'm saving three versions of the uploaded image: 'original', '500_auto'and a custom size image.

在我的 Laravel 网络应用程序中,我使用了干预图像库。我保存上传图像的三个版本:'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);
}

The driver of Intervention is set in the config as 'gd':

干预的驱动程序在配置中设置为'gd'

'driver' => 'gd'

This is the image I'm uploading: original.jpg

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

Original image

原图

And this is the result from the custom thumb with the config settings set to exact the original sizes (1800 x 586): 1800_586.jpg

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

Resized image

调整大小的图像

As you can see the second image there is a lot of quality loss in the resized image. How can I fix this?

正如您所看到的第二张图片,调整后的图片有很多质量损失。我怎样才能解决这个问题?

回答by Joel Hinz

You're first resizing the image to a small format, then you take the small image and resize that to the original size again. If you reverse the order, you'll go from original size -> original size -> small size instead.

您首先将图像大小调整为小格式,然后拍摄小图像并再次将其调整为原始大小。如果您颠倒顺序,您将从原始尺寸 -> 原始尺寸 -> 小尺寸改为。

Personally, I usually prefer to redo the Image::make()call for every new image, just to make sure I don't screw something like this up along the way.

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

回答by user2138568

You can use "backup()" method to save the object's status and "reset()" method to return back to backup state:

您可以使用“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');

more info on this page: http://image.intervention.io/api/reset

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