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

adminZpd 专业教程

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

PHP图片水印类代码如何实现自定义文字与图片水印?-第1张图片-99系统专家

水印类的基本结构

一个完整的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图片水印类代码如何实现自定义文字与图片水印?-第2张图片-99系统专家

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));
}

抱歉,评论功能暂时关闭!