最近在開發上需要使用PHP來產生縮圖,因此在google上搜尋了一下,偶然看到自己舊站發表過一篇自訂尺寸等比例縮圖的文章。(自己都忘了=.=)

不過實測後發現到照著舊文章的code,執行起來似乎有那麼些小bug,那時接觸PHP的時間很短,還很菜。(雖然現在還是很嫩?)

不過說來真是慚愧,那篇文章竟然出現在搜尋結果的第一頁...(好險不是第一筆,不然臉都不知道要往哪裡擺了)

今天就重新寫一個再製版吧~應該會比舊的好一點點吧!


直接進入正文,曬改寫後的code


縮圖類Code

<?php
/**
 * 縮圖類
 * author:Aidec li
 */
class Thumb
{
	public $path;
	public $output;
	public $width;
	public $height;
	public $quality = 100;
	

	function __construct()
	{
		
	}

	/**
	 *	保存縮圖
	 *	@param $path 	 原始圖路徑
	 *	@param $output 	 輸出圖路徑
	 *	@param $width    輸出的寬度
	 *	@param $height   輸出的高度
	 *	@param $quality  輸出的品質
	 */
	//處理縮圖函式 (路徑,輸出寬度,輸出高度,輸出品質)
	public function save($path='',$output='',$width=0,$height=0,$quality=0){

	 	$this->path 	= ($this->isExist($path)) 	? $path 	: $this->path;
	 	$this->output 	= ($this->isExist($output)) ? $output 	: $this->output;
	 	$this->width 	= ($width>0) 	? $width 	: $this->width;
	 	$this->height 	= ($height>0) 	? $height 	: $this->height;
	 	$this->quality 	= ($quality>0)  ? $quality 	: $this->quality;
	 	
	 	//檢查參數是否正確
	 	if(!$this->isExist($this->path)){
	 		die('原圖路徑未設定或圖片不存在!');
	 	}
	 	//獲取原圖路徑資訊
	 	$origImageInfo = pathinfo($this->path);
	 	//獲取原始圖的寬高
	 	list($width_o,$height_o)=getimagesize($path);

	 	//若無指定輸出路徑則以原始路徑的檔名+_s  
	 	//例如 /path/images/xxx.jpg => /path/images/xxx_s.jpg
	 	if($this->output==''){ 
	 		$this->output = $origImageInfo['dirname'].'/'.$origImageInfo['filename'].'_s.'.$origImageInfo['extension'];
	 	}

	 	//若無有效的寬度,則以原始寬度
	 	if((int)$this->width<=0){
	 		$this->width = $width_o;
	 	}

	 	//若無有效高度,則以原始高度
	 	if((int)$this->height<=0){
	 		$this->height = $height_o;
	 	}

	 	//判斷原圖片格式副檔名
		$ext = $origImageInfo['extension'];
		//$img_f=strrchr($path,"."); 
		 
		//對輸出路徑產生對應資料夾
		$this->genDir($this->output); 
		 
		//建立一個自訂寬度、高度的黑色畫布
		$image_pic = imagecreatetruecolor($this->width,$this->height);
		 
		//判斷副檔名,建立對應的圖像(圖像來源為原始圖片)
		switch (strtolower($ext)) {
			case 'jpg': 
			case 'jpeg':
				$image=imagecreatefromjpeg($path); break; 
			case 'png': 
				$image=imagecreatefrompng($path); 
				//保留透明度
				$color=imagecolorallocate($image_pic,255,255,255);  
			        imagecolortransparent($image_pic,$color); 
			        imagefill($image_pic,0,0,$color); 
				break;
			case 'gif': 
				$image=imagecreatefromgif($path); break; 
			default:
				die('不支援此圖片格式'); break;
		} 
		
		//保留透明度
		//imagesavealpha($image_pic, true);
	    //$color = imagecolorallocatealpha($image_pic, 0, 0, 0, 127);
	    //imagefill($image_pic, 0, 0, $color);

		//將圖像複製到另一個圖像(原始圖像 to 新的圖像)
		//imagecopyresampled(新目標圖像,原始圖像,目標X起點,目標Y起點,來源X起點,來源Y起點,目標圖像寬度,目標圖像高度,原始圖像寬度,原始圖像高度)
		imagecopyresampled($image_pic,$image,0,0,0,0,$this->width,$this->height,$width_o,$height_o);
		// start resize (產生縮圖 - 無法處理透明度)
		//imagecopyresized($image_pic,$image, 0, 0, 0, 0, $width,$height,$width_o,$height_o);
		
		 
		//判斷副檔名,使用對應函式將檔案存入
		switch (strtolower($ext)) {
			case 'jpg': 
			case 'jpeg':
				//imagejpeg(圖像來源,儲存路徑,圖像品質)
				imagejpeg($image_pic,$this->output,$this->quality);
			case 'png': 
				//imagepng(圖像來源,儲存路徑,圖像品質)
				imagepng($image_pic,$this->output,round(9*($this->quality/100)));
			case 'gif': 
				imagegif($image_pic,$this->output); break; 
			default:
				die('不支援此圖片格式'); break;
		}  
		//釋放資源
		imagedestroy($image_pic);
		//回傳縮圖後的路徑
		return $output; 
	}

	 


	/**
		判斷路徑是否為空,且檔案是否真實存在
	*/
	public function isExist($path='')
	{
		if($path!='' && file_exists($path)){
	 		return true;
	 	}
	 	return false;
	}


	/**
		產生資料夾
	*/
	public function genDir($path='')
	{
		$imageInfo = pathinfo($path);
		$dir=$imageInfo['dirname'].'/'; 
	    if(!is_dir($dir)){
	        mkdir($dir, 0777, TRUE);
	        //禁止直接訪問目錄
	        $str = "Directory access is forbidden.";
	        $x = fopen($dir."index.html","a+"); 
	        fwrite($x,$str);
	        fclose($x);
	    }
	}
}

使用方式

<?php
  include 'thumb.class.php'; 
  $thumb = new Thumb();
  $thumb->width = 200; //設定縮圖寬度
  $thumb->height = 200; //設定縮圖高度
  $thumb->quality = 90; //設定縮圖品質
  //$thumb->output = ''; //設定輸出路徑
  $thumb->save( 'path/7b7a577d1d9450c70c9993c49bcd63a4.png');


這次有好好的粗略測試過,是能運行的。

但原圖若有透明度有時似乎輸出的不完美。改天有空再研究看看。


文章轉載或引用,請先告知並保留原文出處與連結!!(單純分享或非營利的只需保留原文出處,不用告知)

原文連結:
https://blog.aidec.tw/post/php-thumb