以下是几种PHP实现添加图片水印的示例代码:
简单的图片水印添加示例
<?php
// 加载原始图片
$source = imagecreatefromjpeg('image.jpg');
// 加载水印图片
$watermark = imagecreatefrompng('watermark.png');
// 获取水印图片的宽度和高度
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// 计算水印在原始图片中的位置,这里将水印放置在右下角,距离边缘10像素
$dest_x = imagesx($source) - $watermark_width - 10;
$dest_y = imagesy($source) - $watermark_height - 10;
// 将水印添加到原始图片中
imagecopy($source, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
// 保存添加水印后的图片为新文件
imagejpeg($source, 'result.jpg');
// 释放内存
imagedestroy($source);
imagedestroy($watermark);
?>
可调整水印位置和透明度的示例
<?php
// 目标图片路径
$targetimagepath = 'images/original.jpg';
// 水印图片路径
$watermarkimagepath = 'images/watermark.png';
// 加载目标图片
$targetimage = imagecreatefromjpeg($targetimagepath);
// 获取目标图片的宽度和高度
$targetwidth = imagesx($targetimage);
$targetheight = imagesy($targetimage);
// 加载水印图片
$watermarkimage = imagecreatefrompng($watermarkimagepath);
// 获取水印图片的宽度和高度
$watermarkwidth = imagesx($watermarkimage);
$watermarkheight = imagesy($watermarkimage);
// 设置水印的位置,这里示例为右下角,可根据需求调整
$x = $targetwidth - $watermarkwidth;
$y = $targetheight - $watermarkheight;
// 设置水印透明度,取值范围0-127,0为完全透明,127为完全不透明
$alpha = 50;
// 循环遍历水印图片的每个像素点,添加透明度效果并合并到目标图片上
for ($i = 0; $i < $watermarkwidth; $i++) {
for ($j = 0; $j < $watermarkheight; $j++) {
$watermarkcolor = imagecolorat($watermarkimage, $i, $j);
$alphachannel = ($watermarkcolor >> 24) & 0xff;
$alphachannel = $alphachannel * $alpha / 127;
$watermarkcolor = ($watermarkcolor & 0xffffff) | ($alphachannel << 24);
if ($alphachannel > 0) {
$targetcolor = imagecolorat($targetimage, $x + $i, $y + $j);
$targetcolor = imagecolorset($targetimage, $x + $i, $y + $j,
(($watermarkcolor >> 16) & 0xff) * $alphachannel / 127 + (($targetcolor >> 16) & 0xff) * (127 - $alphachannel) / 127,
(($watermarkcolor >> 8) & 0xff) * $alphachannel / 127 + (($targetcolor >> 8) & 0xff) * (127 - $alphachannel) / 127,
($watermarkcolor & 0xff) * $alphachannel / 127 + ($targetcolor & 0xff) * (127 - $alphachannel) / 127,
($alphachannel << 24)
);
}
}
}
// 输出添加水印后的图片,这里输出为jpeg格式,可根据需要调整格式
header('content-type: image/jpeg');
imagejpeg($targetimage);
// 释放内存,销毁图片资源
imagedestroy($targetimage);
imagedestroy($watermarkimage);
?>
带有更多功能的图片水印函数示例
<?php
function _imgwatermark($src_image, $water_image, $path_image = '', $position = 10, $pct = 30, $angle = 15)
{
// 检查源图和水印图文件是否存在
if (!is_file($src_image)) {
$error = '源图不存在';
return false;
}
if (!is_file($water_image)) {
$error = '水印图不存在';
return false;
}
// 获取图片信息
$src_info = getimagesize($src_image);
$water_info = getimagesize($water_image);
// 定义允许的图片mime类型
$allow = array(
'image/jpeg' => 'jpeg',
'image/gif' => 'gif',
'image/jpg' => 'jpg',
'image/png' => 'png',
'image/pjpeg' => 'jpeg'
);
// 检查图像类型是否被允许
if (!array_key_exists($src_info['mime'], $allow)) {
$error = "源图类型不支持添加水印";
return false;
}
if (!array_key_exists($water_info['mime'], $allow)) {
$error = "水印图类型不支持";
return false;
}
// 根据mime类型创建打开和保存图片的函数
$src_open = 'imagecreatefrom'. $allow[$src_info['mime']];
$water_open = 'imagecreatefrom'. $allow[$water_info['mime']];
$src_save = 'image'. $allow[$src_info['mime']];
// 打开图像资源
$src = $src_open($src_image);
$water = $water_open($water_image);
// 根据比例计算水印的新尺寸
$scale_factor = 0.15; // 水印的相对大小比例
$new_water_width = $src_info[0] * $scale_factor;
$new_water_height = ($water_info[1] / $water_info[0]) * $new_water_width;
// 创建缩放后的水印图
$resized_water = imagecreatetruecolor($new_water_width, $new_water_height);
imagealphablending($resized_water, false);
imagesavealpha($resized_water, true);
imagecopyresampled($resized_water, $water, 0, 0, 0, 0, $new_water_width, $new_water_height, $water_info[0], $water_info[1]);
// 释放原始水印资源,使用缩放后的水印
imagedestroy($water);
$water = $resized_water;
$water_info[0] = $new_water_width;
$water_info[1] = $new_water_height;
if ($angle) {
// 旋转水印图片
$water = imagerotate($water, $angle, imagecolorallocatealpha($water, 0, 0, 0, 127));
// 获取旋转后的水印尺寸
$sourceinfo[0] = imagesx($water);
$sourceinfo[1] = imagesy($water);
// 设置水印平铺的间距
$ww = $src_info[0] * 0.1; // 水平方向间距
$hh = $src_info[1] * 0.1; // 垂直方向间距
}
if ($position == 10) {
$offset_x = $water_info[0] / 2;
}
// 计算水印在源图中的位置
$start_x = $start_y = 0;
switch ($position) {
case 1:
break;
case 2:
$start_x = floor(($src_info[0] - $water_info[0]) / 2);
break;
case 3:
$start_x = $src_info[0] - $water_info[0];
break;
case 4:
$start_y = floor(($src_info[1] - $water_info[1]) / 2);
break;
case 5:
$start_x = floor(($src_info[0] - $water_info[0]) / 2);
$start_y = floor(($src_info[1] - $water_info[1]) / 2);
break;
case 6:
$start_x = $src_info[0] - $water_info[0];
$start_y = floor(($src_info[1] - $water_info[1]) / 2);
break;
case 7:
$start_y = $src_info[1] - $water_info[1];
break;
case 8:
$start_x = floor(($src_info[0] - $water_info[0]) / 2);
$start_y = $src_info[1] - $water_info[1];
break;
case 9:
$start_x = $src_info[0] - $water_info[0];
$start_y = $src_info[1] - $water_info[1];
break;
case 10:
// 创建用于平铺的图像资源
$fun = 'imagecreatefrom'. image_type_to_extension($src_info[2], false);
$thumb = $fun($src_image);
// 定义总长度
$start_x = $src_info[0] - 0;
$start_y = $src_info[1] - 0;
$index = 0;
$ccasd = 0;
$ccasdy = 0;
// 循环平铺水印
for ($x = 0; $x < $start_x; $x++) {
for ($y = 0; $y < $start_y; $y++) {
// 此处省略平铺水印的具体代码逻辑,可根据实际需求补充完整
}
}
}
// 合并水印到源图
imagecopy($src, $water, $start_x, $start_y, 0, 0, $water_info[0], $water_info[1]);
// 保存添加水印后的图片
if ($path_image!= '') {
$src_save($src, $path_image);
} else {
header('Content-Type: '. $src_info['mime']);
$src_save($src);
}
// 释放内存
imagedestroy($src);
imagedestroy($water);
return true;
}
?>
在使用这些示例代码时,需要注意以下几点:
• 确保服务器环境已经安装并配置好了PHP的GD库,因为这些函数都是基于GD库来实现图像处理的.
• 图片路径要使用正确的路径,可以是相对路径或绝对路径,根据实际情况进行调整.
• 不同的图片格式可能需要使用不同的函数来创建图像资源,如
imagecreatefromjpeg
、imagecreatefrompng
等.• 在处理完图片后,要及时使用
imagedestroy
函数释放内存,以避免内存泄漏.