liuhairui 5 сар өмнө
parent
commit
59189f3bbf

+ 3 - 3
application/api/controller/WorkOrder.php

@@ -73,7 +73,7 @@ class WorkOrder extends Api
         ]);
 
         // -------- 发送请求到 SD API -------- //
-        $apiUrl = "http://20.0.17.233:45001/sdapi/v1/img2img";
+        $apiUrl = "http://20.0.17.188:45001/sdapi/v1/img2img";
         $headers = ['Content-Type: application/json'];
 
         $ch = curl_init();
@@ -125,7 +125,7 @@ class WorkOrder extends Api
         $config = [
             'input_dir' => 'uploads/operate/ai/Preview/arr/',
             'output_dir' => 'uploads/extra_image/',
-            'api_url' => 'http://20.0.17.233:45001/sdapi/v1/extra-single-image',
+            'api_url' => 'http://20.0.17.188:45001/sdapi/v1/extra-single-image',
             'timeout' => 120, // 增加超时时间,高清处理可能耗时较长
             'upscale_params' => [
                 'resize_mode' => 0,
@@ -248,7 +248,7 @@ class WorkOrder extends Api
      * 接口地址: /sdapi/v1/sd-models
      */
     public function sd_models() {
-        $url = "http://20.0.17.233:45001/sdapi/v1/sd-models";
+        $url = "http://20.0.17.188:45001/sdapi/v1/sd-models";
 
         // 初始化 cURL
         $ch = curl_init();

+ 16 - 33
application/job/ImageToSingleJob.php

@@ -13,7 +13,6 @@ class ImageToSingleJob{
         $logId = $data['log_id'] ?? null;
 
         try {
-            // 任务类型校验(必须是图生图)
             if (!isset($data['type']) || $data['type'] !== '高清放大') {
                 $job->delete();
                 return;
@@ -127,65 +126,49 @@ class ImageToSingleJob{
 
     public function ImageToImage($fileName, $outputDirRaw, $new_image_url, $width, $height)
     {
-        // 统一路径分隔符
         $rootPath = str_replace('\\', '/', ROOT_PATH);
-
-        // 输出目录,如:ROOT/public/uploads/operate/ai/dall-e/
         $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
-
-        // 当前日期目录,如:2025-06-16/
         $dateDir = date('Y-m-d') . '/';
-
-        // 完整基本路径,如:ROOT/public/uploads/operate/ai/dall-e/hua/2025-06-16/
         $fullBaseDir = $outputDir . $dateDir;
 
-        // 创建输出目录,包括原图目录、目录、自定义尺寸目录
-        foreach ([$fullBaseDir, $fullBaseDir . 'newimg_679x862/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
+        foreach ([$fullBaseDir, $fullBaseDir . 'high_definition/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
             if (!is_dir($dir)) {
                 mkdir($dir, 0755, true);
             }
         }
 
-        // 从数据库中查询原图记录
         $record = Db::name('text_to_image')
             ->where('old_image_url', 'like', "%{$fileName}")
             ->order('id desc')
             ->find();
 
         if (!$record) {
-            return json([
-                'code' => 1,
-                'msg' => '没有找到匹配的图像记录'
-            ]);
+            return json(['code' => 1, 'msg' => '没有找到匹配的图像记录']);
         }
 
-        // 调用图生图 API
         $ai = new AIGatewayService();
         $res = $ai->imgtogqGptApi($new_image_url);
 
-        // 检查返回结果
         if (!isset($res['code']) || $res['code'] !== 0) {
-            return json([
-                'code' => 1,
-                'msg' => $res['msg'] ?? '图像高清放大生成失败'
-            ]);
+            return json(['code' => 1, 'msg' => $res['msg'] ?? '图像高清放大生成失败']);
         }
 
-        // 解码图像 base64 数据
+        $base64Image = $res['data']['base64_image'];
         $originalBaseName = pathinfo($new_image_url, PATHINFO_FILENAME);
         $finalFileName = $originalBaseName . '.png';
-
-        // 保存到 子目录
-        $targetDir = $fullBaseDir . 'newimg_679x862/';
+        $targetDir = $fullBaseDir . 'high_definition/';
         $savePath = $targetDir . $finalFileName;
-        file_put_contents($savePath, base64_decode($res['data']['url']));
-
-        Db::name('text_to_image')->where('id', $record['id'])->update([
-            'imgtoimg_url' => $outputDirRaw . '/' . $dateDir . 'newimg_679x862/' . $finalFileName,
-            'status_name' => '高清放大',
-            'error_msg' => '',
-            'update_time' => date('Y-m-d H:i:s')
-        ]);
+
+        if (!file_put_contents($savePath, base64_decode($base64Image))) {
+            return json(['code' => 1, 'msg' => '保存图片失败']);
+        }
+
+//        Db::name('text_to_image')->where('id', $record['id'])->update([
+//            'imgtoimg_url' => $outputDirRaw . '/' . $dateDir . 'high_definition/' . $finalFileName,
+//            'status_name' => '高清放大',
+//            'error_msg' => '',
+//            'update_time' => date('Y-m-d H:i:s')
+//        ]);
 
         return '成功';
     }

+ 126 - 44
application/service/AIGatewayService.php

@@ -212,7 +212,7 @@ class AIGatewayService{
         $base64Img = base64_encode($imgData);
         $params['init_images'] = ['data:image/png;base64,' . $base64Img];
 
-        $apiUrl = "http://20.0.17.233:45001/sdapi/v1/img2img";
+        $apiUrl = "http://20.0.17.188:45001/sdapi/v1/img2img";
         $headers = ['Content-Type: application/json'];
 
         $ch = curl_init();
@@ -260,23 +260,24 @@ class AIGatewayService{
 
     /**
      * 图片高清放大
+     * @param string $imageRelPath 原图相对路径(相对 public)
+     * @param array $options 可选参数,可覆盖默认放大配置
+     * @return array
      */
     /**
-     * 图片高清放大
+     * 图片高清放大(不保存,返回 base64)
      * @param string $imageRelPath 原图相对路径(相对 public)
-     * @param array $options 可选参数,可覆盖默认放大配置
+     * @param array $options 可选参数
      * @return array
      */
     public function imgtogqGptApi($imageRelPath, $options = [])
     {
-        // 构造图片路径
         $imgPath = ROOT_PATH . 'public/' . $imageRelPath;
 
         if (!file_exists($imgPath)) {
             return ['code' => 1, 'msg' => '原图不存在:' . $imageRelPath];
         }
 
-        // 默认放大配置
         $defaultParams = [
             'resize_mode' => 0,
             'show_extras_results' => true,
@@ -291,10 +292,8 @@ class AIGatewayService{
             'upscale_first' => false
         ];
 
-        // 合并配置参数
         $params = array_merge($defaultParams, $options);
 
-        // 编码原始图片
         try {
             $imgData = file_get_contents($imgPath);
             if ($imgData === false) {
@@ -305,10 +304,9 @@ class AIGatewayService{
             return ['code' => 1, 'msg' => '图片读取失败:' . $e->getMessage()];
         }
 
-        $apiUrl = "http://20.0.17.233:45001/sdapi/v1/extra-single-image";
+        $apiUrl = "http://20.0.17.188:45001/sdapi/v1/extra-single-image";
         $headers = ['Content-Type: application/json'];
 
-        // 调用接口
         $ch = curl_init();
         curl_setopt_array($ch, [
             CURLOPT_URL => $apiUrl,
@@ -324,17 +322,14 @@ class AIGatewayService{
         $curlErr = curl_error($ch);
         curl_close($ch);
 
-        // 网络请求失败
         if ($curlErr) {
             return ['code' => 1, 'msg' => '请求失败:' . $curlErr];
         }
 
-        // 状态码错误
         if ($httpCode !== 200) {
             return ['code' => 1, 'msg' => 'API请求失败,HTTP状态码:' . $httpCode];
         }
 
-        // 解析响应
         $data = json_decode($response, true);
         if (json_last_error() !== JSON_ERROR_NONE) {
             return ['code' => 1, 'msg' => 'API返回数据解析失败:' . json_last_error_msg()];
@@ -344,39 +339,126 @@ class AIGatewayService{
             return ['code' => 1, 'msg' => '接口未返回有效的图像数据'];
         }
 
-        // 保存新图片
-        try {
-            $baseName = pathinfo($imageRelPath, PATHINFO_FILENAME);
-            $ext = pathinfo($imageRelPath, PATHINFO_EXTENSION);
-            $outputDir = 'uploads/extra_image/';
-            $outputPath = ROOT_PATH . 'public/' . $outputDir;
-
-            if (!is_dir($outputPath)) {
-                mkdir($outputPath, 0755, true);
-            }
-
-            $saveFileName = $baseName . '-hd.' . $ext;
-            $saveFullPath = $outputPath . $saveFileName;
-            $resultImg = base64_decode($data['image']);
-
-            if ($resultImg === false || file_put_contents($saveFullPath, $resultImg) === false) {
-                throw new Exception('保存图片失败');
-            }
-
-            return [
-                'code' => 0,
-                'msg' => '高清图生成成功',
-                'data' => [
-                    'url' => '/' . $outputDir . $saveFileName,
-                    'original_size' => filesize($imgPath),
-                    'processed_size' => filesize($saveFullPath),
-                    'resolution' => getimagesize($saveFullPath)
-                ]
-            ];
-        } catch (Exception $e) {
-            return ['code' => 1, 'msg' => '保存失败:' . $e->getMessage()];
-        }
+        return [
+            'code' => 0,
+            'msg' => '高清图生成成功',
+            'data' => [
+                'base64_image' => $data['image'],
+                'original_size' => strlen($imgData),
+                'processed_size' => strlen(base64_decode($data['image']))
+            ]
+        ];
     }
+//    public function imgtogqGptApi($imageRelPath, $options = [])
+//    {
+//        // 构造图片路径
+//        $imgPath = ROOT_PATH . 'public/' . $imageRelPath;
+//
+//        if (!file_exists($imgPath)) {
+//            return ['code' => 1, 'msg' => '原图不存在:' . $imageRelPath];
+//        }
+//
+//        // 默认放大配置
+//        $defaultParams = [
+//            'resize_mode' => 0,
+//            'show_extras_results' => true,
+//            'gfpgan_visibility' => 0,
+//            'codeformer_visibility' => 0,
+//            'codeformer_weight' => 0,
+//            'upscaling_resize' => 2.45,
+//            'upscaling_crop' => true,
+//            'upscaler_1' => 'R-ESRGAN 4x+ Anime6B',
+//            'upscaler_2' => 'None',
+//            'extras_upscaler_2_visibility' => 0,
+//            'upscale_first' => false
+//        ];
+//
+//        // 合并配置参数
+//        $params = array_merge($defaultParams, $options);
+//
+//        // 编码原始图片
+//        try {
+//            $imgData = file_get_contents($imgPath);
+//            if ($imgData === false) {
+//                throw new Exception('无法读取图片文件');
+//            }
+//            $params['image'] = base64_encode($imgData);
+//        } catch (Exception $e) {
+//            return ['code' => 1, 'msg' => '图片读取失败:' . $e->getMessage()];
+//        }
+//
+//        $apiUrl = "http://20.0.17.188:45001/sdapi/v1/extra-single-image";
+//        $headers = ['Content-Type: application/json'];
+//
+//        // 调用接口
+//        $ch = curl_init();
+//        curl_setopt_array($ch, [
+//            CURLOPT_URL => $apiUrl,
+//            CURLOPT_RETURNTRANSFER => true,
+//            CURLOPT_POST => true,
+//            CURLOPT_HTTPHEADER => $headers,
+//            CURLOPT_POSTFIELDS => json_encode($params),
+//            CURLOPT_TIMEOUT => 120
+//        ]);
+//
+//        $response = curl_exec($ch);
+//        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+//        $curlErr = curl_error($ch);
+//        curl_close($ch);
+//
+//        // 网络请求失败
+//        if ($curlErr) {
+//            return ['code' => 1, 'msg' => '请求失败:' . $curlErr];
+//        }
+//
+//        // 状态码错误
+//        if ($httpCode !== 200) {
+//            return ['code' => 1, 'msg' => 'API请求失败,HTTP状态码:' . $httpCode];
+//        }
+//
+//        // 解析响应
+//        $data = json_decode($response, true);
+//        if (json_last_error() !== JSON_ERROR_NONE) {
+//            return ['code' => 1, 'msg' => 'API返回数据解析失败:' . json_last_error_msg()];
+//        }
+//
+//        if (empty($data['image'])) {
+//            return ['code' => 1, 'msg' => '接口未返回有效的图像数据'];
+//        }
+//
+//        // 保存新图片
+//        try {
+//            $baseName = pathinfo($imageRelPath, PATHINFO_FILENAME);
+//            $ext = pathinfo($imageRelPath, PATHINFO_EXTENSION);
+//            $outputDir = 'uploads/extra_image/';
+//            $outputPath = ROOT_PATH . 'public/' . $outputDir;
+//
+//            if (!is_dir($outputPath)) {
+//                mkdir($outputPath, 0755, true);
+//            }
+//
+//            $saveFileName = $baseName . '-hd.' . $ext;
+//            $saveFullPath = $outputPath . $saveFileName;
+//            $resultImg = base64_decode($data['image']);
+//
+//            if ($resultImg === false || file_put_contents($saveFullPath, $resultImg) === false) {
+//                throw new Exception('保存图片失败');
+//            }
+//
+//            return [
+//                'code' => 0,
+//                'msg' => '高清图生成成功',
+//                'data' => [
+//                    'url' => '/' . $outputDir . $saveFileName,
+//                    'original_size' => filesize($imgPath),
+//                    'processed_size' => filesize($saveFullPath),
+//                    'resolution' => getimagesize($saveFullPath)
+//                ]
+//            ];
+//        } catch (Exception $e) {
+//            return ['code' => 1, 'msg' => '保存失败:' . $e->getMessage()];
+//        }
+//    }
 
     /**
      * 通用 API 调用方法(支持重试机制)

+ 1 - 1
application/service/ImageService.php

@@ -99,7 +99,7 @@ class ImageService{
                     $insertData['model_name'] = '图生图';
                     break;
                 case '高清放大':
-                    $insertData['model'] = "";
+                    $insertData['model'] = "高清放大";
                     $insertData['model_name'] = '高清放大';
                     break;
                 default: