image icon indicating copy to clipboard operation
image copied to clipboard

File size increases almost 300%

Open gvinson opened this issue 8 years ago • 11 comments

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?

gvinson avatar Nov 01 '16 15:11 gvinson

Pictures of code?

Greetings

sigfriedseldeslachts avatar Dec 23 '16 12:12 sigfriedseldeslachts

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

nicolas-t avatar May 02 '17 11:05 nicolas-t

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?

stuartjnelson avatar May 31 '17 09:05 stuartjnelson

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

icyz avatar Sep 15 '17 07:09 icyz

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

vekien avatar Dec 22 '17 15:12 vekien

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

A solution could be switching to ImageMagick.

gerzenstl avatar Jul 19 '18 20:07 gerzenstl

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();});

dipenparmar12 avatar Jan 25 '20 22:01 dipenparmar12

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

chillbits-legacy avatar Jan 04 '21 04:01 chillbits-legacy

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

oleksandr-shubin avatar Jan 09 '23 16:01 oleksandr-shubin

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

rizwanmcs avatar Jan 22 '24 12:01 rizwanmcs

The same issue (

siarheipashkevich avatar Aug 11 '24 14:08 siarheipashkevich