PNG Compression

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
marc_gfx

PNG Compression

Post by marc_gfx »

I am using Imagick to resize uploaded images. I have noticed that a large PNG24 I uploaded (2000*2249 pixels at 208kb) increases in size after resizing. The goal of resizing is really to reduce the load on the server :)

The new values are 1024*910 pixels at 460kb. So for a quarter of the pixels it uses more than double the amount of file space.

I compared this to compression done with photoshop6 and it reduces the image to ~152kb, so something must be going wrong. I assume there are options I have not found yet?

I have tried $im->setCompression (Imagick::COMPRESSION_ZIP) with no change in the result.
marc_gfx

Re: PNG Compression

Post by marc_gfx »

I found a solution in the forum (I thought). But it wont work probably because of this:
There seems to be a slight problem with setting image or channel depth. Using Q16, setting the depth to 8 bit, you will have a 16 bit file.
Doesn't matter with better previews / image processing programs. With Win systems the display for TIFF files is a kind of psychedelic art.
This took me hours until I did check with PhotoShop. Besides: even if it works with RGB and PNG files, this consumes resources. Have a look on file size.
Only way out: change to Q8.
IMHO there is no way to run Q8 and Q16 simultaneously.
The above said concerns also setImageDepth.
is there a disadvantage if I use Q8? or does it make more sense anyway for websites?
marc_gfx

Re: PNG Compression

Post by marc_gfx »

I will continue to answer my own questions ;) ... I have installed Q8 and the PNG file is now 130kb after resizing. I guess Imagick Q16 has a little bug in the setImageDepth function (in the version I have).
vitty

Re: PNG Compression

Post by vitty »

Hello,

I have a simmilar problem as above.

source file: 24-bits per pixel RGB image (each channel 8-bits per pixel)
I use the scale method for resizing images and then I write the image to the disk.
output file: 48-bits per pixel RGB (each channel 8-bits per pixel)

This happens for PNG files. (not JPG files)
If I use setImageDepth(8) then it doesn't change the output depth (still 16). The output PNG files are so big because 16-bit output. :-(

This looks like a bug.

Linux OS
ImageMagick version ImageMagick 6.3.8 01/30/08 Q16
imagick module version 2.0.1

Thanks for help
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: PNG Compression

Post by mkoppanen »

Can you post code and example images? I can not reproduce this issue.

I am running ImageMagick 6.4.2.
Mikko Koppanen
My blog: http://valokuva.org
vitty

Re: PNG Compression

Post by vitty »

Hi,

below is the code and I've attached test images.

Code: Select all

$im_handler = new Imagick();
$im_handler->readImage( '/var/www/html/test/png/input.png' );
$im_handler->scaleImage( 640, 512 );
$im_handler->writeImage( '/var/www/html/test/png/output.png' );
$im_handler->destroy(); 

$im_handler_2 = new Imagick();
$im_handler_2->readImage( '/var/www/html/test/png/input.png' );
$im_handler_2->scaleImage( 640, 512 );
$im_handler_2->setImageDepth( 8 );
$im_handler_2->writeImage( '/var/www/html/test/png/output_2.png' );
$im_handler_2->destroy(); 

exec("/opt/ImageMagick/bin/convert \"/var/www/html/test/png/input.png\" -scale 640x512 -depth 8 \"/var/www/html/test/png/output_3.png\"",$output);
Linux OS
ImageMagick version ImageMagick 6.3.8 01/30/08 Q16
imagick module version 2.0.1
Attachments
output no.2 - PNG image - 640x512px RGB, 48-bits per pixel (each channel 16-bits), 86kB
output no.2 - PNG image - 640x512px RGB, 48-bits per pixel (each channel 16-bits), 86kB
output_2.png (86.04 KiB) Viewed 24968 times
output no.1 - PNG image - 640x512px RGB, 48-bits per pixel (each channel 16-bits), 127kB
output no.1 - PNG image - 640x512px RGB, 48-bits per pixel (each channel 16-bits), 127kB
output.png (127.9 KiB) Viewed 24968 times
input PNG image - 1280x1024px RGB, 24-bits per pixel (each channel 8-bits), 176kB
input PNG image - 1280x1024px RGB, 24-bits per pixel (each channel 8-bits), 176kB
input.png (176.24 KiB) Viewed 24968 times
vitty

Re: PNG Compression

Post by vitty »

another two output images
Attachments
output no.4 - Photoshop output - PNG image - 640x512px RGB, 24-bits per pixel (each channel 8-bits), 59,4kB
output no.4 - Photoshop output - PNG image - 640x512px RGB, 24-bits per pixel (each channel 8-bits), 59,4kB
output_4.png (59.41 KiB) Viewed 24961 times
output no.3 - exec command - PNG image - 640x512px RGB, 24-bits per pixel (each channel 8-bits), 56,5kB
output no.3 - exec command - PNG image - 640x512px RGB, 24-bits per pixel (each channel 8-bits), 56,5kB
output_3.png (56.56 KiB) Viewed 24957 times
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: PNG Compression

Post by mkoppanen »

Hi,

unable to reproduce with ImageMagick 6.4.2. I will try with older version at some point.
Mikko Koppanen
My blog: http://valokuva.org
vitty

Re: PNG Compression

Post by vitty »

mkoppanen wrote:Hi,

unable to reproduce with ImageMagick 6.4.2. I will try with older version at some point.


Hi

it means that the problem is fixed in the newest version of imagick and image magick?
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: PNG Compression

Post by mkoppanen »

Can you try it with newer version? You can use ./configure --prefix=/opt/imagemagick_new or something similar so it won't mess your current installation.
Mikko Koppanen
My blog: http://valokuva.org
colnector

Re: PNG Compression

Post by colnector »

Optipng could be useful to optimize the resulting files anyway
Post Reply