WIN-2308041133\Administrator 2 months ago
parent
commit
7ff963d5d4
1 changed files with 212 additions and 120 deletions
  1. 212 120
      crmeb/services/UtilService.php

+ 212 - 120
crmeb/services/UtilService.php

@@ -296,139 +296,231 @@ class UtilService
     public static function setSharePoster($config = array(), $path)
     {
         try {
+            $imageDefault = array(
+                'left' => 0,
+                'top' => 0,
+                'right' => 0,
+                'bottom' => 0,
+                'width' => 100,
+                'height' => 100,
+                'opacity' => 100,
+                'circle' => false // 新增:是否裁剪为圆形
+            );
+            $textDefault = array(
+                'text' => '',
+                'left' => 0,
+                'top' => 0,
+                'fontSize' => 32,       //字号
+                'fontColor' => '255,255,255', //字体颜色
+                'angle' => 0,
+            );
+            $background = $config['background'];//海报最底层得背景
+            if (substr($background, 0, 1) === '/') {
+                $background = substr($background, 1);
+            }
 
+            // 检查背景图片是否存在
+            if (!file_exists($background) && !filter_var($background, FILTER_VALIDATE_URL)) {
+                throw new \Exception('背景图片不存在: ' . $background);
+            }
 
-        $imageDefault = array(
-            'left' => 0,
-            'top' => 0,
-            'right' => 0,
-            'bottom' => 0,
-            'width' => 100,
-            'height' => 100,
-            'opacity' => 100,
-            'circle' => false // 新增:是否裁剪为圆形
-        );
-        $textDefault = array(
-            'text' => '',
-            'left' => 0,
-            'top' => 0,
-            'fontSize' => 32,       //字号
-            'fontColor' => '255,255,255', //字体颜色
-            'angle' => 0,
-        );
-        $background = $config['background'];//海报最底层得背景
-        if (substr($background, 0, 1) === '/') {
-            $background = substr($background, 1);
-        }
-        $backgroundInfo = getimagesize($background);
-        $background = imagecreatefromstring(file_get_contents($background));
-        $backgroundWidth = $backgroundInfo[0];  //背景宽度
-        $backgroundHeight = $backgroundInfo[1];  //背景高度
-        $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
-        $color = imagecolorallocate($imageRes, 0, 0, 0);
-        imagefill($imageRes, 0, 0, $color);
-        imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
-        if (!empty($config['image'])) {
-            foreach ($config['image'] as $key => $val) {
-                $val = array_merge($imageDefault, $val);
-                $info = getimagesize($val['url']);
-                $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
-                if ($val['stream']) {
-                    $info = getimagesizefromstring($val['url']);
-                    $function = 'imagecreatefromstring';
-                }
-                $res = $function($val['url']);
-                $resWidth = $info[0];
-                $resHeight = $info[1];
-
-// 如果是圆形,先创建圆形图片
-                if ($val['circle']) {
-                    // 创建一个支持透明的画布
-                    $canvas = imagecreatetruecolor($val['width'], $val['height']);
-
-                    // 设置画布为透明背景
-                    imagesavealpha($canvas, true);
-                    $transparent = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
-                    imagefill($canvas, 0, 0, $transparent);
-
-                    // 将原图缩放到指定大小
-                    $sourceResized = imagecreatetruecolor($val['width'], $val['height']);
-                    imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
-
-                    // 创建圆形
-                    $centerX = $val['width'] / 2;
-                    $centerY = $val['height'] / 2;
-                    $radius = min($val['width'], $val['height']) / 2;
-
-                    for ($x = 0; $x < $val['width']; $x++) {
-                        for ($y = 0; $y < $val['height']; $y++) {
-                            $distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
-                            if ($distance <= $radius) {
-                                $color = imagecolorat($sourceResized, $x, $y);
-                                imagesetpixel($canvas, $x, $y, $color);
+            $backgroundInfo = getimagesize($background);
+            if ($backgroundInfo === false) {
+                throw new \Exception('无法获取背景图片信息');
+            }
+
+            $backgroundContent = file_get_contents($background);
+            if ($backgroundContent === false) {
+                throw new \Exception('无法读取背景图片');
+            }
+
+            $background = imagecreatefromstring($backgroundContent);
+            if ($background === false) {
+                throw new \Exception('无法创建背景图片资源');
+            }
+
+            $backgroundWidth = $backgroundInfo[0];  //背景宽度
+            $backgroundHeight = $backgroundInfo[1];  //背景高度
+            $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
+
+            // 创建黑色背景
+            $blackColor = imagecolorallocate($imageRes, 0, 0, 0);
+            imagefill($imageRes, 0, 0, $blackColor);
+            imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
+
+            if (!empty($config['image'])) {
+                foreach ($config['image'] as $key => $val) {
+                    $val = array_merge($imageDefault, $val);
+
+                    // 检查图片URL是否存在
+                    if (empty($val['url'])) {
+                        continue; // 跳过空的图片URL
+                    }
+
+                    // 处理URL:如果是相对路径,转换为绝对路径
+                    $imageUrl = $val['url'];
+                    if (!filter_var($imageUrl, FILTER_VALIDATE_URL) && file_exists($imageUrl)) {
+                        // 如果是本地文件路径,直接使用
+                        $imageContent = file_get_contents($imageUrl);
+                    } else {
+                        // 尝试获取远程图片
+                        $imageContent = @file_get_contents($imageUrl);
+                    }
+
+                    if ($imageContent === false) {
+                        // 如果无法获取图片,跳过
+                        continue;
+                    }
+
+                    $res = imagecreatefromstring($imageContent);
+                    if ($res === false) {
+                        // 如果无法创建图片资源,跳过
+                        continue;
+                    }
+
+                    $resWidth = imagesx($res);
+                    $resHeight = imagesy($res);
+
+                    // 如果是圆形,先创建圆形图片
+                    if ($val['circle']) {
+                        // 创建一个正方形的画布
+                        $canvas = imagecreatetruecolor($val['width'], $val['height']);
+                        if ($canvas === false) {
+                            imagedestroy($res);
+                            continue;
+                        }
+
+                        // 用白色填充背景
+                        $whiteColor = imagecolorallocate($canvas, 255, 255, 255);
+                        if ($whiteColor === false) {
+                            imagedestroy($res);
+                            imagedestroy($canvas);
+                            continue;
+                        }
+
+                        $fillResult = imagefill($canvas, 0, 0, $whiteColor);
+                        if ($fillResult === false) {
+                            imagedestroy($res);
+                            imagedestroy($canvas);
+                            continue;
+                        }
+
+                        // 将原图缩放到指定大小
+                        $sourceResized = imagecreatetruecolor($val['width'], $val['height']);
+                        if ($sourceResized === false) {
+                            imagedestroy($res);
+                            imagedestroy($canvas);
+                            continue;
+                        }
+
+                        $copyResult = imagecopyresampled($sourceResized, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
+                        if ($copyResult === false) {
+                            imagedestroy($res);
+                            imagedestroy($canvas);
+                            imagedestroy($sourceResized);
+                            continue;
+                        }
+
+                        // 创建圆形:只复制圆形区域内的像素
+                        $centerX = $val['width'] / 2;
+                        $centerY = $val['height'] / 2;
+                        $radius = min($val['width'], $val['height']) / 2;
+
+                        for ($x = 0; $x < $val['width']; $x++) {
+                            for ($y = 0; $y < $val['height']; $y++) {
+                                $distance = sqrt(pow($x - $centerX, 2) + pow($y - $centerY, 2));
+                                if ($distance <= $radius) {
+                                    $pixelColor = imagecolorat($sourceResized, $x, $y);
+                                    imagesetpixel($canvas, $x, $y, $pixelColor);
+                                }
                             }
                         }
-                    }
 
-                    imagedestroy($sourceResized);
-                    imagedestroy($res);
-                }else {
-                    // 非圆形,按原逻辑处理
-                    $canvas = imagecreatetruecolor($val['width'], $val['height']);
-                    $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);
-                }
+                        imagedestroy($sourceResized);
+                        imagedestroy($res);
+                    } else {
+                        // 非圆形,按原逻辑处理
+                        $canvas = imagecreatetruecolor($val['width'], $val['height']);
+                        if ($canvas === false) {
+                            imagedestroy($res);
+                            continue;
+                        }
+
+                        $bgColor = imagecolorallocate($canvas, 255, 255, 255);
+                        if ($bgColor === false) {
+                            imagedestroy($res);
+                            imagedestroy($canvas);
+                            continue;
+                        }
+
+                        $fillResult = imagefill($canvas, 0, 0, $bgColor);
+                        if ($fillResult === false) {
+                            imagedestroy($res);
+                            imagedestroy($canvas);
+                            continue;
+                        }
+
+                        $copyResult = imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
+                        if ($copyResult === false) {
+                            imagedestroy($res);
+                            imagedestroy($canvas);
+                            continue;
+                        }
 
-                $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'];
+                        imagedestroy($res);
+                    }
+
+                    $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'], 0, 0, $val['width'], $val['height'], $val['opacity']);
+                    // 合并到主图像
+                    $mergeResult = imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], 0, 0, $val['width'], $val['height'], $val['opacity']);
+                    if ($mergeResult === false) {
+                        // 合并失败,但继续处理其他图片
+                    }
 
-                imagedestroy($canvas);
+                    imagedestroy($canvas);
+                }
             }
-        }
 
-        // ... 后面的代码保持不变
-        if (isset($config['text']) && !empty($config['text'])) {
-            foreach ($config['text'] as $key => $val) {
-                $val = array_merge($textDefault, $val);
-                list($R, $G, $B) = explode(',', $val['fontColor']);
-                $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
-                $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
-                $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
-                imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $val['text']);
+            // ... 后面的代码保持不变
+            if (isset($config['text']) && !empty($config['text'])) {
+                foreach ($config['text'] as $key => $val) {
+                    $val = array_merge($textDefault, $val);
+                    list($R, $G, $B) = explode(',', $val['fontColor']);
+                    $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
+                    $val['left'] = $val['left'] < 0 ? $backgroundWidth - abs($val['left']) : $val['left'];
+                    $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
+                    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, [
-            'accessKey' => sys_config('accessKey'),
-            'secretKey' => sys_config('secretKey'),
-            'uploadUrl' => sys_config('uploadUrl'),
-            'storageName' => sys_config('storage_name'),
-            'storageRegion' => sys_config('storage_region'),
-        ]);
-        $res = $upload->to($path)->validate()->stream($res, $key);
-        if ($res === false) {
-            return $upload->getError();
-        } else {
-            $info = $upload->getUploadInfo();
-            $info['image_type'] = $uploadType;
-            return $info;
-        }
-        }catch (\Exception $e) {
+            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, [
+                'accessKey' => sys_config('accessKey'),
+                'secretKey' => sys_config('secretKey'),
+                'uploadUrl' => sys_config('uploadUrl'),
+                'storageName' => sys_config('storage_name'),
+                'storageRegion' => sys_config('storage_region'),
+            ]);
+            $res = $upload->to($path)->validate()->stream($res, $key);
+            if ($res === false) {
+                return $upload->getError();
+            } else {
+                $info = $upload->getUploadInfo();
+                $info['image_type'] = $uploadType;
+                return $info;
+            }
+        } catch (\Exception $e) {
             @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
-            return app('json')->fail('生成图片时,系统错误2', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
+            return '生成图片时,系统错误: ' . $e->getMessage();
         }
     }