0

0

PHP给图片增多水印得类

php中文网

php中文网

发布时间:2016-06-13 13:16:23

|

1029人浏览过

|

来源于php中文网

原创

PHP给图片增加水印得类

src_image_name = strtolower(trim($filename));

$src_image_type = $this->get_type($this->src_image_name);
$src_image = $this->createImage($src_image_type,$this->src_image_name);
if (!$src_image) return;
$src_image_w=ImageSX($src_image);
$src_image_h=ImageSY($src_image);


if ($this->wm_image_name){
$this->wm_image_name = strtolower(trim($this->wm_image_name));
$wm_image_type = $this->get_type($this->wm_image_name);
$wm_image = $this->createImage($wm_image_type,$this->wm_image_name);
$wm_image_w=ImageSX($wm_image);
$wm_image_h=ImageSY($wm_image);
$temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);
$wm_image_x = $temp_wm_image["dest_x"];
$wm_image_y = $temp_wm_image["dest_y"];
imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);
}

if ($this->wm_text){
$this->wm_text = $this->gb2utf8($this->wm_text);
$temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);
$wm_text_x = $temp_wm_text["dest_x"];
$wm_text_y = $temp_wm_text["dest_y"];
if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color))
{
$red = hexdec($color[1]);
$green = hexdec($color[2]);
$blue = hexdec($color[3]);
$wm_text_color = imagecolorallocate($src_image, $red,$green,$blue);
}else{
$wm_text_color = imagecolorallocate($src_image, 255,255,255);
}

imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color,$this->wm_text_font,$this->wm_text);
}

if ($this->save_file)
{
switch ($this->output_type){
case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;
case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;
default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
}
}
else
{
if ($src_image_type = "jpg") $src_image_type="jpeg";
header("Content-type: image/{$src_image_type}");
switch ($src_image_type){
case 'gif':$src_img=ImagePNG($src_image); break;
case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
case 'png':$src_img=ImagePNG($src_image);break;
default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
}
}
imagedestroy($src_image);
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
createImage 根据文件名和类型创建图片
内部函数

$type: 图片的类型,包括gif,jpg,png
$img_name:图片文件名,包括路径名,例如 " ./mouse.jpg"
*/
function createImage($type,$img_name){
if (!$type){
$type = $this->get_type($img_name);
}

switch ($type){
case 'gif':
if (function_exists('imagecreatefromgif'))
$tmp_img=@ImageCreateFromGIF($img_name);
break;
case 'jpg':
$tmp_img=ImageCreateFromJPEG($img_name);
break;
case 'png':
$tmp_img=ImageCreateFromPNG($img_name);
break;
default:
$tmp_img=ImageCreateFromString($img_name);
break;
}
return $tmp_img;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
getPos 根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置
内部函数

$sourcefile_width: 源图像的宽
$sourcefile_height: 原图像的高
$pos: 位置代码
// 0 = middle
// 1 = top left
// 2 = top right
// 3 = bottom right
// 4 = bottom left
// 5 = top middle
// 6 = middle right
// 7 = bottom middle
// 8 = middle left
$wm_image: 水印图片ID
*/
function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){
if($wm_image){
$insertfile_width = ImageSx($wm_image);
$insertfile_height = ImageSy($wm_image);
}else {
$lineCount = explode("\r\n",$this->wm_text);
$fontSize = imagettfbbox($this->wm_text_size,$this->wm_text_angle,$this->wm_text_font,$this->wm_text);
$insertfile_width = $fontSize[2] - $fontSize[0];
$insertfile_height = count($lineCount)*($fontSize[1] - $fontSize[3]);
}

switch ($pos){
case 0:
$dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );
$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
break;

case 1:
$dest_x = 0;
if ($this->wm_text){
$dest_y = $insertfile_height;
}else{
$dest_y = 0;
}
break;

case 2:
$dest_x = $sourcefile_width - $insertfile_width;
if ($this->wm_text){
$dest_y = $insertfile_height;
}else{
$dest_y = 0;
}
break;

case 3:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = $sourcefile_height - $insertfile_height;
break;

case 4:
$dest_x = 0;
$dest_y = $sourcefile_height - $insertfile_height;
break;

case 5:
$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
if ($this->wm_text){
$dest_y = $insertfile_height;
}else{
$dest_y = 0;
}
break;

case 6:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
break;

case 7:
$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
$dest_y = $sourcefile_height - $insertfile_height;
break;

case 8:
$dest_x = 0;
$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
break;

default:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = $sourcefile_height - $insertfile_height;
break;
}
return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
gb2utf8 指定的文字转换为UTF-8格式,包括中英文混合
内部函数
*/
function gb2utf8($gb)
{
if(!trim($gb))
return $gb;
$filename="./gb2312.txt";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);

$utf8="";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$tthis=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb)-2);
$utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));
}
else
{
$tthis=substr($gb,0,1);
$gb=substr($gb,1,strlen($gb)-1);
$utf8.=$this->u2utf8($tthis);
}
}

return $utf8;
}

function u2utf8($c)
{
$str="";
if ($c < 0x80)
{
$str.=$c;
}
else if ($c < 0x800)
{
$str.=chr(0xC0 | $c>>6);
$str.=chr(0x80 | $c & 0x3F);
}
else if ($c < 0x10000)
{
$str.=chr(0xE0 | $c>>12);
$str.=chr(0x80 | $c>>6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
}
else if ($c < 0x200000)
{
$str.=chr(0xF0 | $c>>18);
$str.=chr(0x80 | $c>>12 & 0x3F);
$str.=chr(0x80 | $c>>6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
}
return $str;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
get_type 获得图片的格式,包括jpg,png,gif
内部函数

$img_name: 图片文件名,可以包括路径名
*/
function get_type($img_name)//获取图像文件类型
{
$name_array = explode(".",$img_name);
if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches))
{
$type = strtolower($matches[1]);
}
else
{
$type = "string";
}
return $type;
}

}

?>



使用方法:
$img = new Gimage();
$img->wm_text = "www.discuz.com";
$img->wm_text_font = "./STXINWEI.TTF";
$img->create("./mouse.jpg");

就可以了,其中
mouse.jpg是你要在其上添加水印的图片名称,注意包含路径名
STXINWEI.TTF是字体文件的路径名+文件名
这就是一个简单的测试。如果要调整更复杂的显示效果,只要修改一下类中的属性就可以了,例如把字体放大就可以
$img->wm_text_size = 20;
增加水印图片就可以
$img->wm_image_name="文件名";

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

14

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

12

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

4

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

18

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

19

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

3

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
JavaScript高级框架设计视频教程
JavaScript高级框架设计视频教程

共22课时 | 3.6万人学习

ASP教程
ASP教程

共36课时 | 23.8万人学习

李炎恢ASP基础视频教程
李炎恢ASP基础视频教程

共210课时 | 43.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号