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

Image compression not working on some png images. #29

Closed
avdheshb opened this issue Sep 16, 2015 · 9 comments
Closed

Image compression not working on some png images. #29

avdheshb opened this issue Sep 16, 2015 · 9 comments

Comments

@avdheshb
Copy link

I'm trying to compress some images using below code. But instead of getting compressed it's size gets increased:-

move_uploaded_file($img['tmp_name'],$path.$img_name);
$imageToProcess = $path.$img_name;
$imagick = new Imagick();
$imagick->readImage($imageToProcess);
$imagick->stripImage();
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
$imagick->setImageCompressionQuality(70);
$imagick->thumbnailImage($width, $height);
$imagick->writeImage($imageToProcess);
$imagick->destroy();

This is the original image 246.3kB:-

mobile-review-vte-1 -lao-cooking_tripadvisor_reviews_5

And this is the image after running the code above( 314.2 kB ):-
mobile-review-vte-1 -lao-cooking_tripadvisor_reviews_5_tripadvisor_reviews_5-compressed

However when i convert original image to jpg format using gimp and then run the code above then the results are as expected.

Below is some phpinfo():-

PHP Version 5.4.42

System : Linux server 2.6.32-504.12.2.el6.x86_64 #1 SMP Wed Mar 11 22:03:14 UTC 2015 x86_64

imagick module version : 3.1.2

ImageMagick version : 6.7.2-7 2015-07-23 Q16

Thanks,
Avdhesh

@glennrp
Copy link
Contributor

glennrp commented Sep 16, 2015

You specified compression quality 70 and that's what you get. Try 75. The second digit of the quality means the PNG "filter method". Zero means no filtering, so for this image the compression is less effective.

@avdheshb
Copy link
Author

I tried 75 and this time the size went from 246.3 to 246.5 kB. So still there is no compression. Below is the newly compressed image using 75 as quality setting.

75

@glennrp
Copy link
Contributor

glennrp commented Sep 17, 2015

The uncompressed image is 3.4Mb so reducing it to 246.5kb isn't too bad for lossless compression. I ran your image through pngcrush and that was only able to reduce it by about 3kb, to 243.6kb. If you need a smaller file you'll have to do lossy compression instead.

@avdheshb
Copy link
Author

So does that mean changing setImageCompressionQuality parameter will have none or negligible effect on PNG images. Also its perfectly okay if instead of expecting reduction you get increase in file sizes on reducing this image quality parameter ?

@glennrp
Copy link
Contributor

glennrp commented Sep 17, 2015

It doesn't mean that exactly. It means that if you set the image compression quality to something less effective than whatever was used to compress your original image, the file size might increase. It is perfectly OK for ImageMagick to obey your "-quality" setting, even if it results in a larger file. The default behavior, if you omit setting quality, is pretty good but not optimum. If you are concerned about squeezing out the last few bytes possible, use a third-party optimizer such as my "pngcrush" or any of a number of others that you can find on the net.

@avdheshb
Copy link
Author

In that case the image quality parameter is misleading or maybe not properly documented. Because i'm sure most of us assume that the quality setting is in reference to the already compressed image being fed and not the original uncompressed image.

Anyways thanks for your time and valuable information you've shared.

Also is there a source where I can read what the first and second parameters of this quality parameter mean for different image formats and what best combination compression algo(for example Imagick::COMPRESSION_ZIP) and quality setting i can use which works best for all image types for display on web ?

@glennrp
Copy link
Contributor

glennrp commented Sep 17, 2015

It's explained in http://imagemagick.org/script/command-line-options.php#quality
which you can find by going to imagemagick.org, then navigating to "options" then "quality".

Just for PNG/MNG, it's also explained, as emcconville mentioned, in coders/png.c

The quality is in reference to the image being written.
Experiment with pngcrush to learn more, for example

pngcrush -nobail -brute logo.png logo_brute.png

Glenn

@Danack
Copy link
Contributor

Danack commented Sep 25, 2015

Hi @glennrp,

It looks like the value that the PNG compression reads was changed from wand->image_info->quality to wand->images->quality in this commit

btw if you want to, you could tell PHP users to send issues at https://github.com/mkoppanen/imagick which is where the Imagick extension is maintained.

@avdheshb you'll need to use the setCompressionQuality function rather than the setImageCompressionQuality function if you're using a version of ImageMagick where the change above is applied.

Below is a simple brute force routine to test it's actually working.

$imagick = new Imagick($inputFilename);

for ($compression = 0; $compression <= 9; $compression++) {
    echo "Compression $compression \n";
    for ($filter = 0; $filter <= 9; $filter++) {
        echo "Filter $filter";
        $output = clone $imagick;
        $output->setImageFormat('png');
        $compressionType = intval($compression . $filter);

        //Use this for ImageMagick releases after 6.8.7-5
        $output->setCompressionQuality($compressionType);

        //Use this for ImageMagick releases before 6.8.7-5 
        //$output->setImageCompressionQuality($compressionType);

        $outputName = "./output_"."$compression$filter.png";
        $output->writeImage($outputName);
    }
    echo "\n";
}

@LIXiangChen
Copy link

setImageCompressionQuality() is not work at all, length of the generated files are totally same, only a few bytes are different.

But I found an effective way, that is use setOption('png:compression-level', 9), the value range is 0-9.

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

No branches or pull requests

5 participants