Laravel Image Intervention resize quality loss
Asked Answered
D

2

7

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.

$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':

'driver' => 'gd'

This is the image I'm uploading: 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

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?

Degust answered 18/12, 2015 at 10:23 Comment(0)
V
15

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.

Varietal answered 18/12, 2015 at 10:41 Comment(2)
Thanks for your help. Very logical, it was resizing to the small version and resizing to 1800x586 after that. I updated the method by recalling the Image::make() before every resizing, just like you mentioned.Degust
Good line to create new Image::make() for every resize. That was the reason which causes the blurred image in resize function.Ejaculatory
D
10

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

// 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

Demirep answered 17/8, 2017 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.