如下是php压缩图片程序
- <?php
- Header("Content-type: image/PNG");/*告诉IE浏览器你做的程序是张图片*/
- $image = @imagecreatefrompng ("banner.png");
- imagepng ($image,null,0); /*压缩等级0-9,压缩后9最小,1最大*/
- imagedestroy ($image);
- ?>
原图体积是125k,现在我想看看在不同的等级压缩下会得到什么样的结果。如下为测试记录。
imagepng($img,null,0); --> Size = 225K
imagepng($img,null,1); --> Size = 85.9K
imagepng($img,null,2); --> Size = 83.7K
imagepng($img,null,3); --> Size = 80.9K
imagepng($img,null,4); --> Size = 74.6K
imagepng($img,null,5); --> Size = 73.8K
imagepng($img,null,6); --> Size = 73K
imagepng($img,null,7); --> Size = 72.4K
imagepng($img,null,8); --> Size = 71K
imagepng($img,null,9); --> Size = 70.6K
我不明白的是为什么压缩等级为0时,它的体积反而变大了?
我们在优化的时候,一般采用2等级比较合适,图片没有失真,但体积减少了30%,达到优化的目的了。如果采用6、7、8、9等级压缩的话,图片有所失真,所以不宜采用这些等级进行压缩优化图片。我们看看老外是怎么解释这个现象的:
The result seems accurate since 0 means no compression
quality Compression level: from 0 (no compression) to 9.
It's normal for the 0ed file to be larger than the original (that can be slightly compressed to begin with). You need to understand file compression and PHP GD image constructor.
老外的解释有没有道理呢?他认为原始图片其实是经过了轻微的压缩,而0等级的压缩就是一点压缩都没有,所以它的体积会大于原始图片。我感觉此话不无道理。