首页 > php学习 > 还是比较牛逼的php增加水印类

还是比较牛逼的php增加水印类

2014年11月12日 发表评论 阅读评论

<?php
class water{
public $waterType=1;//水印类型:0为文字水印、1为图片水印
public $position=9;//-1随机,1上左,2上中,3上右,4中左,5中中,6中右,7下左,8下中,9下右,默认9
public $transparent=100;//水印透明度
public $waterImg=”;//水印图片
public $waterStr=”;//水印文字
public $fontSize=12;//文字字体大小,默认为12
public $fontColor=array(0,0,0);//水印文字颜色(RGB),默认为黑色:array(0,0,0)
public $fontFile=”;//字体文件,建议使用绝对路径,默认可以为空
public $srcImg=”;//需要添加水印的图片
private $im=”;//图片句柄
private $water_im=”;//水印图片句柄
private $srcImg_info=”;//源图片信息
private $waterImg_info=”;//水印图片信息
private $str_w=”;//水印文字宽度
private $str_h=”;//水印文字高度
private $x=”;//水印X坐标
private $y=”;//水印y坐标
function __construct(){}
private function imginfo(){//获取需要添加水印的图片的信息,并载入图片。
$this->srcImg_info=getimagesize($this->srcImg);
switch($this->srcImg_info[2]){
case 3:$this->im=imagecreatefrompng($this->srcImg);break;
case 2:$this->im=imagecreatefromjpeg($this->srcImg);break;
case 1:$this->im=imagecreatefromgif($this->srcImg);break;
default:die(‘源图片格式不对,只支持png/jpeg/gif/jpg.’);break;
}
}
private function waterimginfo(){//获取水印图片的信息,并载入图片。
$this->waterImg_info=getimagesize($this->waterImg);
switch($this->waterImg_info[2]){
case 3:$this->water_im=imagecreatefrompng($this->waterImg);break;
case 2:$this->water_im=imagecreatefromjpeg($this->waterImg);break;
case 1:$this->water_im=imagecreatefromgif($this->waterImg);break;
default:die(‘水印图片格式不对,只支持png/jpeg/gif/jpg.’);break;
}
}
private function waterpos(){//-1随机,1上左,2上中,3上右,4中左,5中中,6中右,7下左,8下中,9下右
switch ($this->position){
case -1:$this->x=rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);$this->y=rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);break;
case 1:$this->x=0;$this->y=0;break;
case 2:$this->x=($this->srcImg_info[0]-$this->waterImg_info[0])/2;$this->y=0;break;
case 3:$this->x=$this->srcImg_info[0]-$this->waterImg_info[0];$this->y=0;break;
case 4:$this->x=0;$this->y=($this->srcImg_info[1]-$this->waterImg_info[1])/2;break;
case 5:$this->x=($this->srcImg_info[0]-$this->waterImg_info[0])/2;$this->y=($this->srcImg_info[1]-$this->waterImg_info[1])/2;break;
case 6:$this->x=$this->srcImg_info[0]-$this->waterImg_info[0];$this->y=($this->srcImg_info[1]-$this->waterImg_info[1])/2;break;
case 7:$this->x=0;$this->y=$this->srcImg_info[1]-$this->waterImg_info[1];break;
case 8:$this->x=($this->srcImg_info[0]-$this->waterImg_info[0])/2;$this->y=$this->srcImg_info[1]-$this->waterImg_info[1];break;
default:$this->x=$this->srcImg_info[0]-$this->waterImg_info[0];$this->y=$this->srcImg_info[1]-$this->waterImg_info[1];break;
}
}
private function waterimg(){
if($this->srcImg_info[0]<=$this->waterImg_info[0] || $this->srcImg_info[1]<=$this->waterImg_info[1]){return false;}
$this->waterpos();
$cut=imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$this->transparent);
}
private function waterstr(){
$rect=imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
$w=abs($rect[2]-$rect[6]);
$h=abs($rect[3]-$rect[7]);
$fontHeight=$this->fontSize;
$this->water_im=imagecreatetruecolor($w,$h);
imagealphablending($this->water_im,false);
imagesavealpha($this->water_im,true);
$white_alpha=imagecolorallocatealpha($this->water_im,255,255,255,127);
imagefill($this->water_im,0,0,$white_alpha);
$color=imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
$this->waterImg_info=array(0=>$w,1=>$h);$this->waterimg();
}
function initImg($c_srcimg,$c_waterimg,$c_position,$c_transparent){//图片水印效果
$this->srcImg=file_exists($c_srcimg)?$c_srcimg:die(‘源文件不存在!’);
if($c_waterimg) $this->waterImg=$c_waterimg;
else die(‘请指定水印文件!’);
$this->waterType=1;
if($c_position) $this->position=$c_position;
if($c_transparent) $this->transparent=$c_transparent;
$this->outputWater();
}
function initText($c_srcimg,$c_fontText,$c_fontSize,$c_fontColor,$c_fontFamilyPath,$c_positioin,$c_transparent){//文字水印效果
$this->srcImg=file_exists($c_srcimg)?$c_srcimg:die(‘源文件不存在!’);
if($c_fontText) $this->waterStr=$c_fontText;
else die(‘请指定水印文字内容!’);
if($fc_ontSize) $this->fontSize=$fc_ontSize;
if($c_fontColor) $this->fontColor=$c_fontColor;
if($c_fontFamilyPath) $this->fontFile=$c_fontFamilyPath;
if($c_positioin) $this->position=$c_positioin;
if($c_transparent) $this->transparent=$c_transparent;
$this->waterType=0;$this->outputWater();
}
function outputWater(){
$this->imginfo();
if($this->waterType==0){$this->waterstr();}
else{$this->waterimginfo();$this->waterimg();}
switch($this->srcImg_info[2]){
case 3:imagepng($this->im,$this->srcImg);break;
case 2:imagejpeg($this->im,$this->srcImg);break;
case 1:imagegif($this->im,$this->srcImg);break;
default:die(‘添加水印失败!’);break;
}
imagedestroy($this->im);imagedestroy($this->water_im);
}
}

使用方法如下:
$water=new water();
//原图,水印文件,位置,水印图片透明度
$water->initImg(‘test.jpg’,’water2.png’,9,100);
//原图,文字,文字大小,文字颜色,字体位置(使用绝对路径,不可为空),文字水印位置,透明度
$water->initText(‘test.jpg’,’Hello,world!’,16,array(0,0,0),’1.ttf’,2,100);
?>

分类: php学习 标签: , ,
  1. 本文目前尚无任何评论.
您必须在 登录 后才能发布评论.
css.php