当前位置: 首页 > 编程文章 >

PHP 图片水印类代码

PHP 图片水印类代码

作者:苏宁时间:2025-01-25 13:31:34浏览:728

该 PHP 类封装了 GD 库功能,支持为 PNG、JPEG、GIF 格式图片添加文字或图片水印。核心特性包括自定义水印位置(九宫格或随机)、调整透明度以及配置字体样式。通过实例化对象并设置属性,可一键生成带水印的图片文件。

本文目录展开

PHP 图片水印类功能介绍

本工具具备为图像添加文字标记或图形标识的能力。


用户可指定水印的放置区域,既可选择固定坐标(如九宫格布局),也可设为随机分布。


无论是叠加的文字还是图片,均允许调节其视觉透明程度。


针对文字类型的水印,提供对字体文件、字符颜色及字号大小的自定义选项。


若使用图片作为水印,该类能自动处理背景透明效果,确保融合自然。


具体实现逻辑如下所示:


<?php
/**
这是一个用于添加水印的类,能够处理文字和图片水印的透明度调整,并支持水印图片背景的透明化。
最后更新日期:2011-09-27
来源作者:www.jb51.net
基本使用方法说明:
* $obj = new WaterMask($imgFileName); // 创建对象实例
* $obj->$waterType = 1; // 设定类型:0 代表文字水印,1 代表图片水印
* $obj->$transparent = 45; // 设定水印的透明度数值
* $obj->$waterStr = 'www.jb51.net'; // 定义要显示的水印文字内容
* $obj->$fontSize = 16; // 设定文字的大小尺寸
* $obj->$fontColor = array(255,0255); // 设定水印文字的颜色值(RGB 格式)
* $obj->$fontFile = = 'AHGBold.ttf'; // 指定使用的字体文件路径
* $obj->output(); // 执行输出,将带有水印的新图片覆盖保存至原文件路径
*/
class WaterMask{
    public $waterType = 1; // 水印模式:0 为文字,1 为图片
    public $pos = 0; // 水印在图中的位置索引
    public $transparent = 45; // 水印整体的不透明度参数
    public $waterStr = 'www.jb51.net'; // 当类型为文字时显示的字符串
    public $fontSize = 16; // 文字渲染的高度像素值
    public $fontColor = array(255,0,255); // 文字颜色的 RGB 数组
    public $fontFile = 'AHGBold.ttf'; // TTF 字体文件名称
    public $waterImg = 'logo.png'; // 用作水印的图片文件名
    private $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($img) { // 构造函数
        $this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
    }
    private function imginfo() { // 读取源图片信息并加载到内存
        $this->srcImg_info = getimagesize($this->srcImg);
        switch ($this->srcImg_info[2]) {
            case 3:
            $this->im = imagecreatefrompng($this->srcImg);
            break 1;
            case 2:
            $this->im = imagecreatefromjpeg($this->srcImg);
            break 1;
            case 1:
            $this->im = imagecreatefromgif($this->srcImg);
            break 1;
            default:
            die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
        }
    }
    private function waterimginfo() { // 读取水印图片信息并加载
        $this->waterImg_info = getimagesize($this->waterImg);
        switch ($this->waterImg_info[2]) {
            case 3:
            $this->water_im = imagecreatefrompng($this->waterImg);
            break 1;
            case 2:
            $this->water_im = imagecreatefromjpeg($this->waterImg);
            break 1;
            case 1:
            $this->water_im = imagecreatefromgif($this->waterImg);
            break 1;
            default:
            die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
        }
    }
    private function waterpos() { // 计算水印放置坐标的逻辑
        switch ($this->pos) {
            case 0: // 随机位置
            $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 1;
            case 1: // 左上角
            $this->x = 0;
            $this->y = 0;
            break 1;
            case 2: // 上中
            $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
            $this->y = 0;
            break 1;
            case 3: // 右上角
            $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
            $this->y = 0;
            break 1;
            case 4: // 左中
            $this->x = 0;
            $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
            break 1;
            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 1;
            case 6: // 右中
            $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
            $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
            break 1;
            case 7: // 左下角
            $this->x = 0;
            $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
            break 1;
            case 8: // 下中
            $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
            $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
            break 1;
            default: // 右下角
            $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
            $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
            break 1;
        }
    }
    private function waterimg() {
        if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){
            die('水印比原图大!');
        }
        $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]);
        $pct = $this->transparent;
        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],$pct);
    }
    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 output() {
        $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 1;
            case 2:
            imagejpeg($this->im,$this->srcImg);
            break 1;
            case 1:
            imagegif($this->im,$this->srcImg);
            break 1;
            default:
            die('添加水印失败!');
            break;
        }
        imagedestroy($this->im);
        imagedestroy($this->water_im);
    }
}
?>

