php实用图片水印效果代码
define('Water',1);//水印方式(0,文字水印,1图片水印)
define('WaterImg','./water.jpg');//水印图片地址
define('WaterNum',0);//水印位置,0为随机,1-9分别为顶左顶中顶右中左中中中右底左底中底右
define('WaterX',81);//水印长
define('WaterY',81);//水印高
define('WaterType','image/jpeg');//水印图片类型
define('WaterText','爱搜罗');//水印文字(必须先转换成UTF-8才能实现中文水印)
define('WaterTextColor','#000000');//水印文字颜色
define('WaterTextSize',50);//水印文字大小 px
define('WaterTextFont','C:WINDOWSFontsSIMFANG.TTF');//水印文字字体 (仿宋)
define('WaterPtc',50);//水印透明度
//////////////////////水印默认设置完毕
class water
{
private $img;//图片地址
private $img_type;//图片类型
//private $img_mime;//图片实际类型
private $img_x;//图片长
private $img_y;//图片高
private $is_water;//是否可以添加水印,布尔
private $water_img;//水印图片
private $water_text;//图片文字(UTF-8)
private $water_num;//水印位置(可能用不着)
private $water_w = WaterX;//water的宽
private $water_h = WaterY ;//water的长
private $water_x;//水印的x位置
private $water_y;//水印的y位置
private $src_x;//画布与water两图的长度差
private $src_y;//画布与water两图的高度差
private $err_msg;//图片地址
public function __construct($img,$img_type)
{
if (file_exists($img) == FALSE)
{
$this->Err_Img('No_Img');
}
$this->img = $img;
$this->img_type = $img_type;//写入图片类型
$this->Check_GD();//检查GD库
$this->Do_Water();//判断水印方式
$this->Type_Img();//判断图片类型
}
/*
* 检查GD库
*/
public function Check_GD(){
if (function_exists("gd_info") == FALSE)
{
$this->Err_Img('No_Gd');
}
}
/*
* 处理水印方式及water文件类型
*/
public function Do_Water()
{
if (Water == 1)//图片水印
{
$this->Img_Water();//处理water图片
}else{//文字水印
$this->Text_xy();//处理文字水印图片
}
}
/*
* 处理water图片
*/
public function Img_Water()
{
if (file_exists(WaterImg) == FALSE)
{
$this->Err_Img('No_WaterImg');
//使用文字水印
}
switch (WaterType)
{
case 'image/jpeg':
$this->water_img = imagecreatefromjpeg(WaterImg);
break;
}
}
/*
* 获得文字的长宽 并 计算 两图的长宽差
*/
public function Text_xy()
{
$str = iconv('GB2312','UTF-8',WaterText);
$temp = imagettfbbox(50,0,WaterTextFont,$str);//取得使用 TrueType 字体的文本的范围
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
$this->src_x = $this->img_x - $w;
$this->src_y = $this->img_y - $h;
}
/*
* 判断图片是否能够添加水印,比较大小
*/
public function Is_Water()
{
if (Water == 1)//图片水印
{
$this->src_x = $this->img_x - WaterX;
$this->src_y = $this->img_y - WaterY;
if ($this->src_x src_y
{
$this->Err_Img('Too_Small');
}
}else{//文字水印
if ($this->src_x src_y
{
$this->Err_Img('Too_Small');
}
}
}
/*
* 处理水印位置
* 得到 水印位置的坐标
*/
public function Water_Num()
{
switch(WaterNum)
{
case 0://随机
$this->water_x = rand(0,($this->img_x - $this->water_w));
$this->water_y = rand(0,($this->img_y - $this->water_h));
break;
case 1://顶端居左
$this->water_x = 0;
$this->water_y = 0;
break;
case 2://顶端居中
$this->water_x = ($this->img_x - $this->water_w)/2;
$this->water_y = 0;
break;
case 3://顶端居右
$this->water_x = $this->img_x - $this->water_w;
$this->water_y = 0;
break;
case 4://中间居左
$this->water_x = 0;
$this->water_y = ($this->img_y - $this->water_h)/2;
break;
case 5://中间居中
$this->water_x = ($this->img_x - $this->water_w)/2;
$this->water_y = ($this->img_y - $this->water_h)/2;
break;
case 6://中间居右
$this->water_x = $this->img_x - $this->water_w;
$this->water_y = ($this->img_y - $this->water_h)/2;
break;
case 7://底部居左
$this->water_x = 0;
$this->water_y = $this->img_y - $this->water_h;
break;
case 8://底部居中
$this->water_x = ($this->img_x - $this->water_w)/2;
$this->water_y = $this->img_y - $this->water_h;
break;
case 9://底部居右
$this->water_x = $this->img_x - $this->water_w;
$this->water_y = $this->img_y - $this->water_h;
break;
default://随机
$this->water_x = rand(0,($this->img_x - $this->water_w));
$this->water_y = rand(0,($this->img_y - $this->water_h));
}
}
/*
* 判断图片类型
*/
public function Type_Img()
{
switch ($this->img_type)
{
case 'image/jpeg':
header("Content-type: image/jpeg");
$this->img = imagecreatefromjpeg($this->img);
break;
}
$this->Img_Water();//处理water图片
$this->img_x = imagesx($this->img);
$this->img_y = imagesy($this->img);
$this->Water_Num();//判断水印位置
$this->Is_Water();
$this->Make_img();
}
/*
* 制作图片水印
*/
public function Make_img(){
imagecopymerge($this->img,$this->water_img,$this->water_x,$this->water_y,0,0,WaterX,WaterY,WaterPtc);
imagejpeg($this->img);
}
/*
* 制作文字水印
*/
public function Make_Text()
{
echo '测试';
}
/*
* 返回错误信息
*/
public function Err_Img($msg)
{
switch($msg)
{
case 'No_Gd':
$this->err_msg = '没有安装GD库';
break;
case 'No_Img':
$this->err_msg = '图片不存在';
break;
case 'No_WaterImg':
$this->err_msg = 'water图片不存在';
break;
case 'Too_Small':
$this->err_msg = '图片过小';
break;
}
die($this->err_msg);//输出错误信息
}
public function __destruct()
{
imagedestroy($this->img);
imagedestroy($this->img_water);
}
}
$img = new water('./100.jpg','image/jpeg');//实例化
?>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전
