Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why png size doesn't change after using http gzip compression

I use following .htaccess to set gzip compression:

AddOutputFilterByType DEFLATE text/html image/png image/jpeg text/css text/javascript

Please check this url: http://www.coinex.com/cn/silver_panda/proof/china_1984_27_gram_silver_panda_coin/

the gzip compression works for html, css, js and jpg, but not working for png (really amazing..)

like image 860
Mark Ma Avatar asked Jul 02 '12 07:07

Mark Ma


People also ask

Does PNG allow compression?

Portable Network Graphics (PNG, officially pronounced /pɪŋ/ PING, colloquially pronounced /ˌpiːɛnˈdʒiː/ PEE-en-JEE) is a raster-graphics file format that supports lossless data compression.

Can gzip increase size?

Yes, of cause. Basicly any compression scheme rely on fact that data is redundant so can be reduced through use of specific encoding schemes.

How much does gzip reduce file size?

Gzip, the most popular compression method, is used by web servers and browsers to seamlessly compress and decompress content as it's transmitted over the Internet. Used mostly on code and text files, gzip can reduce the size of JavaScript, CSS, and HTML files by up to 90%.

Does PNG lose quality when compressed?

The biggest advantage of PNG over JPEG is that the compression is lossless, meaning there is no loss in quality each time it is opened and saved again. PNG also handles detailed, high-contrast images well.


4 Answers

PNG is already a compressed data format. Compressing it with GZIP is not likely to decrease the size, and can in fact make it larger.

I'm surprised you're seeing benefits when GZIP-ing JPGs, as they are also compressed.

See here for Google's tips on using GZIP. They recommend not applying it to images.

like image 139
Rob Trickey Avatar answered Sep 21 '22 18:09

Rob Trickey


The PNG image format already uses deflate compression internally. So you will not usually see any appreciable decrease in transmitted size by using HTTP compression on top of that. Therefore you should remove image/png from the list you mentioned to avoid wasting CPU cycles at the server and client on a redundant compression step.

like image 30
tialaramex Avatar answered Sep 21 '22 18:09

tialaramex


If you want to make your PNGs smaller use https://tinypng.com/ or other png optimizer. Yes, it fully supports alpha channel too.

like image 29
Pawel Avatar answered Sep 18 '22 18:09

Pawel


PNG is a lossless image compression format. Basically it uses spatial compression to fully preserve the original image quality. It cannot be compressed further without loss of quality (you would need to use another lossless format to see if it works better).

There is no need to use GZIP (or equivalent) as it will just add processing for the decompression of images client side.

For JPEG the best you can do is make sure you use the correct resolution and quality settings for your purpose. GZIP produces mix results at best. Make sure you strip all metadata from it (unless you need those info client side but you would be better off holding those data in a database).

like image 37
Arnaud Leyder Avatar answered Sep 17 '22 18:09

Arnaud Leyder