ImageService.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\service;
  3. use app\service\AIGatewayService;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Queue;
  7. /**
  8. * ImageService 类用于处理图像任务和存放日志队列。
  9. * 该类将前端传过来的多个图像信息推送到处理队列中。
  10. */
  11. class ImageService{
  12. /**
  13. * 直接调用图生文API并返回结果
  14. * @param array $params 请求参数,包含文生文提示词、模型类型等
  15. * @return array GPT生成的结果
  16. */
  17. public function handleImgToText($params) {
  18. Queue::push('app\job\ImageArrJob', $params, "arrimage");
  19. return true;
  20. }
  21. /**
  22. * 文生文
  23. * @param array $params 请求参数,包含文生文提示词、模型类型等
  24. * @return array GPT生成的结果
  25. */
  26. public function handleTextToText($prompt,$model) {
  27. $ai = new AIGatewayService();
  28. $gptRes = $ai->txtGptApi($prompt, $model);
  29. // 根据不同模型解析返回结果
  30. $gptText = '';
  31. if (strpos($model, 'gemini') !== false && isset($gptRes['candidates'][0]['content']['parts'][0]['text'])) {
  32. $gptText = trim($gptRes['candidates'][0]['content']['parts'][0]['text']);
  33. } elseif (isset($gptRes['choices'][0]['message']['content'])) {
  34. $gptText = trim($gptRes['choices'][0]['message']['content']);
  35. } else {
  36. throw new Exception('AI API返回格式错误');
  37. }
  38. return [
  39. 'success' => true,
  40. 'message' => '生成成功',
  41. 'data' => $gptText
  42. ];
  43. }
  44. /**
  45. * 直接调用文生图API并返回结果
  46. * @param array $params 请求参数,包含文生文提示词、模型类型等
  47. * @return array GPT生成的结果
  48. */
  49. public function handleTextToImg($params) {
  50. // 生成唯一任务ID,格式:$params['id']-年月日时间-随机四位数字
  51. $id = $params['id'];
  52. $time = date('YmdHis');
  53. $random = mt_rand(1000, 9999);
  54. $taskId = "{$id}-{$time}-{$random}";
  55. $params['task_id'] = $taskId;
  56. // 将任务状态存储到Redis
  57. $redis = new \Redis();
  58. $redis->connect('127.0.0.1', 6379);
  59. $redis->auth('123456');
  60. $redis->select(15);
  61. $redis->set("text_to_image_task:{$taskId}", json_encode([
  62. 'status' => 'pending',
  63. 'created_at' => date('Y-m-d H:i:s')
  64. ]), ['EX' => 300]); // 5分钟过期
  65. // 将任务推送到队列
  66. Queue::push('app\job\ImageArrJob', $params, "arrimage");
  67. // 返回任务ID
  68. return ['success' => true, 'message' => '正在生成图片中,请稍等.....', 'task_id' => $taskId];
  69. }
  70. /**
  71. * 推送图像任务到队列(支持链式和单独模式)
  72. * @param array $params 请求参数,包含图像批次、模型类型、尺寸等
  73. */
  74. public function handleImage($params) {
  75. if (!isset($params["batch"])) {return false;}
  76. $arr = [];
  77. // 获取图像批量信息
  78. $batch = $params["batch"];
  79. // 获取执行次数数量
  80. $num = $params["num"];
  81. /*获取ids为1模板
  82. * english_content 文生文提示词
  83. * content 图生文提示词
  84. * */
  85. $template = Db::name('template')
  86. ->field('id,english_content,content,ids')
  87. ->where('path',$params['old_image_file'])
  88. ->where('ids',1)
  89. ->find();
  90. // 构建任务基础结构(每图生成 N 份任务)
  91. foreach ($batch as $k => $v) {
  92. $baseItem = [
  93. "sourceDir" => $this->sourceDir($v, 1),
  94. "outputDir" => $this->sourceDir($v, 2),
  95. "file_name" => $this->sourceDir($v, 3),
  96. "type" => $params['type'] ?? '',
  97. "selectedOption" => $params['selectedOption'],//文生图模型
  98. "txttotxt_selectedOption" => $params['txttotxt_selectedOption'],//文生文模型
  99. "imgtotxt_selectedOption" => $params['imgtotxt_selectedOption'],//图生文模型
  100. "prompt" => '',
  101. "width" => $params['width'],
  102. "height" => $params['height'],
  103. "executeKeywords" => $params['executeKeywords'],//是否执行几何图
  104. "sys_id" => $params['sys_id']//用户
  105. ];
  106. // 创建$num个相同的项目并合并到$arr
  107. $arr = array_merge($arr, array_fill(0, $num, $baseItem));
  108. }
  109. // 插入队列日志
  110. $insertData = [
  111. 'create_time' => date('Y-m-d H:i:s'),
  112. 'old_image_file' => $params['old_image_file'],
  113. 'status' => '等待中',
  114. 'image_count' => count($arr),
  115. 'params' => json_encode($params, JSON_UNESCAPED_UNICODE)
  116. ];
  117. //模型任务类型处理
  118. if (empty($params['type'])) {
  119. /*
  120. * 执行全部任务时一键链式任务队列
  121. * 用于存放队列日志
  122. * 链式任务:图生文 → 文生文 → 文生图
  123. * */
  124. $insertData['model'] = "gpt-4-vision-preview,"."gpt-4,".$params['selectedOption'];
  125. $insertData['model_name'] = '文生图';
  126. $task_id = Db::name('queue_logs')->insertGetId($insertData);
  127. $arr = array_map(function ($item) use ($task_id) {
  128. $item['type'] = '图生文';
  129. $item['chain_next'] = ['文生文', '文生图','图生图','高清放大'];
  130. $item['task_id'] = $task_id;
  131. return $item;
  132. }, $arr);
  133. $payload = [
  134. 'task_id' => $task_id,
  135. 'data' => $arr
  136. ];
  137. Queue::push('app\job\ImageArrJob', $payload, "arrimage");
  138. } else {
  139. // 指定单个独立任务类型
  140. switch ($params['type']) {
  141. case '图生文':
  142. $insertData['model'] = 'gpt-4-vision-preview';
  143. $insertData['model_name'] = '图生文';
  144. break;
  145. case '文生文':
  146. $insertData['model'] = $params['txttotxt_selectedOption'];
  147. $insertData['model_name'] = '文生文';
  148. break;
  149. case '文生图':
  150. $insertData['model'] = $params['selectedOption'];
  151. $insertData['model_name'] = '文生图';
  152. break;
  153. case '图生图':
  154. $insertData['model'] = "realisticVisionV51_v51VAE-inpainting.safetensors [f0d4872d24]";
  155. $insertData['model_name'] = '图生图';
  156. break;
  157. case '高清放大':
  158. $insertData['model'] = "高清放大";
  159. $insertData['model_name'] = '高清放大';
  160. break;
  161. default:
  162. return false;
  163. }
  164. //将一组队列存放queue_logs任务表中,将新增id最为任务id记录
  165. $task_id = Db::name('queue_logs')->insertGetId($insertData);
  166. $arr = array_map(function ($item) use ($params, $task_id) {
  167. $item['type'] = $params['type'];
  168. $item['task_id'] = $task_id;
  169. return $item;
  170. }, $arr);
  171. // 投递任务到队列
  172. $payload = [
  173. 'task_id' => $task_id,
  174. 'data' => $arr
  175. ];
  176. Queue::push('app\job\ImageArrJob', $payload, "arrimage");
  177. }
  178. return true;
  179. }
  180. /**
  181. * 解析图像路径,返回不同组成部分
  182. *
  183. * @param string $filePath 图像路径(如 uploads/operate/ai/Preview/20240610/xxx.png)
  184. * @param int $type 返回内容类型:
  185. * 1 = 基础路径(去掉日期+文件名)
  186. * sourceDir 源目录uploads/operate/ai/Preview/
  187. * 2 = 输出路径(Preview 替换为 dall-e,并加日期代表当天数据存到当天文件夹中)
  188. * outputDir 输出目录/uploads/operate/ai/dall-e/hua/并加日期
  189. * 3 = 文件名
  190. * file_name 文件名0194b6fdd6203fda369d5e3b74b6b454.png
  191. * @return string|null
  192. */
  193. public function sourceDir($filePath, $type) {
  194. $arr = [];
  195. $pathParts = explode('/', $filePath);
  196. $filename = array_pop($pathParts); // 最后是文件名
  197. $baseParts = $pathParts;
  198. // 查找是否有 8 位数字(即日期)文件夹
  199. $date = '';
  200. foreach ($pathParts as $index => $part) {
  201. if (preg_match('/^\d{8}$/', $part)) {
  202. $date = $part;
  203. unset($baseParts[$index]); // 日期不算在 basePath 里
  204. break;
  205. }
  206. }
  207. $arr = [
  208. 'basePath' => implode('/', $baseParts),
  209. 'date' => $date,
  210. 'filename' => $filename
  211. ];
  212. // 根据类型返回不同路径
  213. if ($type == 1) {
  214. return $arr["basePath"];
  215. }
  216. if ($type == 2) {
  217. return '/' . str_replace('/Preview/', '/dall-e/', $arr["basePath"]) . $arr["date"];
  218. }
  219. if ($type == 3) {
  220. return $arr["filename"];
  221. }
  222. }
  223. }