Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File size increases almost 300% #632

Open
gvinson opened this issue Nov 1, 2016 · 11 comments
Open

File size increases almost 300% #632

gvinson opened this issue Nov 1, 2016 · 11 comments

Comments

@gvinson
Copy link

gvinson commented Nov 1, 2016

I have been running into the issue where if a user uploads an jpeg and we store it via Image::make(img-data) and then $image->save(....); The file size increases almost 300%. For example, I uploaded a 323mb file and it uploaded as a 900mb file.

Does saving it out at the default 90% quality increase file size even if the image was optimized before being uploaded?

@sigfriedseldeslachts
Copy link

Pictures of code?

Greetings

@nicolas-t
Copy link

Hi,

With Glide : https://github.com/thephpleague/glide
when I run :

$server->outputImage($path, []);

My image size too increases around 200% or 300%.

Glide related code is here :
https://github.com/thephpleague/glide/blob/master/src/Server.php#L461

An example image :
https://68.media.tumblr.com/80f9f86a4db07102a44851b46aa01788/tumblr_opajjcPvjs1vwae5go1_1280.jpg

@stuartjnelson
Copy link

I'm running into the same issue where my images filesize is being increased by 300% when using $image->save(....); - does anyone know whats going on?

@icyz
Copy link

icyz commented Sep 15, 2017

I have the same issue:

public static function resizeWithFallback($img, $width, $height, $new_name = false, $quality = 90){

    $new_name = (!empty($new_name)) ? $new_name : $img;

    $imgHandler = Image::make($img);
    $imgHandler->orientate();

    $old = array(
        'filesize'  => $imgHandler->filesize(),
        'width'     => $imgHandler->width(),
        'height'    => $imgHandler->height()
    );

    $imgHandler->widen($width, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    })->heighten($height, function ($constraint) {
        $constraint->aspectRatio();
        $constraint->upsize();
    });

    $imgHandler->save($new_name, $quality);

    $status = array(
        'status' => true,
        'filename' => $new_name
    );


    if(file_exists($new_name) && $imgHandler->filesize() >= $old['filesize'] && ($old['width'] == $imgHandler->width() && $old['height'] == $imgHandler->height())){
        if($new_name != $img) {
            unlink($new_name);
        }

        $status = array(
            'status' => false,
            'filename' => $img,
            'debug' => array(
                'old' => $old,
                'new' => array(
                    'filesize'  => $imgHandler->filesize(),
                    'width'     => $imgHandler->width(),
                    'height'    => $imgHandler->height()
                )
            )
        );
    }

    return $status;

}

I'm trying this code, what do you think about?
basically it check filesize, width and height and if new image is not good it remove and return the old one

@vekien
Copy link

vekien commented Dec 22, 2017

This is still an issue, it is not handling the file as I would expect... Here is some example code and steps to repeat (really.. save any image with 100 quality)

  1. Grab any image
  2. Run script below
<?php

$fileopen = __DIR__.'/testimage.jpg';
$filesave = __DIR__.'/testimage_saved.jpg';


// report file size
clearstatcache();
$filesize = filesize($fileopen);
echo "Current file size: {$filesize}\n";


// load image and save it immediately
require 'vendor/autoload.php';
use Intervention\Image\ImageManager;
$manager = new ImageManager(array('driver' => 'gd'));
$image = $manager->make($fileopen);
$image->save($filesave, 100);


// report file size
clearstatcache();
$newsize = filesize($filesave);
echo "Current file size: {$newsize}\n";


// report increase
$increase = round(($newsize / $filesize) * 100, 5);
echo "Increase = {$increase}%\n";

Output:

Current file size: 1138320
Current file size: 2960342
Increase = 260.06237%

screen shot 2017-12-22 at 14 54 55

  • Expected: File size to be the same, or a very very small difference
  • Result: File size increases dramatically

Not sure on cause, maybe file system? (I'm on a mac), maybe GD library? Who knows.

What this means is that if you do: $image->save($filesave, 80); it will be "80% quality of whatever quality intervention is currently at and not 80% quality of the original?

Output with 80%

Current file size: 1138320
Current file size: 1261386
Increase = 110.8112%

I understand there will be some processing added and the algorithm Intervention uses, however 260% is a bit crazy. I don't think the original MS paint was that bad :D

@gerzenstl
Copy link

If you are you using GD, it may be caused by what is described by this answer.

A solution could be switching to ImageMagick.

@dipenparmar12
Copy link

Having the same issue.
but after using the below method almost done my problem.

$file = Image::make($file)->resize(800, null, function ($constraint) {$constraint->aspectRatio();});

@chillbits-legacy
Copy link

I have the same problem. I solve it by using quality 60 - 80.

@oleksandr-shubin
Copy link

Jan 9, 2023. The issue still exists. Driver: Imagick. It is enough to save at 100% quality to reproduce. Any suggestions?

@rizwanmcs
Copy link

I have been running into the issue where if a user uploads an jpeg and we store it via Image::make(img-data) and then $image->save(....); The file size increases almost 300%. For example, I uploaded a 323mb file and it uploaded as a 900mb file.

Does saving it out at the default 90% quality increase file size even if the image was optimized before being uploaded?

Facing same issue but not suitable solution founded

@siarheipashkevich
Copy link

The same issue (

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests