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

PHP图片水印类源码:支持文字与图片水印及自定义参数

PHP图片水印类源码:支持文字与图片水印及自定义参数

作者:许澄时间:2026-08-16 10:54:19浏览:749

本文提供PHP实现图片水印功能的完整源代码,支持文字水印和图片水印两种模式。使用文字水印需指定字体文件,使用图片水印则要求水印图尺寸小于原图且背景透明。该类库允许用户自定义水印位置、大小、透明度以及字体颜色等参数,功能完善且易于集成。

本文目录展开

PHP图片水印功能概述

基于PHP开发的水印添加工具,能够同时处理文字水印与图片水印。若采用文字水印方案,必须提供对应的字体文件;若采用图片水印方案,则需提供水印图片素材,且该素材尺寸不得大于待处理的原始图片,建议选用背景透明的图片以获得更佳效果。

此水印类库具备高度的可配置性,支持调整水印在图片中的具体位置、缩放比例及透明度设置。对于文字水印,还可进一步自定义字体颜色等视觉属性,各项功能均已实现并经过完善。

以下是完整的源代码实现,代码注释中已包含具体的调用示例:


<?php
/**
 * 图片加水印类,支持文字水印、透明度设置、自定义水印位置等。
 * 使用示例:
 *   $obj = new WaterMask($imgFileName);  //实例化对象
 *   $obj->$waterType = 1;   //类型:0为文字水印、1为图片水印
 *   $obj->$transparent = 45;   //水印透明度
 *   $obj->$waterStr = 'www.jb51.net';  //水印文字
 *   $obj->$fontSize = 18;   //文字字体大小
 *   $obj->$fontColor = array(255,255,255);  //水印文字颜色(RGB)
 *   $obj->$fontFile = 'AHGBold.ttf';  //字体文件
 * ……
 *   $obj->output();    //输出水印图片文件覆盖到输入的图片文件
 */
class WaterMask{
  public $waterType     = 0;   //水印类型:0为文字水印、1为图片水印
  public $pos        = 0;   //水印位置
  public $transparent    = 45;   //水印透明度
  public $waterStr      = 'www.jb51.net';  //水印文字
  public $fontSize      = 18;   //文字字体大小
  public $fontColor     = array(255,255,255);  //水印文字颜色(RGB)
  public $fontFile      = 'AHGBold.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中的数组由键值对构成,主要分为索引数组和关联数组两类。索引数组使用整数作为键,可通过中括号直接赋值、array()配合=>符号或省略键名自动从0开始计数三种方式创建。关联数组则使用字符串作为键,同样支持中括号赋值和array()定义键值对的方式。掌握这两种数组的赋值技巧是处理数据结构的基础。

下一篇
ThinkPHP 3 CURD操作与查询方式详解

本文介绍ThinkPHP 3框架中数据操作的CRUD方法及多种查询策略。通过Model类实例化或M函数,可实现数据的读取、添加、删除和更新,并明确各方法的返回值类型。查询方面涵盖普通字符串与数组条件、EQ/LT等表达式查询、BETWEEN/IN区间逻辑以及count/max等统计函数,最后提供query和execute原生SQL执行接口。

相关文章

更多
PHP实现根据图片色阶自动调整水印位置的方法

针对固定位置加水印易与背景融合导致不明显的问题,介绍一种基于图片色阶差异自动识别最佳位置的PHP方案。该方法通过检测图像色调分布,将水印置于对比度最高的区域以提升可见性。同时补充了使用MagickWand模块调用ImageMagick库进行高级图像处理的安装配置及代码示例。

2025.05.04 873
PHP Image类实现等比缩略图与右下角水印功能

本文介绍一个PHP Image类,用于在上传图片时自动生成缩略图并添加水印。核心逻辑通过比较新图与原图的宽高比确定缩放比率,确保不出现黑边且保持比例。水印默认置于右下角,若目标图较小则先等比缩小水印。该类支持JPG质量控制,但无法处理GIF动画帧。

2025.09.22 784
PHP实现图片上传时添加文字和图片水印技巧

本文介绍在PHP上传图片过程中添加文字和水印的实用技巧,适用于商城及图片站。通过自定义water类处理图像资源,支持透明背景、抗锯齿等参数配置,确保水印清晰且不影响原图质量。

2025.01.15 668
PHP水印类实现:支持图片、文字及颜色区域填充

该PHP水印类库支持为图像添加图片水印、文字水印以及填充颜色区域。通过实例化WaterMask对象,可配置水印类型、位置、透明度、字体文件及矩形绘制开关等参数,最终输出带有指定样式的水印图片。

2026.08.25 448
PHP UEditor 上传图片自动添加水印的实现方法

在使用 UEditor 编辑器进行文章编辑时,可通过修改 PHP 源码实现上传图片自动添加水印。核心步骤是在 Uploader.class.php 的 upFile 方法中插入自定义函数调用,并定义基于 GD 库的水印处理逻辑。该方法支持指定水印位置及图片路径,使用前需确保服务器已安装 GD 扩展模块。

2026.09.20 389

专题推荐

更多
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 文本,也可将编码内容还原为文件,方便接口传输与调试。