<?php/***********************************************************類名:ImageWatermark功能:用於產生圖片或文字浮水印************************************************************合成浮水印:1、映像浮水印appendImageMark(暫不可旋轉)2、文字浮水印appendTextMark(漢字浮水印需要設定漢字字型)(可旋轉)輸出浮水印映像:write($filename=null)1、輸出到檔案:指定$filename參數為輸出的檔案名稱。2、輸出到瀏覽器:不指定輸出檔案名,則輸出到瀏覽器.指定浮水印位置:1、指定位置類型$markPosType:(default-0)1-top left 2-top center 3-top right4-middle left 5-middle center 6-middle right7-bottom left 8-bottom center 9-bottom right0-random2、設定具體位置setMarkPos($x,$y),若指定具體位置,則上面的位置類型無效。*************************************************************/class ImageWatermark{ public $markPosType = 0; //浮水印位置,預設為隨機位置輸出浮水印 public $fontFile = 'arial.ttf'; //字型檔名 public $color = '#CCCCCC'; //浮水印字型的顏色 public $fontSize = 12; //浮水印字型大小 public $angle = 0; //浮水印文字旋轉的角度 private $markPos = array(); private $markImageFile = null, $destImageFile = null; private $mark_res = null, $mark_width = 0, $mark_height = 0, $mark_type = null; private $dest_res = null, $dest_width = 0, $dest_height = 0, $dest_type = null; //用靶心圖表片作為建構函式的參數 public function __construct($destImage){ if(!file_exists($destImage)) return false; $this->destImageFile=$destImage; //擷取圖片大小、類型 $imageInfo = getimagesize($this->destImageFile); $this->dest_width = $imageInfo[0];$this->dest_height = $imageInfo[1];$this->dest_type = $imageInfo[2]; //得到圖片資源控制代碼 $this->dest_res = $this->getImageResource($this->destImageFile,$this->dest_type); } public function __destruct(){ imagedestroy($this->dest_res); } //添加文字浮水印 public function appendTextMark($markText){ if($markText==null) return false; //計算浮水印文本的大小 $box = imagettfbbox($this->fontSize,$this->angle,$this->fontFile,$markText); $this->mark_width = $box[2]-$box[6]; $this->mark_height = $box[3]-$box[7]; //計算浮水印位置 $pos = ($this->markPos!=null)?$this->markPos:$this->getMarkPosition($this->markPosType); $pos[1]+=$this->mark_height; //將文字列印到圖片上 $RGB=$this->colorHexRgb($this->color); $imageColor=imagecolorallocate($this->dest_res,$RGB[0],$RGB[1],$RGB[2]); imagettftext($this->dest_res,$this->fontSize,$this->angle,$pos[0],$pos[1],$imageColor,$this->fontFile,$markText); } //添加圖片浮水印 public function appendImageMark($markImage){ if(!file_exists($markImage)) return false; $this->markImageFile=$markImage; //擷取浮水印圖片大小、類型 $imageInfo = getimagesize($this->markImageFile); $this->mark_width = $imageInfo[0];$this->mark_height = $imageInfo[1];$this->mark_type = $imageInfo[2]; //得到圖片資源控制代碼 $this->mark_res = $this->getImageResource($this->markImageFile,$this->mark_type); //計算浮水印位置 $pos = ($this->markPos!=null)?$this->markPos:$this->getMarkPosition($this->markPosType); //設定映像混色模式 imagealphablending($this->dest_res, true); //複製疊加映像 imagecopy($this->dest_res,$this->mark_res,$pos[0],$pos[1],0,0,$this->mark_width,$this->mark_height); imagedestroy($this->mark_res); } //將疊加浮水印後的圖片寫入指定檔案,若不定檔案名稱,則輸出到瀏覽器 public function write($filename=null){ $this->writeImage($this->dest_res,$filename,$this->dest_type); } //設定浮水印x,y座標 public function setMarkPos($x,$y){ $this->markPos[0]=$x; $this->markPos[1]=$y; } //將十六進位的顏色值分解成RGB形式 private function colorHexRgb($color){ $color = preg_replace('/#/','',$color); $R=hexdec($color[0].$color[1]); $G=hexdec($color[2].$color[3]); $B=hexdec($color[4].$color[5]); return array($R,$G,$B); } //計算浮水印位置 private function getMarkPosition($type=0){ switch($type){ case 0: $x = rand(0,$this->dest_width-$this->mark_width); $y = rand(0,$this->dest_height-$this->mark_height); break;//random case 1: $x = 0; $y = 0; break;//topleft case 2: $x = ($this->dest_width-$this->mark_width)/2; $y = 0; break; //topcenter case 3: $x = $this->dest_width-$this->mark_width; $y = 0; break;// topright case 4: $x = 0; $y = ($this->dest_height-$this->mark_height)/2; break;//middleleft case 5: $x = ($this->dest_width-$this->mark_width)/2; $y = ($this->dest_height-$this->mark_height)/2; break;//middlecenter case 6: $x = $this->dest_width-$this->mark_width; $y = ($this->dest_height-$this->mark_height)/2; break;//middleright case 7: $x = 0; $y = $this->dest_height-$this->mark_height; break;//bottomleft case 8: $x = ($this->dest_width-$this->mark_width)/2; $y = $this->dest_height-$this->mark_height; break;//bottomcenter case 9: $x = $this->dest_width-$this->mark_width; $y = $this->dest_height-$this->mark_height; break;//bottomright default:$x = rand(0,$this->dest_width-$this->mark_width); $y = rand(0,$this->dest_height-$this->mark_height); break;//random } return array($x,$y); } //從一個影像檔中取得圖片資源標識符 private function getImageResource($filename,$type=0){ switch($type){ case 1:return imagecreatefromgif($filename);break; case 2:return imagecreatefromjpeg($filename);break; case 3:return imagecreatefrompng($filename);break; // 以後可添加其它格式 default:return null; } } //將映像寫入檔案或輸出到瀏覽器 private function writeImage($ImageRes,$filename=null,$type=0){ switch($type) { case 1:imagegif($ImageRes,$filename);break; case 2:imagejpeg($ImageRes,$filename);break; case 3:imagepng($ImageRes,$filename);break; default:return null; } return true; }}//使用樣本$markimg = new ImageWatermark('c_si.jpg');//$markimg->setMarkPos(100,200);//如何設定setMarkPos,則markPosType無效。$markimg->markPosType=5;$markimg->appendImageMark('mark.png');$markimg->fontFile='STCAIYUN.TTF';$markimg->color='#FFFFFF';$markimg->fontSize=24;$markimg->angle=45;//設定角度時,注意浮水印可能旋轉出靶心圖表片之外。$markimg->appendTextMark('漢字浮水印');$markimg->write();$markimg=null;?>