Przeglądaj źródła

新增速创API中转站的GptImage2模型

lh 2 miesięcy temu
rodzic
commit
b48f7c9946
1 zmienionych plików z 55 dodań i 19 usunięć
  1. 55 19
      app/Services/AIGeneration/AIImageGenerationService.php

+ 55 - 19
app/Services/AIGeneration/AIImageGenerationService.php

@@ -858,24 +858,29 @@ class AIImageGenerationService
                 'key' => $apiKey,
                 'prompt' => $task->prompt,
             ];
-            
-            // 尺寸参数(根据宽高判断)
-            $area = $task->width * $task->height;
-            if ($area >= 1024*1024 && $area < 2048*2048) {
-                $apiParams['size'] = '1k';
-            } elseif ($area >= 2048*2048) {
-                $apiParams['size'] = '2k';
-            } else {
-                $apiParams['size'] = '1k'; // 默认1k
-            }
-            
-            // 画面比例
-            if ($task->width && $task->height) {
-                $ratio = $task->width . ':' . $task->height;
-                // 简化比例
-                $gcd = $this->gcd($task->width, $task->height);
-                $ratio = ($task->width / $gcd) . ':' . ($task->height / $gcd);
-                $apiParams['aspectRatio'] = $ratio;
+
+            if ($task->model == self::MODEL_GPT_IMAGE_2) {
+                // 该模型下,size需设置为9:16类似的数值,请根据如下比例反推
+                $apiParams['size'] = $this->getRatioFromSize($task->width, $task->height);
+            }else {
+                // 尺寸参数(根据宽高判断)
+                $area = $task->width * $task->height;
+                if ($area >= 1024*1024 && $area < 2048*2048) {
+                    $apiParams['size'] = '1k';
+                } elseif ($area >= 2048*2048) {
+                    $apiParams['size'] = '2k';
+                } else {
+                    $apiParams['size'] = '1k'; // 默认1k
+                }
+                
+                // 画面比例
+                if ($task->width && $task->height) {
+                    $ratio = $task->width . ':' . $task->height;
+                    // 简化比例
+                    $gcd = $this->gcd($task->width, $task->height);
+                    $ratio = ($task->width / $gcd) . ':' . ($task->height / $gcd);
+                    $apiParams['aspectRatio'] = $ratio;
+                }
             }
             
             // 参考图片
@@ -1550,4 +1555,35 @@ class AIImageGenerationService
         
         return $assetIds;
     }
-}
+
+    /**
+     * 根据宽高反推图片比例
+     * @param int $width 宽度
+     * @param int $height 高度
+     * @return string 比例字符串,如 "9:16",匹配不到则返回 "auto"
+     */
+    private function getRatioFromSize($width, $height)
+    {
+        if (!$width || !$height) {
+            return 'auto';
+        }
+
+        // 遍历所有预定义的比例,查找匹配的
+        foreach (BaseConst::IMAGE_RATIOS as $ratio => $size) {
+            if ($size['width'] == $width && $size['height'] == $height) {
+                return $ratio;
+            }
+        }
+
+        // 没有精确匹配,尝试通过计算比例匹配
+        $gcd = $this->gcd($width, $height);
+        $simplifiedRatio = ($width / $gcd) . ':' . ($height / $gcd);
+
+        // 检查简化后的比例是否在预定义列表中
+        if (isset(BaseConst::IMAGE_RATIOS[$simplifiedRatio])) {
+            return $simplifiedRatio;
+        }
+
+        return 'auto';
+    }
+}