WIN-2308041133\Administrator 4 months ago
parent
commit
5da14667f9
2 changed files with 67 additions and 17 deletions
  1. 12 12
      app/api/controller/user/UserBillController.php
  2. 55 5
      crmeb/services/UtilService.php

+ 12 - 12
app/api/controller/user/UserBillController.php

@@ -323,19 +323,19 @@ class UserBillController
                 foreach ($routineSpreadBanner as $key => &$item) {
                     $posterInfo = '海报生成失败:(';
                     $config = array(
-                        array(  // ===== 新增:圆形头像(左上位置,适配大海报) =====
-                            'url' => $avatarUrl,
-                            'stream' => 0,
-                            'left' => 120,    // 调整:与文字对齐
-                            'top' => 50,      // 调整:海报顶部
-                            'right' => 0,
-                            'bottom' => 0,
-                            'width' => 150,
-                            'height' => 150,
-                            'opacity' => 100,
-                            'circle' => true  // 圆形蒙版
-                        ),
                         'image' => array(
+                            array(  // ===== 新增:圆形头像(左上位置,适配大海报) =====
+                                'url' => $avatarUrl,
+                                'stream' => 0,
+                                'left' => 120,    // 调整:与文字对齐
+                                'top' => 50,      // 调整:海报顶部
+                                'right' => 0,
+                                'bottom' => 0,
+                                'width' => 150,
+                                'height' => 150,
+                                'opacity' => 100,
+                                'circle' => true  // 圆形蒙版
+                            ),
                             array(
                                 'url' => $urlCode,     //二维码资源
                                 'stream' => 0,

+ 55 - 5
crmeb/services/UtilService.php

@@ -302,7 +302,8 @@ class UtilService
             'bottom' => 0,
             'width' => 100,
             'height' => 100,
-            'opacity' => 100
+            'opacity' => 100,
+            'circle' => false // 新增:是否裁剪为圆形
         );
         $textDefault = array(
             'text' => '',
@@ -336,14 +337,61 @@ class UtilService
                 $res = $function($val['url']);
                 $resWidth = $info[0];
                 $resHeight = $info[1];
-                $canvas = imagecreatetruecolor($val['width'], $val['height']);
-                imagefill($canvas, 0, 0, $color);
-                imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
+
+                // 如果是圆形,先创建圆形图片
+                if ($val['circle']) {
+                    $canvas = imagecreatetruecolor($val['width'], $val['height']);
+                    imagefill($canvas, 0, 0, imagecolorallocatealpha($canvas, 0, 0, 0, 127));
+
+                    // 创建圆形蒙版
+                    $mask = imagecreatetruecolor($val['width'], $val['height']);
+                    $transparent = imagecolorallocate($mask, 255, 0, 0);
+                    imagecolortransparent($mask, $transparent);
+                    imagefill($mask, 0, 0, $transparent);
+
+                    $black = imagecolorallocate($mask, 0, 0, 0);
+                    imagefilledellipse($mask, $val['width']/2, $val['height']/2, $val['width'], $val['height'], $black);
+
+                    // 将原图缩放并裁剪到圆形
+                    $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);
+
+                    // 将透明背景设置为白色(如果不想要白色背景,可以调整这里的颜色)
+                    $white = imagecolorallocate($canvas, 255, 255, 255);
+                    for ($x = 0; $x < $val['width']; $x++) {
+                        for ($y = 0; $y < $val['height']; $y++) {
+                            $color = imagecolorsforindex($canvas, imagecolorat($canvas, $x, $y));
+                            if ($color['alpha'] == 127) {
+                                imagesetpixel($canvas, $x, $y, $white);
+                            }
+                        }
+                    }
+
+                    imagedestroy($sourceResized);
+                    imagedestroy($mask);
+                } else {
+                    // 非圆形,按原逻辑处理
+                    $canvas = imagecreatetruecolor($val['width'], $val['height']);
+                    imagefill($canvas, 0, 0, $color);
+                    imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
+                }
+
                 $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($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);//左,上,右,下,宽度,高度,透明度
+
+                // 修改合并方式,支持透明度
+                imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']);
+
+                imagedestroy($res);
+                imagedestroy($canvas);
             }
         }
+
+        // ... 后面的代码保持不变
         if (isset($config['text']) && !empty($config['text'])) {
             foreach ($config['text'] as $key => $val) {
                 $val = array_merge($textDefault, $val);
@@ -354,11 +402,13 @@ class UtilService
                 imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
             }
         }
+
         ob_start();
         imagejpeg($imageRes);
         imagedestroy($imageRes);
         $res = ob_get_contents();
         ob_end_clean();
+
         $key = substr(md5(rand(0, 9999)), 0, 5) . date('YmdHis') . rand(0, 999999) . '.jpg';
         $uploadType = (int)sys_config('upload_type', 1);
         $upload = new Upload($uploadType, [