以下是 `_imgWatermark` 函数的中文注释和解析。该函数用于在图像上添加水印:
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) {if ($x == 0 && $y == 0) {$x = round($ww / 2);}$index++;imagecopy($src, $thumb, 0, 0, $x + $ccasd - $offset_x, $y + $ccasdy, $sourceInfo[0], $sourceInfo[1]);imagecopy($src, $water, 0, 0, 0, 0, $sourceInfo[0], $sourceInfo[1]);imagecopymerge($thumb, $src, $x + $ccasd - $offset_x, $y + $ccasdy, 0, 0, $sourceInfo[0], $sourceInfo[1], $pct);$y += $sourceInfo[1] + $hh;}$x += $sourceInfo[0] + $ww;$index = 0;}$src_save($thumb, $path_image);imagedestroy($src);imagedestroy($water);return $imagename;break;}// 合并水印到源图if (imagecopymerge($src, $water, $start_x, $start_y, 0, 0, $water_info[0], $water_info[1], $pct) && $position != 10) {$src_save($src, $path_image);imagedestroy($src);imagedestroy($water);return $imagename;} else {$error = "水印制作失败";return false;}
}
### 关键点:
- **文件存在性检查:** 确保源图和水印图文件存在。
- **支持的图像类型:** 验证图像是否属于支持的类型(JPEG、GIF、PNG)。
- **图像缩放:** 根据比例缩放水印图,使其相对于源图的尺寸更小。
- **水印位置:** 水印可以根据 `position` 参数被放置在源图的不同位置。
- **水印平铺:** 如果 `position` 设置为 10,水印将在整个图像上平铺。
- **透明度和旋转:** 水印可以通过 `angle` 和 `pct` 参数进行旋转和调整透明度。
- **资源管理:** 函数确保所有图像资源在使用后被销毁以释放内存。
该函数为图像添加水印提供了灵活的方式,允许用户自定义水印的位置、透明度和平铺方式。