PHP實(shí)現(xiàn)圖片壓縮同時(shí)保持清晰度

上傳圖片會(huì)占用很多空間,影響體驗(yàn)效果,很頭大,最近找到一篇文章,實(shí)現(xiàn)了圖片壓縮,做個(gè)總結(jié):

調(diào)用代碼:

$source =  'test.jpg';  
$dst_img = 'test_111.jpg';  
$percent = 1;  #原圖壓縮,不縮放,但體積大大降低  
$image = (new imgcompress($source,$percent))->compressImg($dst_img); 

相關(guān)類(lèi)文件如下,保存為imgcompress.class.php

    <?php  
      
    /** 
     * 圖片壓縮類(lèi):通過(guò)縮放來(lái)壓縮。 
    * 如果要保持源圖比例,把參數(shù)$percent保持為1即可。 
    * 即使原比例壓縮,也可大幅度縮小。數(shù)碼相機(jī)4M圖片。也可以縮為700KB左右。如果縮小比例,則體積會(huì)更小。 
     *   
     * 結(jié)果:可保存、可直接顯示。 
     */  
    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  提供圖片名(可不帶擴(kuò)展名,用源圖擴(kuò)展名)用于保存。或不提供文件名直接顯示 
            */  
           public function compressImg($saveName='')  
           {  
                  $this->_openImage();  
                  if(!emptyempty($saveName)) $this->_saveImage($saveName);  //保存  
                  else $this->_showImage();  
           }  
      
           /** 
            * 內(nèi)部:打開(kāi)圖片 
            */  
           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();  
           }  
           /** 
            * 內(nèi)部:操作圖片 
            */  
           private function _thumpImage()  
           {  
                  $new_width = $this->imageinfo['width'] * $this->percent;  
                  $new_height = $this->imageinfo['height'] * $this->percent;  
                  $image_thump = imagecreatetruecolor($new_width,$new_height);  
                  //將原圖復(fù)制帶圖片載體上面,并且按照一定比例壓縮,極大的保持了清晰度  
                  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);  
           }  
           /** 
            * 保存圖片到硬盤(pán): 
            * @param  string $dstImgName  1、可指定字符串不帶后綴的名稱(chēng),使用源圖擴(kuò)展名 。2、直接指定目標(biāo)圖片名帶擴(kuò)展名。 
            */  
           private function _saveImage($dstImgName)  
           {  
                  if(emptyempty($dstImgName)) return false;  
                  $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];   //如果目標(biāo)圖片名有后綴就用目標(biāo)圖片擴(kuò)展名 后綴,如果沒(méi)有,則用源圖的擴(kuò)展名  
                  $dstExt =  strrchr($dstImgName ,".");  
                  $sourseExt = strrchr($this->src ,".");  
                  if(!emptyempty($dstExt)) $dstExt =strtolower($dstExt);  
                  if(!emptyempty($sourseExt)) $sourseExt =strtolower($sourseExt);  
      
                  //有指定目標(biāo)名擴(kuò)展名  
                  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);  
           }  
      
           /** 
            * 銷(xiāo)毀圖片 
            */  
           public function __destruct(){  
                  imagedestroy($this->image);  
           }  
    }  

注意:
當(dāng)上傳圖片過(guò)大時(shí),會(huì)報(bào)錯(cuò)誤,Allowed memory size內(nèi)存不足,報(bào)錯(cuò)提醒是imagecreatetruecolor()函數(shù),一開(kāi)始一直糾結(jié)在php.ini 中memory_limit 的設(shè)置,memory_limit 設(shè)置很大,還是會(huì)報(bào)錯(cuò),被坑了好久,最后把_thumpImage中imagecreatetruecolor()都注釋之后好了,


圖片.png

,這個(gè)_thumpImage方法,主要是根據(jù)縮放比例縮放圖片,不影響圖片壓縮
郁悶的是,我在本地環(huán)境運(yùn)行沒(méi)有報(bào)這個(gè)錯(cuò)誤,查看phpinfo,gd庫(kù)信息不一樣,可能是服務(wù)器gd庫(kù)不支持函數(shù)imagecreatetruecolor(),具體原因也不是很清楚


服務(wù)器gd信息

本地環(huán)境gd信息

參考文章
https://blog.csdn.net/qq_36608163/article/details/73167284

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容