上一篇
PHP内置字符串处理函数详解

本文介绍PHP中常用的内置字符串处理函数。包括strtolower、strtoupper等大小写转换函数,以及nl2br、htmlspecialchars等HTML标签处理函数。所有字符串处理函数均不修改原字符串,而是返回新格式化后的字符串。

下一篇
Discuz与EcShop字符串截取函数对比及优化方案

本文对比了Discuz和EcShop两个知名PHP项目中字符串截取函数的实现逻辑。Discuz的cutstr函数在未安装mbstring扩展时,通过字节位判断处理UTF-8字符,但存在将中文按2字节计算导致的截断Bug;EcShop的sub_str依赖mb_substr或iconv_substr,以字符为单位计数,虽解决了中文乱码问题,但在混合中英文场景下无法区分字符权重。文章最后提供了一种兼顾GBK/UTF-8编码且支持中英文不同权重的优化版截取函数。

相关文章

更多
PHP图像处理核心函数与操作指南

本文梳理了PHP中基于GD库的核心图像处理功能,涵盖从资源创建到最终输出的完整流程。重点解析了imagecreatetruecolor等创建方法、getimagesize获取宽高及类型标记、PNG/JPEG/GIF的透明色差异处理,以及imagecopyresampled进行高质量裁剪、imagerotate实现旋转和翻转,并介绍了添加文字或图片水印时的编码转换技巧。

2026.06.16 805
PHP实现图片水印的两种方法

本文介绍利用PHP为图片添加水印的两种常用方式。第一种方法通过直接合并图片资源实现基础水印效果,操作简便;第二种方法则利用GD库函数在图片上绘制文字,适用于自定义文字水印的场景。掌握这两种技术有助于防止图片被盗用并提升内容版权保护能力。

2026.04.13 622
PHP图片水印类封装实现

本文介绍了一种PHP图片水印类的封装方法,旨在提供便捷的图像处理功能。通过该类,开发者可以轻松地为图片添加水印效果,提升图片版权保护能力。代码结构清晰,便于集成到现有项目中。

2025.07.27 548
PHP使用GD库实现文字水印与图片水印的代码示例

本文介绍利用PHP的GD库在图像上添加水印的两种方法。文字水印通过imagefttext函数配合字体文件实现,可自定义颜色、大小及位置;图片水印则使用imagecopymerge或imagecopy函数将源图叠加至目标图,支持透明度控制。代码涵盖从加载图片、执行绘制到按类型输出及资源释放的完整流程。

2026.05.09 448
PHP图片处理类实现文字水印、缩略图及图片水印示例

本文介绍一个名为ImageToTest的PHP类,用于实现图片的文字水印、缩略图裁剪及图片叠加水印功能。该类通过构造函数加载图片资源,提供thumb方法调整尺寸,fontMark方法添加自定义字体水印,以及imageMark方法合成其他图片作为水印。测试代码展示了如何实例化该类并调用相应方法完成图片编辑操作。

2026.02.21 972
PHP图片添加水印实现示例

本文提供一段PHP实现图片添加水印的代码示例,旨在帮助开发者快速掌握相关技术。通过具体的实例演示,展示如何在图片上叠加水印信息,适用于各类Web应用中的图像版权保护或品牌标识需求。

2025.02.11 848

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。