当前位置:首页 > 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 实现后台管理系统的方法 使用 Vue 实现后台管理系统可以结合 Vue.js 框架的响应式特性和丰富的生态系统。以下是一个完整的实现方案: 技术栈选择 Vue 3:使用 Compositio…

vue elementui实现管理后台

vue elementui实现管理后台

Vue + ElementUI 实现管理后台 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。使用 Vue CLI 创建项目: vue create admin-dem…

vue实现后台权限管理

vue实现后台权限管理

Vue 实现后台权限管理的方法 路由权限控制 通过动态路由实现权限过滤,在用户登录后根据角色或权限列表生成可访问的路由表。使用 Vue Router 的 addRoutes 方法动态添加路由。 //…

vue页面实现水印相机

vue页面实现水印相机

实现水印相机功能 在Vue中实现水印相机功能,可以通过结合HTML5的Canvas和拍照API完成。以下是一个完整的实现方案: 创建基础页面结构 <template> <div…

php后台实现

php后台实现

PHP 后台实现的关键要素 PHP 是一种广泛应用于服务器端编程的脚本语言,适合构建动态网站和后台系统。以下是实现 PHP 后台的核心方法和注意事项。 基础环境搭建 安装 PHP 运行环境,推荐使用…

php实现水印

php实现水印

添加水印的基本方法 使用 PHP 的 GD 库或 Imagick 扩展可以为图片添加水印。GD 库是 PHP 默认支持的图像处理库,而 Imagick 提供了更强大的功能。 使用 GD 库添加文字水…