php高清晰度无损压缩
经常会用到把上传的大图片压缩,特别是体积,在微信等APP应用上,也默认都是有压缩的,那么,怎么样对图片大幅度压缩却仍能保持较高的清晰度呢?
压缩通常是有按比例缩放,和指定宽度压缩的,效果很不错,一个数码相机拍的4M图片,压缩后保持了较高的清晰度和原图宽高值,只有700K。
下面是代码(有两个文件,imgcompress.class.php 类,及compress.php)
compress.php
1 <?php2 require_once ‘imgcompress.class.php‘;3 $source = ?‘test.png‘;//原图文件名4 $dst_img = ‘test_.png‘;//保存图片的文件名5 $percent = 1; ?#原图压缩,不缩放,但体积大大降低6 $image = (new imgcompress($source,$percent))->compressImg($dst_img);
imgcompress.class.php
?1 <?php ?3 /** ?4 ?* 图片压缩类:通过缩放来压缩。 ?5 ?* 如果要保持源图比例,把参数$percent保持为1即可。 ?6 ?* 即使原比例压缩,也可大幅度缩小。数码相机4M图片。也可以缩为700KB左右。如果缩小比例,则体积会更小。 ?7 ?* ?8 ?* 结果:可保存、可直接显示。 ?9 ?*/ 10 class imgcompress{ 11 ?12 ????private $src; 13 ????private $image; 14 ????private $imageinfo; 15 ????private $percent = 0.5; 16 ?17 ????/** 18 ?????* 图片压缩 19 ?????* @param $src 源图 20 ?????* @param float $percent ?压缩比例 21 ?????*/ 22 ????public function __construct($src, $percent=1) 23 ????{ 24 ????????$this->src = $src; 25 ????????$this->percent = $percent; 26 ????} 27 ?28 ?29 ????/** 高清压缩图片 30 ?????* @param string $saveName ?提供图片名(可不带扩展名,用源图扩展名)用于保存。或不提供文件名直接显示 31 ?????*/ 32 ????public function compressImg($saveName=‘‘) 33 ????{ 34 ????????$this->_openImage(); 35 ????????if(!empty($saveName)) $this->_saveImage($saveName); ?//保存 36 ????????else $this->_showImage(); 37 ????} 38 ?39 ????/** 40 ?????* 内部:打开图片 41 ?????*/ 42 ????private function _openImage() 43 ????{ 44 ????????list($width, $height, $type, $attr) = getimagesize($this->src); 45 ????????$this->imageinfo = array( 46 ????????????‘width‘=>$width, 47 ????????????‘height‘=>$height, 48 ????????????‘type‘=>image_type_to_extension($type,false), 49 ????????????‘attr‘=>$attr 50 ????????); 51 ????????$fun = "imagecreatefrom".$this->imageinfo[‘type‘]; 52 ????????$this->image = $fun($this->src); 53 ????????$this->_thumpImage(); 54 ????} 55 ????/** 56 ?????* 内部:操作图片 57 ?????*/ 58 ????private function _thumpImage() 59 ????{ 60 ????????$new_width = $this->imageinfo[‘width‘] * $this->percent; 61 ????????$new_height = $this->imageinfo[‘height‘] * $this->percent; 62 ????????$image_thump = imagecreatetruecolor($new_width,$new_height); 63 ????????//将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度 64 ????????imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo[‘width‘],$this->imageinfo[‘height‘]); 65 ????????imagedestroy($this->image); 66 ????????$this->image = $image_thump; 67 ????} 68 ????/** 69 ?????* 输出图片:保存图片则用saveImage() 70 ?????*/ 71 ????private function _showImage() 72 ????{ 73 ????????header(‘Content-Type: image/‘.$this->imageinfo[‘type‘]); 74 ????????$funcs = "image".$this->imageinfo[‘type‘]; 75 ????????$funcs($this->image); 76 ????} 77 ????/** 78 ?????* 保存图片到硬盘: 79 ?????* @param ?string $dstImgName ?1、可指定字符串不带后缀的名称,使用源图扩展名 。2、直接指定目标图片名带扩展名。 80 ?????*/ 81 ????private function _saveImage($dstImgName) 82 ????{ 83 ????????if(empty($dstImgName)) return false; 84 ????????$allowImgs = [‘.jpg‘, ‘.jpeg‘, ‘.png‘, ‘.bmp‘, ‘.wbmp‘,‘.gif‘]; ??//如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名 85 ????????$dstExt = ?strrchr($dstImgName ,"."); 86 ????????$sourseExt = strrchr($this->src ,"."); 87 ????????if(!empty($dstExt)) $dstExt =strtolower($dstExt); 88 ????????if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt); 89 ?90 ????????//有指定目标名扩展名 91 ????????if(!empty($dstExt) && in_array($dstExt,$allowImgs)){ 92 ????????????$dstName = $dstImgName; 93 ????????}elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)){ 94 ????????????$dstName = $dstImgName.$sourseExt; 95 ????????}else{ 96 ????????????$dstName = $dstImgName.$this->imageinfo[‘type‘]; 97 ????????} 98 ????????$funcs = "image".$this->imageinfo[‘type‘]; 99 ????????$funcs($this->image,$dstName);100 ????}101 102 ????/**103 ?????* 销毁图片104 ?????*/105 ????public function __destruct(){106 ????????imagedestroy($this->image);107 ????}108 109 }
使用之后个人感觉 $percent 设置为0.5 左右就不错了,压缩后的图片与原图质量基本一样。
原文:https://sylearning.iteye.com/blog/2368860
2018年12月7日17:41:13
php图片压缩-高清晰度
原文地址:https://www.cnblogs.com/lygz/p/10084171.html