<?php
/*************************
类名:imagewatermark
功能:用于生成图片或文字水印
****************************
合成水印:
1、图像水印appendimagemark(暂不可旋转)
2、文字水印appendtextmark(汉字水印需要设置汉字字体)(可旋转)
输出水印图像:write($filename=null)
1、输出到文件:指定$filename参数为输出的文件名。
2、输出到浏览器:不指定输出文件名,则输出到浏览器.
指定水印位置:
1、指定位置类型$markpostype:(default-0)
1-top left 2-top center 3-top right
4-middle left 5-middle center 6-middle right
7-bottom left 8-bottom center 9-bottom right
0-random www.jb200.com
2、设置具体位置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;
?>