支持文字水印、图片水印
支持水印的位置随机或固定(九宫格)
水印透明度设置(图片水印和文字水印都支持)
文字水印的字体、颜色、大小设置
图片水印的背景透明
<BR><?php <BR>/** <BR>* 加水印类,支持文字图片水印的透明度设置、水印图片背景透明。 <BR>* 日期:2011-09-27 <BR>* 作者:www.gaodaima.com <BR>* 使用: <BR>* $obj = new WaterMask($imgFileName); //实例化对象 <BR>* $obj->$waterType = 1; //类型:0为文字水印、1为图片水印 <BR>* $obj->$transparent = 45; //水印透明度 <BR>* $obj->$waterStr = 'www.gaodaima.com'; //水印文字 <BR>* $obj->$fontSize = 16; //文字字体大小 <BR>* $obj->$fontColor = array(255,0255); //水印文字颜色(RGB) <BR>* $obj->$fontFile = = 'AHGBold.ttf'; //字体文件 <BR>* $obj->output(); //输出水印图片文件覆盖到输入的图片文件 <BR>*/ <BR>class WaterMask{ <BR>public $waterType = 1; //水印类型:0为文字水印、1为图片水印 <BR>public $pos = 0; //水印位置 <BR>public $transparent = 45; //水印透明度 <br><br>public $waterStr = 'www.gaodaima.com'; //水印文字 <BR>public $fontSize = 16; //文字字体大小 <BR>public $fontColor = array(255,0,255); //水印文字颜色(RGB) <BR>public $fontFile = 'AHGBold.ttf'; //字体文件 <br><br>public $waterImg = 'logo.png'; //水印图片 <br><br>private $srcImg = ''; //需要添加水印的图片 <BR>private $im = ''; //图片句柄 <BR>private $water_im = ''; //水印图片句柄 <BR>private $srcImg_info = ''; //图片信息 <BR>private $waterImg_info = ''; //水印图片信息 <BR>private $str_w = ''; //水印文字宽度 <BR>private $str_h = ''; //水印文字高度 <BR>private $x = ''; //水印X坐标 <BR>private $y = ''; //水印y坐标 <br><br>function __construct($img) { //析构函数 <BR>$this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!'); <BR>} <BR>private function imginfo() { //获取需要添加水印的图片的信息,并载入图片。 <BR>$this->srcImg_info = getimagesize($this->srcImg); <BR>switch ($this->srcImg_info[2]) { <BR>case 3: <BR>$this->im = imagecreatefrompng($this->srcImg); <BR>break 1; <BR>case 2: <BR>$this->im = imagecreatefromjpeg($this->srcImg); <BR>break 1; <BR>case 1: <BR>$this->im = imagecreatefromgif($this->srcImg); <BR>break 1; <BR>default: <BR>die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。'); <BR>} <BR>} <BR>private function waterimginfo() { //获取水印图片的信息,并载入图片。 <BR>$this->waterImg_info = getimagesize($this->waterImg); <BR>switch ($this->waterImg_info[2]) { <BR>case 3: <BR>$this->water_im = imagecreatefrompng($this->waterImg); <BR>break 1; <BR>case 2: <BR>$this->water_im = imagecreatefromjpeg($this->waterImg); <BR>break 1; <BR>case 1: <BR>$this->water_im = imagecreatefromgif($this->waterImg); <BR>break 1; <BR>default: <BR>die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。'); <BR>} <BR>} <BR>private function waterpos() { //水印位置算法 <BR>switch ($this->pos) { <BR>case 0: //随机位置 <BR>$this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]); <BR>$this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]); <BR>break 1; <BR>case 1: //上左 <BR>$this->x = 0; <BR>$this->y = 0; <BR>break 1; <BR>case 2: //上中 <BR>$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; <BR>$this->y = 0; <BR>break 1; <BR>case 3: //上右 <BR>$this->x = $this->srcImg_info[0]-$this->waterImg_info[0]; <BR>$this->y = 0; <BR>break 1; <BR>case 4: //中左 <BR>$this->x = 0; <BR>$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; <BR>break 1; <BR>case 5: //中中 <BR>$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; <BR>$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; <BR>break 1; <BR>case 6: //中右 <BR>$this->x = $this->srcImg_info[0]-$this->waterImg_info[0]; <BR>$this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2; <BR>break 1; <BR>case 7: //下左 <BR>$this->x = 0; <BR>$this->y = $this->srcImg_info[1]-$this->waterImg_info[1]; <BR>break 1; <BR>case 8: //下中 <BR>$this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2; <BR>$this->y = $this->srcImg_info[1]-$this->waterImg_info[1]; <BR>break 1; <BR>default: //下右 <BR>$this->x = $this->srcImg_info[0]-$this->waterImg_info[0]; <BR>$this->y = $this->srcImg_info[1]-$this->waterImg_info[1]; <BR>break 1; <BR>} <BR>} <BR>private function waterimg() { <BR>if ($this->srcImg_info[0] waterImg_info[0] || $this->srcImg_info[1] waterImg_info[1]){ <BR>die('水印比原图大!'); <BR>} <BR>$this->waterpos(); <BR>$cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]); <BR>imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]); <BR>$pct = $this->transparent; <BR>imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_<i style="color:transparent">本#文来源gaodai$ma#com搞$$代**码网$</i><button>搞代gaodaima码</button>info[0],$this->waterImg_info[1]); <BR>imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct); <BR>} <BR>private function waterstr() { <BR>$rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr); <BR>$w = abs($rect[2]-$rect[6]); <BR>$h = abs($rect[3]-$rect[7]); <BR>$fontHeight = $this->fontSize; <BR>$this->water_im = imagecreatetruecolor($w, $h); <BR>imagealphablending($this->water_im,false); <BR>imagesavealpha($this->water_im,true); <BR>$white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127); <BR>imagefill($this->water_im,0,0,$white_alpha); <BR>$color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]); <BR>imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr); <BR>$this->waterImg_info = array(0=>$w,1=>$h); <BR>$this->waterimg(); <BR>} <BR>function output() { <BR>$this->imginfo(); <BR>if ($this->waterType == 0) { <BR>$this->waterstr(); <BR>}else { <BR>$this->waterimginfo(); <BR>$this->waterimg(); <BR>} <BR>switch ($this->srcImg_info[2]) { <BR>case 3: <BR>imagepng($this->im,$this->srcImg); <BR>break 1; <BR>case 2: <BR>imagejpeg($this->im,$this->srcImg); <BR>break 1; <BR>case 1: <BR>imagegif($this->im,$this->srcImg); <BR>break 1; <BR>default: <BR>die('添加水印失败!'); <BR>break; <BR>} <BR>imagedestroy($this->im); <BR>imagedestroy($this->water_im); <BR>} <BR>} <BR>?> <BR>