zoukankan      html  css  js  c++  java
  • php分10个不同等级压缩优化图片

    今天找到一个php写的压缩图片程序,可以分10个等级(0-9)来压缩,0等级时压缩比率不是很大,图片不会失真;随着压缩等级不断增大,图片会变得越来越不清晰,通常压缩后图片大小可以减少到原来的50%,压缩比还是挺大的。

    如下是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等级的压缩就是一点压缩都没有,所以它的体积会大于原始图片。我感觉此话不无道理。

  • 相关阅读:
    JavaScript数组
    JavaScript正则表达式之分组匹配 / 反向引用
    JavaScript函数之作用域 / 作用链域 / 预解析
    JavaScript正则表达式实例汇总
    python+unittets框架
    locust简介
    seleniumselenium各种技巧,滑动,提示框,窗口大小,iframe切换等等
    selenium 定位元素的方式
    jmeter环境配置,配置中文环境,原理-1
    appium环境搭建+真机+获取package和activity来验证是否搭建成功+appium自带的定位元素
  • 原文地址:https://www.cnblogs.com/tdalcn/p/5899391.html
Copyright © 2011-2022 走看看