当前位置:首页 > 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适合复杂图形或自定义样式。 CSS方式 通过绝对定位和透明度控制,将水印覆盖在页面上:…

vue 实现后台搜索

vue 实现后台搜索

Vue 实现后台搜索功能 在 Vue 中实现后台搜索功能通常涉及以下几个关键步骤: 创建搜索输入框 使用 v-model 绑定搜索关键词,监听输入变化触发搜索请求: <template>…

vue实现视频去除水印

vue实现视频去除水印

去除视频水印的常见方法 去除视频水印可以通过多种技术手段实现,以下是一些常见的方法及其在Vue中的实现思路。 使用CSS覆盖水印 通过CSS定位覆盖水印区域,适用于水印位置固定的场景。在Vue中可以…

css怎样制作水印

css怎样制作水印

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

uniapp 后台定位

uniapp 后台定位

uniapp 后台定位实现方法 在uniapp中实现后台定位功能,需要使用uni-app的API结合原生能力。由于uniapp本身是跨平台框架,不同平台(iOS/Android)的实现方式略有差异。…

vue实现列表水印

vue实现列表水印

实现列表水印的方法 使用CSS背景图 通过CSS的background-image属性为列表容器添加水印背景。水印可以是文字或图片,通过调整透明度实现半透明效果。 <template&…