php圖片壓縮兩個類

不是壓縮尺寸,是圖片的佔用空間,第二個比較好用app

1.ide

 
 
class image_blur{
  
/** 
     * 圖片高斯模糊(適用於png/jpg/gif格式) 
     * @param $srcImg 原圖片 
     * @param $savepath 保存路徑 
     * @param $savename 保存名字 
     * @param $positon 模糊程度  
     * 
     *基於Martijn Frazer代碼的擴充, 感謝 Martijn Frazer 
     */  
    public function gaussian_blur($srcImg,$savepath=null,$savename=null,$blurFactor=3){  
        $gdImageResource=$this->image_create_from_ext($srcImg);  
        $srcImgObj=$this->blur($gdImageResource,$blurFactor);  
        $temp = pathinfo($srcImg);  
        $name = $temp['basename'];  
        $path = $temp['dirname'];  
        $exte = $temp['extension'];  
        $savename = $savename ? $savename : $name;  
        $savepath = $savepath ? $savepath : $path;  
        $savefile = $savepath .'/'. $savename;  
        $srcinfo = @getimagesize($srcImg);  
          
        switch ($srcinfo[2]) {  
            case 1: imagegif($srcImgObj, $savefile); break;  
            case 2: imagejpeg($srcImgObj, $savefile); break;  
            case 3: imagepng($srcImgObj, $savefile); break;  
            default: return '保存失敗'; //保存失敗  
        }  
  
        return $savefile;  
        imagedestroy($srcImgObj);  
    }  
  
    /** 
    * Strong Blur 
    * 
    * @param  $gdImageResource  圖片資源 
    * @param  $blurFactor          可選擇的模糊程度  
    *  可選擇的模糊程度  0使用   3默認   超過5時 極其模糊 
    * @return GD image 圖片資源類型 
    * @author Martijn Frazer, idea based on http://stackoverflow.com/a/20264482 
    */  
    private function blur($gdImageResource, $blurFactor = 3)  
    {  
        // 模糊因子必須是一個整數 
        $blurFactor = round($blurFactor);  
  
        $originalWidth = imagesx($gdImageResource);  
        $originalHeight = imagesy($gdImageResource);  
  
        $smallestWidth = ceil($originalWidth * pow(0.5, $blurFactor));  
        $smallestHeight = ceil($originalHeight * pow(0.5, $blurFactor));  
  
        // for the first run, the previous image is the original input  
        $prevImage = $gdImageResource;  
        $prevWidth = $originalWidth;  
        $prevHeight = $originalHeight;  
  
        // scale way down and gradually scale back up, blurring all the way  
        for($i = 0; $i < $blurFactor; $i += 1)  
        {      
            // determine dimensions of next image  
            $nextWidth = $smallestWidth * pow(2, $i);  
            $nextHeight = $smallestHeight * pow(2, $i);  
  
            // resize previous image to next size  
            $nextImage = imagecreatetruecolor($nextWidth, $nextHeight);  
            imagecopyresized($nextImage, $prevImage, 0, 0, 0, 0,   
              $nextWidth, $nextHeight, $prevWidth, $prevHeight);  
  
            // apply blur filter  
            imagefilter($nextImage, IMG_FILTER_GAUSSIAN_BLUR);  
  
            // now the new image becomes the previous image for the next step  
            $prevImage = $nextImage;  
            $prevWidth = $nextWidth;  
            $prevHeight = $nextHeight;  
        }  
  
        // scale back to original size and blur one more time  
        imagecopyresized($gdImageResource, $nextImage,   
        0, 0, 0, 0, $originalWidth, $originalHeight, $nextWidth, $nextHeight);  
        imagefilter($gdImageResource, IMG_FILTER_GAUSSIAN_BLUR);  
  
        // clean up  
        imagedestroy($prevImage);  
  
        // return result  
        return $gdImageResource;  
    }  
  
    private function image_create_from_ext($imgfile)  
    {  
        $info = getimagesize($imgfile);  
        $im = null;  
        switch ($info[2]) {  
        case 1: $im=imagecreatefromgif($imgfile); break;  
        case 2: $im=imagecreatefromjpeg($imgfile); break;  
        case 3: $im=imagecreatefrompng($imgfile); break;  
        }  
        return $im;  
    }  
  
} 

2.this

class Compress
{
    private $src;
    private $image;
    private $imageinfo;
    private $percent=0.5;
    
    /*
    param    $src源圖
    param    $percent壓縮比例
    */
    public function __construct($src,$percent=1)
    {
        $this->src = $src;
        $this->percent = $percent;
    }
    
    
    
    /*
    param string $saveName 圖片名(可不帶擴展名用原圖名)用於保存。或不提供文件名直接顯示
    */
    public function compressImg($saveName='')
    {
        $this->_openImage();
        if(!empty($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  一、可指定字符串不帶後綴的名稱,使用源圖擴展名 。二、直接指定目標圖片名帶擴展名。
    */
    private function _saveImage($dstImgName)
    {
        if(empty($dstImgName)) return false;
        $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];   //若是目標圖片名有後綴就用目標圖片擴展名 後綴,若是沒有,則用源圖的擴展名
        $dstExt =  strrchr($dstImgName ,".");
        $sourseExt = strrchr($this->src ,".");
        if(!empty($dstExt)) $dstExt =strtolower($dstExt);
        if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
     
        //有指定目標名擴展名
        if(!empty($dstExt) && in_array($dstExt,$allowImgs))
        {
            $dstName = $dstImgName;
        }
        elseif(!empty($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);
    }
}

$source = 'test.jpg';
$dst_img = 'test.jpg';
$percent = 1;  #原圖壓縮,不縮放
$image = (new Compress($source,$percent))->compressImg($dst_img);
相關文章
相關標籤/搜索