1. 程式人生 > >php gd庫水印類7年後重構了 支援php7

php gd庫水印類7年後重構了 支援php7

<?php
/**
 * 縮圖水印生成類 文字水印 字元編碼為 utf-8 
 * 中文需要處理還字型的問題
 * @name    MakeMiniature
 * @see  
 * @version	2.1.0 (2016-1-22)
 * @author	sanshi0815
 */
class MakeMiniature
{
	//字型帶目錄
	private $font;
	//水印內容 可以是 文字,可以是圖片路徑,也可以為為空
	private $watermark;
	//原始檔
	private $srcFile;
	//目標檔案
	private $dstFile;
	//水印類別 0 無水印,1圖片水印,2文字水印
	private $watetType;
	//支援的圖片類別當前支援2種
	private $imgType=array("jpg","jpeg","png");
	//圖片開啟資源控制代碼
	private $im;
	//原始圖片型別
	private $fileType;
	private $errorMsg="";
	public function __construct()
	{

	}
	/**
	* 獲取失敗資訊
	* @author  sanshi0815
	* @return  		string	失敗資訊
	*/
	public function getErrorMsg()
	{
		return $this->errorMsg;
	}
	/**
	* 引數設定
	* @author  sanshi0815
	* @param 		string		$font	字型檔案帶目錄文字水印使用
	* @param 		string		$watermark	水印文字或者是圖片地址
	* @param 		string		$srcFile	原始圖片地址
	* @param 		string		$dstFile	生成新圖片地址
	* @return  		null					無返回值
	*/
	public function set($font,$watermark,$srcFile,$dstFile)
	{
		$this->font = $font;
		$this->watermark = $watermark;
		$this->srcFile = $srcFile;
		$this->dstFile = $dstFile;
		if(empty($this->watermark ))
		{
			//無水印
			$this->watetType = 0;
		}elseif(is_file($this->watermark)){
			//圖片水印
			$this->watetType = 1;
		}else{
			//文字水印
			$this->watetType = 2;
		}
	}
	/**
	* 圖片資源獲取 成功返回陣列,失敗返回false
	* @author  sanshi0815
	* @param 		string		$fileName	圖片地址
	* @return  		array		r{控制代碼},t{字尾},w{寬度},h{高度}
	*/
	private function getResource($fileName)
	{
		if(!is_file($fileName))
		{
			$this->errorMsg = "{$fileName}  不存在 line:".__LINE__;
		}
		$temp = explode('.',$fileName);
		$fileType = strtolower(end($temp));
		//判斷後綴是否是否符合要求
		if(!in_array($fileType,$this->imgType))
		{
			//檔案型別不支援
			$this->errorMsg = "{$fileName}圖片字尾型別不支援 line:".__LINE__;
			return false;
		}
		if($fileType=="jpg" || $fileType=="jpeg")
		{
			$im=imageCreateFromjpeg($fileName);
		}else{
			$im=imagecreatefrompng($fileName);
		}
		if(!$im)
		{
			//圖片初始化失敗
			$this->errorMsg = "{$fileName}圖片初始化資源失敗 line:".__LINE__;
			return false;
		}
		//源圖片寬
		$width=imagesx($im);
		//源圖片高
		$height=imagesy($im);
		if(empty($width) || empty($height))
		{
			//圖片高度寬度獲取失敗
			$this->errorMsg = "{$fileName}圖片高度或者寬度獲取失敗 line:".__LINE__;
			return false;
		}
		return array("r"=>$im,"t"=>$fileType,"w"=>$width,"h"=>$height);
	}
	/**
	* 原始圖片全域性變數設定 成功返回陣列,失敗返回false 
	* @author  sanshi0815
	* @return  	array		w{寬度},h{高度}
	*/
	private function initSrcImgWH()
	{
		$temp = $this->getResource($this->srcFile);
		if(empty($temp)) 
		{
			$this->errorMsg = "影象資源不存在 line:".__LINE__;
			return false;
		}
		$this->fileType = $temp['t'];
		$this->im=$temp['r'];
		return array("w"=>$temp['w'],"h"=>$temp['h']);
	}
	/**
	* 固定寬度,高度 進行圖片縮放
	* @author  sanshi0815
	* @param 		int		$width	生成圖片寬度
	* @param 		int		$height	生成圖片高度
	* @return  	bool		成功為 true,失敗為false
	*/
	public function resetImgWH($width,$height)
	{
		$temp = $this->initSrcImgWH();
		if(empty($temp))
		{
			$this->errorMsg = "影象資源不存在 line:".__LINE__;
			return false;
		}
		$srcW = $temp['w'];
		$srcH = $temp['h'];
		$detW = intval($width);
		$detH = intval($height);
		//生成新的影象資源
		$om = $this->getNewImg($srcW,$srcH,$detW,$detH);
		$temp = empty($om) ? false : $this->createImg($om,$detW,$detH);
		return $temp;
	}
	/**
	* 根據最大高度 進行圖片等比縮放
	* @author  sanshi0815
	* @param 		int		$maxHeight	生成圖片高度
	* @return  	bool		成功為 true,失敗為false
	*/
	public function resetImgMaxH($maxHeight)
	{
		$maxHeight = intval($maxHeight);
		$temp = $this->initSrcImgWH();
		if(empty($temp))
		{
			$this->errorMsg = "影象資源不存在 line:".__LINE__;
			return false;
		}
		$srcW = $temp['w'];
		$srcH = $temp['h'];
		//計算縮放比
		$scale = round($maxHeight/$srcH,4);
		$detW = round($srcW*$scale);
		$detH = round($srcH*$scale);
		//生成新的影象資源
		$om = $this->getNewImg($srcW,$srcH,$detW,$detH);
		$temp = empty($om) ? false : $this->createImg($om,$detW,$detH);
		return $temp;

	}
	/**
	* 根據最大寬度 進行圖片等比縮放
	* @author  sanshi0815
	* @param 		int		$maxWidth	生成圖片寬度
	* @return  	bool		成功為 true,失敗為false
	*/
	public function resetImgMaxW($maxWidth)
	{
		$temp = $this->initSrcImgWH();
		if(empty($temp))
		{
			$this->errorMsg = "影象資源不存在 line:".__LINE__;
			return false;
		}
		$srcW = $temp['w'];
		$srcH = $temp['h'];
		//計算縮放比
		$scale = round($maxWidth/$srcW,4);
		$detW = round($srcW*$scale);
		$detH = round($srcH*$scale);
		//生成新的影象資源
		$om = $this->getNewImg($srcW,$srcH,$detW,$detH);
		//$om = $this->im;
		$temp = empty($om) ? false : $this->createImg($om,$detW,$detH);
		return $temp;

	}
	/**
	* 獲得縮放後的圖片資源控制代碼
	* @author  sanshi0815
	* @param 		int		$srcW	原始圖片寬度
	* @param 		int		$srcH	原始圖片高度
	* @param 		int		$detW	原始圖片寬度
	* @param 		int		$detH	原始圖片高度
	* @return  	bool		成功為 true,失敗為false
	*/
	private function getNewImg($srcW,$srcH,$detW,$detH)
	{
		$om=imagecreatetruecolor($detW,$detH);//真色彩對gb庫有要求
		if(empty($om))
		{
			$this->errorMsg = "imagecreatetruecolor 函式失敗 line:".__LINE__;
			return false;
		}
		//ImageCopyResized($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
		$temp = imagecopyresampled($om,$this->im,0,0,0,0,$detW,$detH,$srcW,$srcH);
		if(empty($temp))
		{
			$this->errorMsg = "imagecopyresampled 函式失敗 line:".__LINE__;
			return false;
		}
		return $om;
	}
	/**
	* 獲得圖片加文字水印後資源
	* @author  sanshi0815
	* @param 		resource		$im  	原始資源
	* @param 		int		$detW	原始圖片寬度
	* @param 		int		$detH	原始圖片高度
	* @return  	resource		成功為 水印後的圖片資源,失敗為false
	*/
	private function getWatermarkText($im,$detW,$detH)
	{
		if(!$is_file($this->font))
		{
			$this->errorMsg = "{$this->font} 字型不存在 line:".__LINE__;
			return false;
		}
		//旋轉角度
		$angle = 20; 
		$width = $detW/10;
		$size =  $detW/8;
		$height = $detH;
		//echo $height;
		$black = imagecolorallocate($im, 0, 0, 0);
		$grey = imagecolorallocate($im, 180, 180, 180);
		//生成水印次數
		$formax = 3;
		for($i=$formax;$i>=1;$i--)
		{
			$height =$height-$detH/($formax+2);
			//echo $height."<br/>";
			$temp = imagettftext($im, $size, $angle,$width,$height, $grey, $this->font,$this->watermark);
			if(empty($temp))
			{
				$this->errorMsg = "imagettftext 函式失敗 line:".__LINE__;
				return false;
			}
		}
		return $im;
	}
	/**
	* 獲得圖片加圖片水印後資源
	* @author  sanshi0815
	* @param 		resource		$im  	原始資源
	* @param 		int		$detW	原始圖片寬度
	* @param 		int		$detH	原始圖片高度
	* @return  	resource		成功為 水印後的圖片資源,失敗為false
	*/
	private function getWatermarkPic($im,$detW,$detH)
	{
		$temp = $this->getResource($this->watermark);
		if(empty($temp)) return false;
		$wm = $temp['r'];
		$src_x = 0;
		$src_y =  0;
		$src_w = $temp['w'];
		$src_h = $temp['h'];
		$dst_x = $detW;
		$dst_y = $detH;
		$height = $dst_y > $src_h ? ($dst_y - $src_h)/2 :0;
		$width =  $dst_x > $src_w ? ($dst_x - $src_w)/2 : 0;
		$temp = imagealphablending($im,true);
		if(empty($temp))
		{
			$this->errorMsg = "imagealphablending 函式失敗 line:".__LINE__;
			return false;
		}
		$temp = imagecopymerge($im,$wm,$width,$height,0,0,$src_w,$src_h,70);
		if(empty($temp))
		{
			$this->errorMsg = "imagecopymerge 函式失敗 line:".__LINE__;
			return false;
		}
		return $im;
	}
	/**
	* 新圖片生成
	* @author  sanshi0815
	* @param 		resource		$im  	原始資源
	* @param 		int		$detW	原始圖片寬度
	* @param 		int		$detH	原始圖片高度
	* @return  		bool		成功為true,失敗為false
	*/
	private function createImg($im,$detW,$detH)
	{
		//處理水印
		if($this->watetType==2)
		{
			$om = $this->getWatermarkText($im,$detW,$detH);
		}elseif ($this->watetType==1) {
			$om = $this->getWatermarkPic($im,$detW,$detH);
		}else{
			$om = $im;
		}
		if(empty($om))
		{
			$this->errorMsg = "圖片資源不存在 line:".__LINE__;
			return false;
		}
		$fileType = $this->fileType;
		if($fileType=="jpg" || $fileType=="jpeg")
		{
			$temp=imagejpeg($om,$this->dstFile);
		}else{
			$temp=imagepng($om,$this->dstFile);
		}
		return $temp;
	}
}

$file=new MakeMiniature();
$file->set("./simhei.ttf","張磊專用","1_1453362028.png","s1_1453362028.png");
$file->resetImgMaxW(800);

?>

很早以前的水印類   寫個東西,看到了曾經寫水印類,時間太久了,使用起來有些東西不太順手了,而且場景發生了變化,比如縮圖,更多的應該是根據最大寬度或者高度去生成而不是固定比例,因為這樣出來的圖片才是最佳的觀賞效果,而且不會變形。所以重新改進了這個類,對於php7的支援,實際上老的類也是支援的。

以前的類     http://blog.csdn.net/sanshi0815/article/details/1604905