当前位置:首页 > PHP

php后台实现水印

2026-02-16 13:30:04PHP

添加水印的基本方法

在PHP中实现图片水印功能主要通过GD库或Imagick扩展完成。以下是两种常见实现方式:

GD库实现方式:

php后台实现水印

function addWatermark($sourceImagePath, $watermarkText, $outputImagePath) {
    $image = imagecreatefromjpeg($sourceImagePath);
    $font = 'arial.ttf';
    $color = imagecolorallocatealpha($image, 255, 255, 255, 70);
    $fontSize = 20;
    $angle = 0;
    $margin = 30;

    $imageWidth = imagesx($image);
    $imageHeight = imagesy($image);
    $textBox = imagettfbbox($fontSize, $angle, $font, $watermarkText);
    $textWidth = $textBox[2] - $textBox[0];
    $textHeight = $textBox[7] - $textBox[1];

    $x = ($imageWidth - $textWidth) / 2;
    $y = ($imageHeight - $textHeight) / 2;

    imagettftext($image, $fontSize, $angle, $x, $y, $color, $font, $watermarkText);
    imagejpeg($image, $outputImagePath);
    imagedestroy($image);
}

使用图片作为水印

如果需要使用图片作为水印而非文字:

function addImageWatermark($sourceImagePath, $watermarkImagePath, $outputImagePath) {
    $sourceImage = imagecreatefromjpeg($sourceImagePath);
    $watermark = imagecreatefrompng($watermarkImagePath);

    $sourceWidth = imagesx($sourceImage);
    $sourceHeight = imagesy($sourceImage);
    $watermarkWidth = imagesx($watermark);
    $watermarkHeight = imagesy($watermark);

    $positionX = ($sourceWidth - $watermarkWidth) / 2;
    $positionY = ($sourceHeight - $watermarkHeight) / 2;

    imagecopy($sourceImage, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight);
    imagejpeg($sourceImage, $outputImagePath);

    imagedestroy($sourceImage);
    imagedestroy($watermark);
}

水印透明度控制

调整水印透明度可以使效果更自然:

php后台实现水印

function addTransparentWatermark($sourceImagePath, $watermarkImagePath, $outputImagePath) {
    $sourceImage = imagecreatefromjpeg($sourceImagePath);
    $watermark = imagecreatefrompng($watermarkImagePath);

    $sourceWidth = imagesx($sourceImage);
    $sourceHeight = imagesy($sourceImage);
    $watermarkWidth = imagesx($watermark);
    $watermarkHeight = imagesy($watermark);

    $positionX = $sourceWidth - $watermarkWidth - 10;
    $positionY = $sourceHeight - $watermarkHeight - 10;

    imagealphablending($watermark, true);
    imagesavealpha($watermark, true);

    imagecopymerge($sourceImage, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight, 50);
    imagejpeg($sourceImage, $outputImagePath);

    imagedestroy($sourceImage);
    imagedestroy($watermark);
}

批量添加水印

处理目录下所有图片的批量水印添加:

function batchAddWatermark($directory, $watermarkText) {
    $files = scandir($directory);
    foreach ($files as $file) {
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        if (in_array($ext, ['jpg', 'jpeg', 'png'])) {
            $sourcePath = $directory . '/' . $file;
            $outputPath = $directory . '/watermarked_' . $file;
            addWatermark($sourcePath, $watermarkText, $outputPath);
        }
    }
}

水印位置调整

通过参数控制水印位置:

function addWatermarkWithPosition($sourceImagePath, $watermarkText, $outputImagePath, $position = 'center') {
    $image = imagecreatefromjpeg($sourceImagePath);
    $font = 'arial.ttf';
    $color = imagecolorallocatealpha($image, 255, 255, 255, 70);
    $fontSize = 20;
    $angle = 0;
    $margin = 30;

    $imageWidth = imagesx($image);
    $imageHeight = imagesy($image);
    $textBox = imagettfbbox($fontSize, $angle, $font, $watermarkText);
    $textWidth = $textBox[2] - $textBox[0];
    $textHeight = $textBox[7] - $textBox[1];

    switch ($position) {
        case 'top-left':
            $x = $margin;
            $y = $margin + $textHeight;
            break;
        case 'top-right':
            $x = $imageWidth - $textWidth - $margin;
            $y = $margin + $textHeight;
            break;
        case 'bottom-left':
            $x = $margin;
            $y = $imageHeight - $margin;
            break;
        case 'bottom-right':
            $x = $imageWidth - $textWidth - $margin;
            $y = $imageHeight - $margin;
            break;
        default: // center
            $x = ($imageWidth - $textWidth) / 2;
            $y = ($imageHeight - $textHeight) / 2;
    }

    imagettftext($image, $fontSize, $angle, $x, $y, $color, $font, $watermarkText);
    imagejpeg($image, $outputImagePath);
    imagedestroy($image);
}

注意事项

确保服务器已安装GD库或Imagick扩展,可通过phpinfo()查看。处理不同格式图片时需使用对应的函数:imagecreatefromjpeg()、imagecreatefrompng()等。输出时同样需要注意图片格式匹配。水印图片建议使用PNG格式以保持透明度。

标签: 水印后台
分享给朋友:

相关文章

vue实现网页水印

vue实现网页水印

添加静态水印 在Vue中可以通过CSS或Canvas实现静态水印。CSS方法适合简单文字水印,Canvas适合复杂水印效果。 <template> <div class="wa…

vue实现后台功能

vue实现后台功能

Vue 实现后台功能的方法 Vue.js 是一个流行的前端框架,适合构建后台管理系统。以下是实现后台功能的常见方法和步骤。 项目初始化 使用 Vue CLI 或 Vite 初始化项目: # Vue…

vue实现后台管理系统

vue实现后台管理系统

Vue 实现后台管理系统的关键步骤 项目初始化与基础配置 使用 Vue CLI 或 Vite 创建项目,推荐选择 Vue 3 组合式 API 开发模式。安装必要依赖如 vue-router、pinia…

css制作水印

css制作水印

使用CSS制作水印 通过CSS可以轻松为网页或图片添加水印效果,以下是几种常见方法: 使用伪元素添加文字水印 通过::after或::before伪元素在目标元素上叠加半透明文字: .wa…

css制作后台页面

css制作后台页面

后台页面布局设计 使用CSS Grid或Flexbox创建响应式布局。Grid适合复杂网格结构,Flexbox适合单行或单列排列。以下是一个基于Grid的布局示例: .container { d…

css怎样制作水印

css怎样制作水印

使用伪元素添加水印 通过 ::after 或 ::before 伪元素在目标元素上叠加水印,适合局部区域水印: .watermark { position: relative; } .water…