php后台实现水印
添加水印的基本方法
在PHP中实现图片水印功能主要通过GD库或Imagick扩展完成。以下是两种常见实现方式:
GD库实现方式:

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);
}
水印透明度控制
调整水印透明度可以使效果更自然:

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格式以保持透明度。






