|
|
@@ -340,7 +340,7 @@ class UtilService
|
|
|
|
|
|
// 如果是圆形,先创建圆形图片
|
|
|
if ($val['circle']) {
|
|
|
- // 创建一个新的画布用于圆形头像
|
|
|
+ // 创建一个正方形的画布
|
|
|
$canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
|
|
|
// 创建透明背景
|
|
|
@@ -348,44 +348,40 @@ class UtilService
|
|
|
$transparent = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
|
|
|
imagefill($canvas, 0, 0, $transparent);
|
|
|
|
|
|
- // 创建圆形蒙版
|
|
|
- $mask = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
- imagefill($mask, 0, 0, imagecolorallocate($mask, 255, 0, 0));
|
|
|
-
|
|
|
- // 绘制圆形
|
|
|
- imagefilledellipse($mask, $val['width']/2, $val['height']/2, $val['width'], $val['height'], imagecolorallocate($mask, 0, 0, 0));
|
|
|
-
|
|
|
- // 设置红色为透明色
|
|
|
- imagecolortransparent($mask, imagecolorallocate($mask, 255, 0, 0));
|
|
|
-
|
|
|
- // 将原图缩放到圆形大小
|
|
|
+ // 将原图缩放到指定大小
|
|
|
$sourceResized = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
|
|
|
- // 应用圆形蒙版
|
|
|
- imagecopy($canvas, $sourceResized, 0, 0, 0, 0, $val['width'], $val['height']);
|
|
|
- imagecopymerge($canvas, $mask, 0, 0, 0, 0, $val['width'], $val['height'], 100);
|
|
|
+ // 创建圆形
|
|
|
+ $centerX = $val['width'] / 2;
|
|
|
+ $centerY = $val['height'] / 2;
|
|
|
+ $radius = min($val['width'], $val['height']) / 2;
|
|
|
|
|
|
- // 将透明背景设置为白色(可选)
|
|
|
- $white = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
for ($x = 0; $x < $val['width']; $x++) {
|
|
|
for ($y = 0; $y < $val['height']; $y++) {
|
|
|
- $rgba = imagecolorat($canvas, $x, $y);
|
|
|
- $alpha = ($rgba >> 24) & 0x7F;
|
|
|
- if ($alpha > 100) {
|
|
|
- imagesetpixel($canvas, $x, $y, $white);
|
|
|
+ $distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
|
|
|
+ if ($distance <= $radius) {
|
|
|
+ $color = imagecolorat($sourceResized, $x, $y);
|
|
|
+ imagesetpixel($canvas, $x, $y, $color);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 添加白色边框(可选)
|
|
|
+ $borderColor = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
+ $borderRadius = $radius + 1;
|
|
|
+ for ($angle = 0; $angle < 360; $angle += 1) {
|
|
|
+ $x = $centerX + $borderRadius * cos(deg2rad($angle));
|
|
|
+ $y = $centerY + $borderRadius * sin(deg2rad($angle));
|
|
|
+ imagesetpixel($canvas, $x, $y, $borderColor);
|
|
|
+ }
|
|
|
+
|
|
|
imagedestroy($sourceResized);
|
|
|
- imagedestroy($mask);
|
|
|
imagedestroy($res);
|
|
|
} else {
|
|
|
// 非圆形,按原逻辑处理
|
|
|
$canvas = imagecreatetruecolor($val['width'], $val['height']);
|
|
|
- // 修正这里:使用正确的颜色填充
|
|
|
- $bgColor = imagecolorallocate($canvas, 255, 255, 255); // 使用白色背景
|
|
|
+ $bgColor = imagecolorallocate($canvas, 255, 255, 255);
|
|
|
imagefill($canvas, 0, 0, $bgColor);
|
|
|
imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
|
|
|
imagedestroy($res);
|
|
|
@@ -394,7 +390,7 @@ class UtilService
|
|
|
$val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) - $val['width'] : $val['left'];
|
|
|
$val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
|
|
|
|
|
|
- // 修正:使用正确的imagecopymerge参数
|
|
|
+ // 合并到主图像
|
|
|
imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
|
|
|
|
|
|
imagedestroy($canvas);
|