PHP图片水印类代码如何实现自定义文字与图片水印?

PHP图片水印类代码是开发中常用的功能,主要用于在图片上添加文字或图片水印,以保护版权或标识来源,下面将详细介绍如何实现一个功能完善的PHP图片水印类,包括类的设计、核心方法、使用示例以及注意事项。

PHP图片水印类代码如何实现自定义文字与图片水印?

水印类的基本结构

一个完整的PHP图片水印类应包含以下核心功能:支持文字水印和图片水印、支持多种水印位置(如左上、右上、居中、左下、右下等)、支持透明度调整、支持水印大小自适应等,以下是类的基本框架:

class ImageWatermark {
    private $sourceImage; // 源图片资源
    private $watermarkImage; // 水印图片资源(如果是图片水印)
    private $watermarkText; // 水印文字(如果是文字水印)
    private $fontSize; // 字体大小
    private $fontColor; // 字体颜色
    private $watermarkPosition; // 水印位置
    private $watermarkOpacity; // 水印透明度
    public function __construct($sourceImagePath) {
        // 初始化源图片
        $this->sourceImage = $this->createImageResource($sourceImagePath);
    }
    // 其他方法...
}

核心方法实现

创建图片资源

private function createImageResource($imagePath) {
    $imageInfo = getimagesize($imagePath);
    switch ($imageInfo[2]) {
        case IMAGETYPE_JPEG:
            return imagecreatefromjpeg($imagePath);
        case IMAGETYPE_PNG:
            return imagecreatefrompng($imagePath);
        case IMAGETYPE_GIF:
            return imagecreatefromgif($imagePath);
        default:
            throw new Exception("Unsupported image type");
    }
}

添加文字水印

public function setTextWatermark($text, $fontSize = 16, $color = '#000000') {
    $this->watermarkText = $text;
    $this->fontSize = $fontSize;
    $this->fontColor = $this->hexToRgb($color);
}
private function hexToRgb($hexColor) {
    $hexColor = ltrim($hexColor, '#');
    return [
        'r' => hexdec(substr($hexColor, 0, 2)),
        'g' => hexdec(substr($hexColor, 1, 2)),
        'b' => hexdec(substr($hexColor, 2, 2))
    ];
}

添加图片水印

public function setImageWatermark($watermarkImagePath) {
    $this->watermarkImage = $this->createImageResource($watermarkImagePath);
}

设置水印位置和透明度

public function setPosition($position = 'bottom-right') {
    $this->watermarkPosition = $position;
}
public function setOpacity($opacity = 50) {
    $this->watermarkOpacity = min(max($opacity, 0), 100);
}

应用水印并保存

public function applyWatermark($outputPath, $quality = 90) {
    $sourceWidth = imagesx($this->sourceImage);
    $sourceHeight = imagesy($this->sourceImage);
    if ($this->watermarkText) {
        // 文字水印逻辑
        $fontFile = 'arial.ttf'; // 确保字体文件存在
        $textWidth = imagettfbbox($this->fontSize, 0, $fontFile, $this->watermarkText)[2];
        $textHeight = imagettfbbox($this->fontSize, 0, $fontFile, $this->watermarkText)[5];
        $position = $this->calculatePosition($sourceWidth, $sourceHeight, $textWidth, $textHeight);
        $color = imagecolorallocatealpha($this->sourceImage, 
            $this->fontColor['r'], 
            $this->fontColor['g'], 
            $this->fontColor['b'], 
            127 ($this->watermarkOpacity * 127 / 100)
        );
        imagettftext($this->sourceImage, $this->fontSize, 0, $position['x'], $position['y'], $color, $fontFile, $this->watermarkText);
    } elseif ($this->watermarkImage) {
        // 图片水印逻辑
        $watermarkWidth = imagesx($this->watermarkImage);
        $watermarkHeight = imagesy($this->watermarkImage);
        $position = $this->calculatePosition($sourceWidth, $sourceHeight, $watermarkWidth, $watermarkHeight);
        imagecopymerge($this->sourceImage, $this->watermarkImage, 
            $position['x'], $position['y'], 0, 0, 
            $watermarkWidth, $watermarkHeight, 
            $this->watermarkOpacity
        );
    }
    // 保存图片
    switch (pathinfo($outputPath, PATHINFO_EXTENSION)) {
        case 'jpg':
        case 'jpeg':
            imagejpeg($this->sourceImage, $outputPath, $quality);
            break;
        case 'png':
            imagepng($this->sourceImage, $outputPath, round($quality / 11));
            break;
        case 'gif':
            imagegif($this->sourceImage, $outputPath);
            break;
    }
    imagedestroy($this->sourceImage);
    if ($this->watermarkImage) {
        imagedestroy($this->watermarkImage);
    }
}
private function calculatePosition($sourceWidth, $sourceHeight, $watermarkWidth, $watermarkHeight) {
    $margin = 10;
    switch ($this->watermarkPosition) {
        case 'top-left':
            return ['x' => $margin, 'y' => $margin];
        case 'top-right':
            return ['x' => $sourceWidth $watermarkWidth $margin, 'y' => $margin];
        case 'bottom-left':
            return ['x' => $margin, 'y' => $sourceHeight $watermarkHeight $margin];
        case 'bottom-right':
            return ['x' => $sourceWidth $watermarkWidth $margin, 'y' => $sourceHeight $watermarkHeight $margin];
        case 'center':
            return ['x' => ($sourceWidth $watermarkWidth) / 2, 'y' => ($sourceHeight $watermarkHeight) / 2];
        default:
            return ['x' => $sourceWidth $watermarkWidth $margin, 'y' => $sourceHeight $watermarkHeight $margin];
    }
}

