Laravel Image Intervention resize quality loss

13,384

Solution 1

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.

Solution 2

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

Share:
13,384
kipzes
Author by

kipzes

Passionate about Laravel, Magento and E-commerce.

Updated on June 18, 2022

Comments

  • kipzes
    kipzes almost 2 years

    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?