PHP通过GD库添加水印的核心是加载原图和水印(图片或文字),利用imagecopymerge()或imagettftext()将水印叠加到原图指定位置,支持透明度、字体样式和精准定位,最后输出并释放资源。

PHP通过GD库给图片添加水印的核心思路,就是加载原始图片和水印图片(或者生成水印文字),然后利用GD库提供的函数,将水印内容叠加到原始图片上,最后将处理后的图片保存下来或者直接输出到浏览器。这过程涉及到对图像像素的直接操作,理解起来并不复杂,但要做好细节,比如透明度、位置和性能,就需要一些技巧了。
要用PHP GD库给图片添加水印,我们通常会遵循一套比较标准的流程。我个人在做项目的时候,一般会先确定好水印的类型——是图片水印还是文字水印,因为这两种的处理方式略有不同。
图片水印的实现步骤:
imagecreatefromjpeg()
imagecreatefrompng()
imagecreatefromgif()
imagesx()
imagesy()
imagecopy()
imagecopymerge()
pct
imagejpeg()
imagepng()
imagegif()
imagedestroy()
这是一个简单的图片水印代码示例:
立即学习“PHP免费学习笔记(深入)”;
<?php
function addImageWatermark($sourceImage, $watermarkImage, $outputImage = null, $position = 'bottom-right', $opacity = 50) {
// 确保GD库已加载
if (!extension_loaded('gd')) {
echo "GD库未加载,请检查PHP配置。";
return false;
}
$source_info = getimagesize($sourceImage);
$watermark_info = getimagesize($watermarkImage);
if (!$source_info || !$watermark_info) {
echo "无法获取图片信息,请检查路径或文件是否损坏。";
return false;
}
$source_mime = $source_info['mime'];
$watermark_mime = $watermark_info['mime'];
// 根据MIME类型创建原始图片资源
switch ($source_mime) {
case 'image/jpeg':
$source_img = imagecreatefromjpeg($sourceImage);
break;
case 'image/png':
$source_img = imagecreatefrompng($sourceImage);
break;
case 'image/gif':
$source_img = imagecreatefromgif($sourceImage);
break;
default:
echo "不支持的原始图片格式: " . $source_mime;
return false;
}
// 根据MIME类型创建水印图片资源
switch ($watermark_mime) {
case 'image/jpeg':
$watermark_img = imagecreatefromjpeg($watermarkImage);
break;
case 'image/png':
$watermark_img = imagecreatefrompng($watermarkImage);
// 针对PNG透明度处理
imagealphablending($source_img, true);
imagesavealpha($source_img, true);
break;
case 'image/gif':
$watermark_img = imagecreatefromgif($watermarkImage);
break;
default:
echo "不支持的水印图片格式: " . $watermark_mime;
imagedestroy($source_img);
return false;
}
$source_width = imagesx($source_img);
$source_height = imagesy($source_img);
$watermark_width = imagesx($watermark_img);
$watermark_height = imagesy($watermark_img);
// 计算水印位置
$dest_x = 0;
$dest_y = 0;
switch ($position) {
case 'top-left':
$dest_x = 0;
$dest_y = 0;
break;
case 'top-right':
$dest_x = $source_width - $watermark_width;
$dest_y = 0;
break;
case 'bottom-left':
$dest_x = 0;
$dest_y = $source_height - $watermark_height;
break;
case 'bottom-right':
default: // 默认右下角
$dest_x = $source_width - $watermark_width - 10; // 留10px边距
$dest_y = $source_height - $watermark_height - 10; // 留10px边距
break;
case 'center':
$dest_x = ($source_width - $watermark_width) / 2;
$dest_y = ($source_height - $watermark_height) / 2;
break;
}
// 叠加水印,使用imagecopymerge实现透明度
// 注意:imagecopymerge对PNG透明度处理可能不如直接imagecopy + imagealphablending + imagesavealpha好
// 如果水印是带alpha通道的PNG,且需要保留其自身透明度,推荐直接使用imagecopy,并确保原图支持alpha
if ($watermark_mime == 'image/png' && $opacity == 100) {
imagecopy($source_img, $watermark_img, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
} else {
imagecopymerge($source_img, $watermark_img, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
}
// 保存或输出图片
if ($outputImage) {
switch ($source_mime) {
case 'image/jpeg':
imagejpeg($source_img, $outputImage, 90); // 质量90
break;
case 'image/png':
imagepng($source_img, $outputImage);
break;
case 'image/gif':
imagegif($source_img, $outputImage);
break;
}
} else {
// 直接输出到浏览器
header("Content-Type: " . $source_mime);
switch ($source_mime) {
case 'image/jpeg':
imagejpeg($source_img);
break;
case 'image/png':
imagepng($source_img);
break;
case 'image/gif':
imagegif($source_img);
break;
}
}
// 释放内存
imagedestroy($source_img);
imagedestroy($watermark_img);
return true;
}
// 示例用法:
// addImageWatermark('path/to/source.jpg', 'path/to/watermark.png', 'path/to/output.jpg', 'bottom-right', 70);
// addImageWatermark('path/to/source.png', 'path/to/watermark.png', null, 'center', 50); // 直接输出到浏览器
?>文字水印的实现步骤:
.ttf
imagettfbbox()
imagettftext()
<?php
function addTextWatermark($sourceImage, $text, $fontFile, $outputImage = null, $position = 'bottom-right', $fontSize = 20, $color = [0, 0, 0, 50], $angle = 0) {
if (!extension_loaded('gd')) {
echo "GD库未加载,请检查PHP配置。";
return false;
}
if (!file_exists($fontFile)) {
echo "字体文件不存在: " . $fontFile;
return false;
}
$source_info = getimagesize($sourceImage);
if (!$source_info) {
echo "无法获取原始图片信息。";
return false;
}
$source_mime = $source_info['mime'];
switch ($source_mime) {
case 'image/jpeg':
$source_img = imagecreatefromjpeg($sourceImage);
break;
case 'image/png':
$source_img = imagecreatefrompng($sourceImage);
// 确保PNG透明度支持
imagealphablending($source_img, true);
imagesavealpha($source_img, true);
break;
case 'image/gif':
$source_img = imagecreatefromgif($sourceImage);
break;
default:
echo "不支持的原始图片格式: " . $source_mime;
return false;
}
$source_width = imagesx($source_img);
$source_height = imagesy($source_img);
// 分配颜色,包括alpha通道实现透明度
$alpha = isset($color[3]) ? $color[3] : 0; // 0-127, 0为完全不透明
$watermark_color = imagecolorallocatealpha($source_img, $color[0], $color[1], $color[2], $alpha);
// 计算文字边界框
$text_bbox = imagettfbbox($fontSize, $angle, $fontFile, $text);
$text_width = abs($text_bbox[2] - $text_bbox[0]);
$text_height = abs($text_bbox[7] - $text_bbox[1]); // abs($text_bbox[3] - $text_bbox[1]) 也可以
// 计算文字位置
$dest_x = 0;
$dest_y = 0;
switch ($position) {
case 'top-left':
$dest_x = 10;
$dest_y = 10 + $text_height; // 考虑到imagettftext的y坐标是基线
break;
case 'top-right':
$dest_x = $source_width - $text_width - 10;
$dest_y = 10 + $text_height;
break;
case 'bottom-left':
$dest_x = 10;
$dest_y = $source_height - 10;
break;
case 'bottom-right':
default: // 默认右下角
$dest_x = $source_width - $text_width - 10;
$dest_y = $source_height - 10;
break;
case 'center':
$dest_x = ($source_width - $text_width) / 2;
$dest_y = ($source_height + $text_height) / 2; // 居中
break;
}
// 写入文字
imagettftext($source_img, $fontSize, $angle, $dest_x, $dest_y, $watermark_color, $fontFile, $text);
// 保存或输出图片
if ($outputImage) {
switch ($source_mime) {
case 'image/jpeg':
imagejpeg($source_img, $outputImage, 90);
break;
case 'image/png':
imagepng($source_img, $outputImage);
break;
case 'image/gif':
imagegif($source_img, $outputImage);
break;
}
} else {
header("Content-Type: " . $source_mime);
switch ($source_mime) {
case 'image/jpeg':
imagejpeg($source_img);
break;
case 'image/png':
imagepng($source_img);
break;
case 'image/gif':
imagegif($source_img);
break;
}
}
imagedestroy($source_img);
return true;
}
// 示例用法:
// addTextWatermark('path/to/source.jpg', 'My Watermark', 'path/to/font.ttf', 'path/to/output_text.jpg', 'bottom-right', 24, [255, 255, 255, 60]);
?>实现半透明图片水印,这是个常见需求,尤其是在版权保护和品牌推广上,水印太实了会影响图片观感,太淡了又起不到效果。GD库提供了
imagecopymerge()
pct
我记得第一次用
imagecopymerge
pct
pct
透明度控制的细节:
imagecopymerge(dest_image, src_image, dest_x, dest_y, src_x, src_y, src_w, src_h, pct)
dest_image
src_image
pct
兼容性考量:
这里有个小坑,GD库在处理不同图片格式的透明度时表现可能不尽相同。
imagecopy()
imagealphablending($source_img, true); imagesavealpha($source_img, true);
imagecopymerge()
imagecopymerge()
imagecopymerge()
pct
总的来说,处理透明度时,要根据水印图片的实际格式和期望的效果来选择不同的函数或参数组合。我通常会测试几次,确保最终效果符合预期。
我个人觉得文字水印在很多场景下比图片水印更灵活,尤其需要动态显示版权信息或者用户ID的时候。比如,一个图片分享平台,给用户上传的图片加上他们的用户名作为水印,用图片水印就得为每个用户生成一个水印图,那维护成本就高了。文字水印就省事多了,直接把文字内容传进去就行。
文字水印的优势:
字体、颜色和位置的精细化设置:
GD库提供了
imagettftext()
imagettftext()
imagecolorallocate()
imagecolorallocatealpha()
imagecolorallocatealpha($image, $red, $green, $blue, $alpha)
$alpha
imagecopymerge
pct
imagettftext()
imagettfbbox()
x
y
以上就是PHP如何给图片添加水印_PHP GD库图片水印添加方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号