使用示例

try {
    $watermark = new ImageWatermark('source.jpg');
    $watermark->setTextWatermark('© 2025 My Company', 20, '#ffffff');
    $watermark->setPosition('bottom-right');
    $watermark->setOpacity(70);
    $watermark->applyWatermark('output.jpg');
    echo "Watermark applied successfully!";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

注意事项

  1. GD库支持:确保PHP已启用GD库,可通过phpinfo()检查。
  2. 字体文件:使用文字水印时需提供有效的字体文件路径(如.ttf)。
  3. 图片格式:支持JPEG、PNG和GIF格式,但GIF的透明度处理可能不完美。
  4. 性能考虑:大图片处理时建议先压缩再添加水印。

相关问答FAQs

Q1:如何为水印添加倾斜效果?
A:可以在imagettftext()函数中使用$angle参数实现文字倾斜,例如imagettftext($image, $fontSize, 45, $x, $y, $color, $fontFile, $text)中的45表示45度倾斜。

PHP图片水印类代码如何实现自定义文字与图片水印?

Q2:水印类如何支持批量处理多张图片?
A:可以创建一个循环遍历图片目录,对每张图片实例化ImageWatermark类并调用applyWatermark方法,

$images = glob('images/*.jpg');
foreach ($images as $image) {
    $watermark = new ImageWatermark($image);
    $watermark->setTextWatermark('Batch Processed');
    $watermark->applyWatermark('output/' . basename($image));
}

图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/222618.html

(0)
上一篇2026年1月10日 12:01
下一篇 2026年1月10日 12:04

相关推荐

  • apache多域名301跳转怎么配置多个域名统一跳转?

    在Apache服务器环境中,实现多域名的301跳转是网站管理中常见的需求,无论是域名合并、品牌统一还是SEO优化,301跳转都能有效将旧域名的权重和流量引导至新域名,本文将详细介绍在Apache下实现多域名301跳转的多种方法、配置要点及注意事项,帮助管理员高效完成域名迁移工作,理解301跳转的基础概念301跳……

    2025年10月28日
    0390
  • asp.net下经典数据库记录分页代码的具体实现流程及注意事项有哪些?

    ASP.NET下经典数据库记录分页代码解析分页基础概念与重要性数据库分页是Web应用中处理海量数据的核心技术,其核心思想是将大量数据按需拆分为多个小数据块(页),用户通过导航控件(如“上一页”“下一页”)逐步加载,避免一次性加载过多数据导致的性能瓶颈(如内存溢出、页面加载缓慢),分页不仅提升了用户体验,还降低了……

    2026年1月5日
    0130
    • 服务器间歇性无响应是什么原因?如何排查解决?

      根源分析、排查逻辑与解决方案服务器间歇性无响应是IT运维中常见的复杂问题,指服务器在特定场景下(如高并发时段、特定操作触发时)出现短暂无响应、延迟或服务中断,而非持续性的宕机,这类问题对业务连续性、用户体验和系统稳定性构成直接威胁,需结合多维度因素深入排查与解决,常见原因分析:从硬件到软件的多维溯源服务器间歇性……

      2026年1月10日
      020
  • Angular如何递归显示博客评论并获取回复评论数据?

    递归评论展示的设计思路在博客评论系统中,递归评论展示是指能够嵌套显示回复评论的功能,即每条评论可以包含多条子评论,子评论下还可以继续嵌套,形成树形结构,这种设计需要解决两个核心问题:一是如何在前端界面中递归渲染评论树,二是如何高效获取包含所有层级的评论数据,Angular作为强大的前端框架,其组件化特性和数据绑……

    2025年10月25日
    0340
  • 服务器装360安全吗?会有哪些影响?

    在当今数字化时代,服务器作为企业核心数据存储与业务运行的关键载体,其安全性、稳定性和管理效率直接关系到整体业务 continuity,部分管理员在服务器安全防护的选择上存在认知偏差,例如将个人终端安全软件“360安全卫士”直接部署于服务器环境中,这一做法看似能提供“全方位保护”,实则可能埋下多重隐患,需从技术原……

    2025年12月10日
    0390

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注