PHP语言实现图片压缩同时保持清晰度
小标 2018-07-26 来源 : 阅读 1839 评论 0

摘要:本文主要向大家介绍了PHP语言实现图片压缩同时保持清晰度,通过具体的内容向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言实现图片压缩同时保持清晰度,通过具体的内容向大家展示,希望对大家学习php语言有所帮助。

上传图片会占用很多空间,影响体验效果,很头大,最近找到一篇文章,实现了图片压缩,做个总结:

调用代码:

$source =  'test.jpg';  

$dst_img = 'test_111.jpg';  

$percent = 1;  #原图压缩,不缩放,但体积大大降低  

$image = (new imgcompress($source,$percent))->compressImg($dst_img);

相关类文件如下,保存为imgcompress.class.php

    <?php  

      

    /**

     * 图片压缩类:通过缩放来压缩。

    * 如果要保持源图比例,把参数$percent保持为1即可。

    * 即使原比例压缩,也可大幅度缩小。数码相机4M图片。也可以缩为700KB左右。如果缩小比例,则体积会更小。

     *   

     * 结果:可保存、可直接显示。

     */  

    class imgcompress{  

           private $src;  

           private $image;  

           private $imageinfo;  

           private $percent = 0.5;  

      

           /**

            * 图片压缩

            * @param $src 源图

            * @param float $percent  压缩比例

            */  

           public function __construct($src, $percent=1)  

           {  

                  $this->src = $src;  

                  $this->percent = $percent;  

           }  

      

           /** 高清压缩图片

            * @param string $saveName  提供图片名(可不带扩展名,用源图扩展名)用于保存。或不提供文件名直接显示

            */  

           public function compressImg($saveName='')  

           {  

                  $this->_openImage();  

                  if(!emptyempty($saveName)) $this->_saveImage($saveName);  //保存  

                  else $this->_showImage();  

           }  

      

           /**

            * 内部:打开图片

            */  

           private function _openImage()  

           {  

                  list($width, $height, $type, $attr) = getimagesize($this->src);  

                  $this->imageinfo = array(  

                         'width'=>$width,  

                         'height'=>$height,  

                         'type'=>image_type_to_extension($type,false),  

                         'attr'=>$attr  

                  );  

                  $fun = "imagecreatefrom".$this->imageinfo['type'];  

                  $this->image = $fun($this->src);  

                  $this->_thumpImage();  

           }  

           /**

            * 内部:操作图片

            */  

           private function _thumpImage()  

           {  

                  $new_width = $this->imageinfo['width'] * $this->percent;  

                  $new_height = $this->imageinfo['height'] * $this->percent;  

                  $image_thump = imagecreatetruecolor($new_width,$new_height);  

                  //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度  

                  imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);  

                  imagedestroy($this->image);  

                  $this->image = $image_thump;  

           }  

           /**

            * 输出图片:保存图片则用saveImage()

            */  

           private function _showImage()  

           {  

                  header('Content-Type: image/'.$this->imageinfo['type']);  

                  $funcs = "image".$this->imageinfo['type'];  

                  $funcs($this->image);  

           }  

           /**

            * 保存图片到硬盘:

            * @param  string $dstImgName  1、可指定字符串不带后缀的名称,使用源图扩展名 。2、直接指定目标图片名带扩展名。

            */  

           private function _saveImage($dstImgName)  

           {  

                  if(emptyempty($dstImgName)) return false;  

                  $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];   //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名  

                  $dstExt =  strrchr($dstImgName ,".");  

                  $sourseExt = strrchr($this->src ,".");  

                  if(!emptyempty($dstExt)) $dstExt =strtolower($dstExt);  

                  if(!emptyempty($sourseExt)) $sourseExt =strtolower($sourseExt);  

      

                  //有指定目标名扩展名  

                  if(!emptyempty($dstExt) && in_array($dstExt,$allowImgs)){  

                         $dstName = $dstImgName;  

                  }elseif(!emptyempty($sourseExt) && in_array($sourseExt,$allowImgs)){  

                         $dstName = $dstImgName.$sourseExt;  

                  }else{  

                         $dstName = $dstImgName.$this->imageinfo['type'];  

                  }  

                  $funcs = "image".$this->imageinfo['type'];  

                  $funcs($this->image,$dstName);  

           }  

      

           /**

            * 销毁图片

            */  

           public function __destruct(){  

                  imagedestroy($this->image);  

           }  

    }  

注意:
当上传图片过大时,会报错误,Allowed memory size内存不足,报错提醒是imagecreatetruecolor()函数,一开始一直纠结在php.ini 中memory_limit 的设置,memory_limit 设置很大,还是会报错,被坑了好久,最后把_thumpImage中imagecreatetruecolor()都注释之后好了,

,这个_thumpImage方法,主要是根据缩放比例缩放图片,不影响图片压缩
郁闷的是,我在本地环境运行没有报这个错误,查看phpinfo,gd库信息不一样,可能是服务器gd库不支持函数imagecreatetruecolor(),具体原因也不是很清楚


以上就介绍了PHP的相关知识,希望对PHP有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言PHP频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 2 不喜欢 | 1
看完这篇文章有何感觉?已经有3人表态,67%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程