常常會用到把上傳的大圖片壓縮,特別是體積,在微信等APP應用上,也默認都是有壓縮的,那麼,怎麼樣對圖片大幅度壓縮卻仍能保持較高的清晰度呢?php
壓縮一般是有按比例縮放,和指定寬度壓縮的,效果很不錯,一個數碼相機拍的4M圖片,壓縮後保持了較高的清晰度和原圖寬高值,只有700K。微信
下面是代碼(有兩個文件,imgcompress.class.php 類,及compress.php)ui
compress.phpthis
1 <?php 2 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.phpspa
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 一、可指定字符串不帶後綴的名稱,使用源圖擴展名 。二、直接指定目標圖片名帶擴展名。 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 左右就不錯了,壓縮後的圖片與原圖質量基本同樣。code
原文:https://sylearning.iteye.com/blog/2368860blog
2018年12月7日17:41:13圖片