Laravel图像干预调整了质量损失

kip*_*zes 5 image image-resizing laravel intervention

在我的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);
}
Run Code Online (Sandbox Code Playgroud)

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

'driver' => 'gd'
Run Code Online (Sandbox Code Playgroud)

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

原始图像

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

调整大小的图像

如您所见,第二张图像在调整大小的图像中存在大量质量损失.我怎样才能解决这个问题?

use*_*568 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');
Run Code Online (Sandbox Code Playgroud)

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


Joe*_*inz 9

您首先要将图像调整为小尺寸,然后再拍摄小图像并将其重新调整为原始尺寸.如果您颠倒订单,您将改为原始尺寸 - >原始尺寸 - >小尺寸.

就个人而言,我通常更喜欢重新Image::make()调用每个新图像的调用,只是为了确保我不会在此过程中将这样的东西搞砸.