PHP图片水印
class img_mark{
private $info;
private $type;
private $img;
public $imgtype = array(1=>'gif',2=>'jpeg',3=>'png',4=>'wbmp');
//文字水印
public $str = 'www.lowxp.com';//水印字串
public $fs = 16;//字体尺寸
public $fa = 0;//字体角度*
public $fc = array(0,0,0);//字体颜色
function __construct($src_img, $dst_path, $img_ttf, $fn='img_mark'){//源图片地址,存储目标图片地址
//获取源图片信息
$this->info = getimagesize($src_img);
$this->type = $this->imgtype[$this->info[2]];
$this->img = $this->imgcreate($this->type, $src_img);
$this->$fn($dst_path, $img_ttf);//调用相关函数
}
//创建图片
private function imgcreate($type,$img){$fn='imagecreatefrom'.$type;return $fn($img);}
//图片水印。
private function img_mark($dst_path, $wtImg){//源图片,目标图片路径,水印图片
//水印图片信息
$wtImgInfo = getimagesize($wtImg);//水印图片信息
$wtImgType = $this->imgtype[$wtImgInfo[2]];
$wtImg = $this->imgcreate($wtImgType, $wtImg);
imagecopy($this->img,$wtImg,0,0,0,0,$wtImgInfo[0],$wtImgInfo[1]);
//imagecopy 目标片,水印图片,水印图到目标图左边框的距离,水印图到目标图上边框的距离, 水印图片padding-left.水印图片padding-top,水印图片宽度,水印图片高度
//imagecopyresampled($this->img,$wtImg,0,0,0,0,$wtImgInfo[0],$wtImgInfo[1],$wtImgInfo[0],$wtImgInfo[1]);
//imagecopymerge($this->img, $wtImg, 0,0,0,0, $wtImgInfo[0], $wtImgInfo[1], 100);
$imgfn = 'image'.$this->type;
$imgfn($this->img, $dst_path);
imagedestroy($wtImg);
}
//文字水印
private function ttf_mark($dst_path, $ttf){//源图片,目标图片保存路径,字体文件
imagettftext($this->img, $this->fs, $this->fa, 20, 20, imagecolorallocate($this->img, $this->fc[0], $this->fc[1], $this->fc[2]), $ttf, $this->str);
$imgfn = 'image'.$this->type;
$imgfn($this->img, $dst_path);
}
function __destruct(){imagedestroy($this->img);}
}
添加图片水印:new img_mark(源图片路径, 新图片保存路径, '水印图片, 'img_mark');
添加文字水印:new img_mark(源图片路径, 新图片保存路径, '水印字体文件', 'ttf_mark');