liuhairui 5 сар өмнө
parent
commit
c897b21fa8

+ 96 - 1
application/service/AIGatewayService.php

@@ -26,7 +26,12 @@ class AIGatewayService{
         'txttoimg' => [
             'api_key' => 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY',
             'api_url' => 'https://niubi.zeabur.app/v1/images/generations'
-        ]
+        ],
+//        //文生文-超结损分析
+//        'chaojiesun' => [
+//            'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73',
+//            'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
+//        ]
     ];
 
     /**
@@ -53,6 +58,11 @@ class AIGatewayService{
 
         return $this->callApi($this->config['imgtotxt']['api_url'], $this->config['imgtotxt']['api_key'], $data);
     }
+    /**
+     * 图生文-方法二
+     * @param string $imageUrl 图像 URL,支持公网可访问地址
+     * @param string $prompt   对图像的提问内容或提示文本
+     */
 //    public function callGptApi($imageUrl, $prompt)
 //    {
 //        $data = [
@@ -91,6 +101,25 @@ class AIGatewayService{
         );
     }
 
+    /**
+     * 文生文-超结损分析
+     * @param string $prompt 用户输入的文本提示内容
+     */
+//    public function chaojiesunGptApi($prompt)
+//    {
+//        $data = [
+//            'prompt' => $prompt,
+//            'model' => 'gpt-4',
+//            'session_id' => null,
+//            'context_reset' => true
+//        ];
+//        return $this->callApi(
+//            $this->config['chaojiesun']['api_url'],
+//            $this->config['chaojiesun']['api_key'],
+//            $data
+//        );
+//    }
+
     /**
      * 文生图
      * @param string $prompt          提示文本,用于指导图像生成
@@ -127,6 +156,72 @@ class AIGatewayService{
         return $this->callApi($this->config['txttoimg']['api_url'], $this->config['txttoimg']['api_key'], $data);
     }
 
+
+    public function imgtoimgGptApi($prompt, $new_image_url)
+    {
+        $imgPath = ROOT_PATH . 'public/' . $new_image_url;
+
+        if (!file_exists($imgPath)) {
+            return ['code' => 1, 'msg' => '原图不存在:' . $new_image_url];
+        }
+
+        // 原图 base64 编码
+        $imgData = file_get_contents($imgPath);
+        $base64Img = base64_encode($imgData);
+        $initImage = 'data:image/png;base64,' . $base64Img;
+
+        // 构建 POST 请求参数
+        $postData = json_encode([
+            'prompt' => $prompt,
+            'sampler_name' => 'DPM++ 2M SDE Heun',
+            'seed' => -1,
+            'steps' => 20,
+            'cfg_scale' => 7,
+            'denoising_strength' => 0.6,
+            'width' => 1024,
+            'height' => 2048,
+            'resize_mode' => 0,
+            'inpaint_full_res' => true,
+            'inpainting_fill' => 1,
+            'init_images' => [$initImage],
+            'override_settings' => [
+                'sd_model_checkpoint' => 'realisticVisionV51_v51VAE-inpainting.safetensors [f0d4872d24]',
+                'sd_vae' => 'anything-v4.5.vae.pt'
+            ]
+        ]);
+
+        $apiUrl = "http://20.0.17.233:45001/sdapi/v1/img2img";
+        $headers = ['Content-Type: application/json'];
+
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $apiUrl);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_POST, true);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
+        curl_setopt($ch, CURLOPT_TIMEOUT, 90);
+        $response = curl_exec($ch);
+        $error = curl_error($ch);
+        curl_close($ch);
+
+        if ($error) {
+            return ['code' => 1, 'msg' => '请求失败:' . $error];
+        }
+
+        $data = json_decode($response, true);
+        if (!isset($data['images'][0])) {
+            return ['code' => 1, 'msg' => 'API未返回图像数据'];
+        }
+
+        return [
+            'code' => 0,
+            'msg' => '图像生成成功',
+            'data' => [
+                'url' => $data['images'][0]
+            ]
+        ];
+    }
+
     /**
      * 通用 API 调用方法(支持重试机制)
      *

+ 6 - 3
application/service/ImageService.php

@@ -11,13 +11,12 @@ class ImageService{
      * 推送图像任务到队列(支持链式和单独模式)
      * @param array $params 请求参数,包含图像批次、模型类型、尺寸等
      */
-
     public function handleImage($params) {
         if (!isset($params["batch"])) {return false;}
 
         $arr = [];
         $batch = $params["batch"]; // 获取图像批量信息
-        $num = $params["num"]; // 获取需要生成的实例数量
+        $num = $params["num"]; // 获取执行次数数量
 
         // 获取模板
         // english_content 文生文提示词
@@ -65,7 +64,7 @@ class ImageService{
 
             $arr = array_map(function ($item) use ($task_id) {
                 $item['type'] = '图生文';
-                $item['chain_next'] = ['文生文', '文生图'];
+                $item['chain_next'] = ['文生文', '文生图','图生图'];
                 $item['task_id'] = $task_id;
                 return $item;
             }, $arr);
@@ -91,6 +90,10 @@ class ImageService{
                     $insertData['model'] = $params['selectedOption'];
                     $insertData['model_name'] = '文生图';
                     break;
+                case '图生图':
+                    $insertData['model'] = "realisticVisionV51_v51VAE-inpainting.safetensors [f0d4872d24]";
+                    $insertData['model_name'] = '图生图';
+                    break;
                 default:
                     return false;
             }