閱讀906 返回首頁    go 阿裏雲 go 技術社區[雲棲]


php之圖片處理類縮略圖加水印

用到兩個image係統函數

imagecopymerge 拷貝並合並圖像的一部分

imagecopyresampled 重采樣拷貝部分圖像並調整大小

/*
如何知道圖片的大小和類型
無法確認調用函數:Imagecreatefrompng/jpeg……
可以獨處圖片的寬和高

相當於寬高是已知的
一個重要的函數getimagesize()
*/

/*
想操作圖片
先把圖片的大小,類型信息得到

水印:就是把指定的水印複製到目標上,並加透明效果

縮略圖:就是把大圖片複製到小尺寸畫麵上

*/



class ImageTool{

	//imageinfo	分析圖片的信息
	//return array()
	public static function imageInfo($image){
		//判斷圖片是否存在
		if(!file_exists($image)){
			return false;
		
		}
		$info = getimagesize($image);
		if($info == false){
			return false;
		}
		
		//此時info分析出來是一個數組
		$img['width'] = $info[0];
		$img['height'] = $info[1];
		//分析圖片的後綴
		$img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1);
		
		return $img;
	} 
	
	/*
	加水印
	string $dst 待操作圖片
	       $water 水印小圖
		   $save  不填,則默認替換原始圖
		   $alpha  透明度
		   $pos    水印圖放的位置1234,順時針
	*/
	public static function water($dst,$water,$save=NULL,$pos=2,$alpha=50){
		//先保證兩個圖片存在
		if(!file_exists($dst) || !file_exists($water)){
			return false;
		}
		
		//1.保證水印不能比待操作圖片還大
		$dinfo = self::imageInfo($dst);
		$winfo = self::imageInfo($water);
		
		//判斷 
		if($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']){
			return false;
		}
		
		//兩張圖,讀到畫布上,但是圖片可能是png,可能是jpeg,用什麼函數來讀
		$dfunc = 'imagecreatefrom' .$dinfo['ext'];
		$wfunc = 'imagecreatefrom' .$winfo['ext'];
		
		if(!function_exists($dfunc) || !function_exists($wfunc)){
			return false;
		}
		
		//動態加載函數來創建畫布
		$dim = $dfunc($dst); //創建待操作的畫布
		$wim = $wfunc($water); //創建水印畫布
		
		//根據水印的位置,計算粘貼的坐標
		switch($pos){
			case 0; //左上角
			$posx = 0;
			$posy = 0;
			break;
			
			case 1; //右上角
			$posx = $dinfo['width'] - $winfo['width'];
			$posy = 0;
			break;
			
			case 2; //左下角
			$posx = 0;
			$posy = $dinfo['height'] - $winfo['height'];
			break;
			
			default:   //默認右下角
			$posx = $dinfo['width'] - $winfo['width'];
			$posy = $dinfo['height'] - $winfo['height'];
			
		}
		
		//加水印 在什麼位置?
		// imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

		imagecopymerge($dim,$wim,$posx,$posy,0,0,$winfo['width'],$winfo['height'],$alpha);
		
		if(!$save){
		
			$save=$dst;
			unlink($dst); //刪除原圖
		}
		
		//保存圖片
		$createfunc = 'image' . $dinfo['ext'];
		$createfunc($dim,$save);
		
		//銷毀圖片
		imagedestroy($dim);
		imagedestroy($wim);
		
		return true;
	}

	/*
		thumb 生成縮略圖
		等比例縮放,兩邊留白
	*/
	public static function thumb($dst,$save=NULL,$width=200,$height=200){
		//首先判斷待處理的圖片是否存在
		$dinfo = self::imageInfo($dst);
		if($dinfo == false){
			return false;
		}
		
		//計算縮放比例
		$calc = min($width/$dinfo['width'],$height/$dinfo['height']);
		
		//創建原始圖的畫布
		$dfunc = 'imagecreatefrom'.$dinfo['ext'];
		$dim = $dfunc($dst);
		
		//創建縮略圖畫布
		$tim = imagecreatetruecolor($width,$height);
		
		//創建白色填充縮略圖畫布
		$white = imagecolorallocate($tim,255,255,255);
		
		//填充縮略畫布
		imagefill($tim,0,0,$white);
		
		//複製並縮略
		$dwidth = (int)$dinfo['width']*$calc;
		$dheight = (int)$dinfo['height']*$calc;
		
		$paddingx = ($width - $dwidth) / 2;
		$paddingy = ($height - $dheight) /2;
		
		imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']);
		
		//保存圖片
		if(!$save){
			$save = $dst;
			unlink($dst);
		}
		$createfun = 'image'.$dinfo['ext'];
		$createfun($tim,$save);
		
		imagedestroy($dim);
		imagedestroy($tim);
		
		return true;
		
	}		

調用傳參:

//調用傳參  加水印
echo ImageTool::water('原圖片地址','加水印圖片地址','生成圖片')?'ok':false;
//調用 縮略圖
echo ImageTool::thumb('原圖地址','生成圖片地址',200,200)?'ok':false;



 

 

最後更新:2017-04-02 22:15:57

  上一篇:go uva 10391 - Compound Words
  下一篇:go 12.9 訓練 E - Ternary Password