TextToImageJob.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. namespace app\job;
  3. use app\api\controller\Common;
  4. use app\api\controller\WorkOrder;
  5. use app\service\AIGatewayService;
  6. use app\service\ImageService;
  7. use think\Db;
  8. use think\Exception;
  9. use think\Queue;
  10. use think\queue\Job;
  11. use Redis;
  12. /**
  13. * 文生图任务处理类
  14. * 描述:接收提示词,通过模型生成图像,保存图像并记录数据库信息,是链式任务中的最后一环
  15. */
  16. class TextToImageJob
  17. {
  18. /**
  19. * 队列入口方法
  20. * @param Job $job 队列任务对象
  21. * @param array $data 任务传参,包含图像文件名、路径、尺寸、提示词等
  22. */
  23. public function fire(Job $job, $data)
  24. {
  25. if (empty($data['status_val']) || $data['status_val'] == '文生图') {
  26. // 获取任务ID
  27. $taskId = $data['task_id'];
  28. // 获取产品ID
  29. $Id = $data['id'];
  30. if (empty($taskId)) {
  31. $job->delete();
  32. return;
  33. }
  34. if (empty($Id)) {
  35. $job->delete();
  36. return;
  37. }
  38. //连接Redis(配置见 application/extra/queue.php)
  39. $redis = getTaskRedis();
  40. echo "\n" . date('Y-m-d H:i:s') . " 任务开始:{$taskId}\n";
  41. // 更新任务状态为处理中
  42. $redis->set("text_to_image_task:{$taskId}", json_encode([
  43. 'status' => ImageService::TASK_STATUS_PROCESSING,
  44. 'started_at' => date('Y-m-d H:i:s')
  45. ]), ['EX' => 300]); // 5分钟过期
  46. try {
  47. // 执行图片生成
  48. $result = $this->get_txt_to_img($data);
  49. // sleep(10);
  50. // 更新Redis中的任务状态为成功
  51. $redis->set("text_to_image_task:{$taskId}", json_encode([
  52. 'status' => ImageService::TASK_STATUS_COMPLETED,
  53. // 'image_url' => "/uploads/merchant/690377511/6903775111138/newimg/698550113c2b8.jpeg",
  54. 'image_url' => $result,
  55. 'completed_at' => date('Y-m-d H:i:s')
  56. ]), ['EX' => 300]); // 5分钟过期
  57. echo "🎉 任务 {$taskId} 执行完成,图片生成成功!\n";
  58. $job->delete();
  59. } catch (\Exception $e) {
  60. echo "❌ 任务执行失败:" . $e->getMessage() . "\n";
  61. // 检查是否是网络超时错误
  62. if (strpos($e->getMessage(), 'Connection timed out') !== false) {
  63. // 对于超时错误,保持任务状态为处理中,让前端继续查询
  64. echo "⚠️ 检测到网络超时,任务可能仍在执行中\n";
  65. $redis->set("text_to_image_task:{$taskId}", json_encode([
  66. 'status' => ImageService::TASK_STATUS_PROCESSING,
  67. 'error' => '网络连接超时,正在重试...',
  68. 'updated_at' => date('Y-m-d H:i:s')
  69. ]), ['EX' => 300]); // 5分钟过期
  70. } else {
  71. // 其他错误,标记为失败
  72. $redis->set("text_to_image_task:{$taskId}", json_encode([
  73. 'status' => ImageService::TASK_STATUS_FAILED,
  74. 'error' => $e->getMessage(),
  75. 'completed_at' => date('Y-m-d H:i:s')
  76. ]), ['EX' => 300]); // 5分钟过期
  77. }
  78. $job->delete();
  79. } finally {
  80. $job->delete();
  81. }
  82. } else {
  83. $logId = $data['log_id'] ?? null;
  84. try {
  85. // 任务类型校验(必须是文生图)
  86. if (!isset($data['type']) || $data['type'] !== '文生图') {
  87. $job->delete();
  88. return;
  89. }
  90. $startTime = date('Y-m-d H:i:s');
  91. echo "━━━━━━━━━━ ▶ 文生图任务开始处理━━━━━━━━━━\n";
  92. echo "处理时间:{$startTime}\n";
  93. // 更新日志状态:处理中
  94. if ($logId) {
  95. Db::name('image_task_log')->where('id', $logId)->update([
  96. 'status' => 1,
  97. 'log' => '文生图处理中',
  98. 'update_time' => $startTime
  99. ]);
  100. }
  101. // 拼接原图路径
  102. $old_image_url = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/');
  103. $list = Db::name("text_to_image")
  104. ->where('old_image_url', $old_image_url)
  105. ->where('img_name', '<>', '')
  106. // ->where('status', 0)
  107. ->select();
  108. if (!empty($list)) {
  109. $total = count($list);
  110. echo "📊 共需处理:{$total} 条记录\n";
  111. foreach ($list as $index => $row) {
  112. $currentIndex = $index + 1;
  113. $begin = date('Y-m-d H:i:s');
  114. echo "👉 正在处理第 {$currentIndex} 条,ID: {$row['id']}\n";
  115. // 图像生成
  116. $result = $this->textToImage(
  117. $data["file_name"],
  118. $data["outputDir"],
  119. $data["width"],
  120. $data["height"],
  121. $row["chinese_description"],
  122. $row["img_name"],
  123. $data["selectedOption"],
  124. $data["executeKeywords"],
  125. $data['sourceDir']
  126. );
  127. // 标准化结果文本
  128. if ($result === true || $result === 1 || $result === '成功') {
  129. $resultText = '成功';
  130. // 日志状态设置为成功(仅在未提前失败时)
  131. if ($logId) {
  132. Db::name('image_task_log')->where('id', $logId)->update([
  133. 'status' => 2,
  134. 'log' => '文生图处理成功',
  135. 'update_time' => date('Y-m-d H:i:s')
  136. ]);
  137. }
  138. } else {
  139. $resultText = (string) $result ?: '失败或无返回';
  140. }
  141. echo "✅ 处理结果:{$resultText}\n";
  142. echo "完成时间:" . date('Y-m-d H:i:s') . "\n";
  143. echo "文生图已处理完成\n";
  144. // 若包含关键词,日志状态标为失败(-1)
  145. if (strpos($resultText, '包含关键词') !== false) {
  146. // 命中关键词类错误,状态设为失败
  147. if ($logId) {
  148. Db::name('image_task_log')->where('id', $logId)->update([
  149. 'status' => -1,
  150. 'log' => $resultText,
  151. 'update_time' => date('Y-m-d H:i:s')
  152. ]);
  153. }
  154. }
  155. }
  156. }
  157. // 处理链式任务(如果有)
  158. if (!empty($data['chain_next'])) {
  159. $nextType = array_shift($data['chain_next']);
  160. $data['type'] = $nextType;
  161. Queue::push('app\job\ImageArrJob', [
  162. 'task_id' => $data['task_id'],
  163. 'data' => [$data]
  164. ], 'arrimage');
  165. }
  166. $job->delete();
  167. } catch (\Exception $e) {
  168. echo "❌ 异常信息: " . $e->getMessage() . "\n";
  169. echo "📄 文件: " . $e->getFile() . "\n";
  170. echo "📍 行号: " . $e->getLine() . "\n";
  171. if ($logId) {
  172. Db::name('image_task_log')->where('id', $logId)->update([
  173. 'status' => -1,
  174. 'log' => '文生图失败:' . $e->getMessage(),
  175. 'update_time' => date('Y-m-d H:i:s')
  176. ]);
  177. }
  178. $job->delete();
  179. }
  180. }
  181. $job->delete();
  182. }
  183. /**
  184. * 任务失败时的处理
  185. */
  186. public function failed($data)
  187. {
  188. // 记录失败日志或发送通知
  189. echo "ImageJob failed: " . json_encode($data);
  190. }
  191. public function get_txt_to_img($data){
  192. $status_val = trim($data['status_val']);
  193. $prompt = trim($data['prompt']);
  194. $model = trim($data['model']);
  195. $size = trim($data['size']);
  196. // 获取产品信息
  197. $product = Db::name('product')->where('id', $data['id'])->find();
  198. if (empty($product)) {
  199. return '产品不存在';
  200. }
  201. // 调用AI生成图片
  202. $aiGateway = new AIGatewayService();
  203. $res = $aiGateway->buildRequestData($status_val,$model,$prompt,$size);
  204. // 提取base64图片数据,兼容两种返回格式
  205. $base64_data = null;
  206. $image_type = 'png';
  207. // 新格式:data[0].b64_json 或 data[0].url
  208. if (isset($res['data'][0]['b64_json']) && $res['data'][0]['b64_json']) {
  209. $base64_data = preg_replace('/\s+/', '', $res['data'][0]['b64_json']);
  210. } elseif (isset($res['data'][0]['url']) && $res['data'][0]['url']) {
  211. $imageContent = file_get_contents($res['data'][0]['url']);
  212. $base64_data = base64_encode($imageContent);
  213. }
  214. // 旧格式:candidates[0].content.parts[0].inlineData.data 或 text
  215. elseif (isset($res['candidates'][0]['content']['parts'][0]['inlineData']['data'])) {
  216. $text_content = $res['candidates'][0]['content']['parts'][0]['inlineData']['data'];
  217. // 带前缀 data:image/xxx;base64, 或 裸 base64 都能识别
  218. if (preg_match('/data:image\/(png|jpg|jpeg|webp);base64,(.+)$/is', $text_content, $matches)) {
  219. $image_type = ($matches[1] == 'jpg' ? 'jpeg' : $matches[1]);
  220. $base64_data = preg_replace('/\s+/', '', $matches[2]);
  221. } else {
  222. $base64_data = preg_replace('/\s+/', '', $text_content);
  223. }
  224. } elseif (isset($res['candidates'][0]['content']['parts'][0]['text'])) {
  225. $text_content = $res['candidates'][0]['content']['parts'][0]['text'];
  226. if (preg_match('/data:image\/(png|jpg|jpeg|webp);base64,(.+)$/is', $text_content, $matches)) {
  227. $image_type = ($matches[1] == 'jpg' ? 'jpeg' : $matches[1]);
  228. $base64_data = preg_replace('/\s+/', '', $matches[2]);
  229. } else {
  230. $base64_data = preg_replace('/\s+/', '', $text_content);
  231. }
  232. }
  233. if (empty($base64_data)) {
  234. return '未找到图片数据';
  235. }
  236. $image_data = base64_decode($base64_data);
  237. if ($image_data === false) {
  238. return '图片解码失败';
  239. }
  240. $rootPath = str_replace('\\', '/', ROOT_PATH);
  241. $relDir = '/uploads/ceshi/';
  242. // 生产:$code = $product['product_code'];
  243. // 生产:$relDir = '/uploads/merchant/' . substr($code, 0, 9) . '/' . $code . '/newimg/';
  244. $saveDir = rtrim($rootPath, '/') . '/public' . $relDir;
  245. if (!is_dir($saveDir)) {
  246. mkdir($saveDir, 0755, true);
  247. }
  248. $file_name = uniqid() . '.' . $image_type;
  249. if (!file_put_contents($saveDir . $file_name, $image_data)) {
  250. return '图片保存失败';
  251. }
  252. $db_img_path = $relDir . $file_name;
  253. // 本地落盘成功后同步 OSS(失败不阻断主流程)
  254. Common::uploadLocalFileToOss((string)($saveDir . $file_name), (string)$db_img_path);
  255. Db::name('product')->where('id', $data['id'])->update([
  256. 'createTime' => date('Y-m-d H:i:s'),
  257. 'content' => $data['prompt'],
  258. 'product_new_img' => $db_img_path
  259. ]);
  260. $record = [
  261. 'product_id' => $data['id'],
  262. 'product_new_img' => $db_img_path,
  263. 'product_content' => $data['prompt'],
  264. 'template_id' => $data['template_id'] ?? 0,
  265. 'createTime' => date('Y-m-d H:i:s'),
  266. ];
  267. Db::name('product_image')->insert($record);
  268. return $db_img_path;
  269. }
  270. /**
  271. * 文生图处理函数
  272. * 描述:根据提示词调用图像生成接口,保存图像文件,并更新数据库
  273. */
  274. public function textToImage($data,$prompt,$img_name)
  275. {
  276. $fileName = $data["file_name"];
  277. $outputDirRaw = $data["outputDir"];
  278. $width = $data["width"];
  279. $height = $data["height"];
  280. $selectedOption = $data["selectedOption"];
  281. $executeKeywords = $data["executeKeywords"];
  282. $sourceDir = $data["sourceDir"];
  283. $rootPath = str_replace('\\', '/', ROOT_PATH);
  284. $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
  285. $dateDir = date('Y-m-d') . '/';
  286. $fullBaseDir = $outputDir . $dateDir;
  287. // 创建输出目录
  288. foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
  289. if (!is_dir($dir)) mkdir($dir, 0755, true);
  290. }
  291. // 确保目录存在
  292. if (!is_dir($fullBaseDir . '2048x2048/')) {
  293. mkdir($fullBaseDir . '2048x2048/', 0755, true);
  294. }
  295. // 获取图像记录
  296. $record = Db::name('text_to_image')
  297. ->where('old_image_url', 'like', "%{$fileName}")
  298. ->order('id desc')
  299. ->find();
  300. Db::name('text_to_image')->where('id', $record['id'])->update([
  301. 'new_image_url' => '',
  302. ]);
  303. if (!$record) return '没有找到匹配的图像记录';
  304. //判断是否执行几何图
  305. if($executeKeywords == false){
  306. // 过滤关键词
  307. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  308. foreach (['几何', 'geometry', 'geometric'] as $keyword) {
  309. if (stripos($prompt, $keyword) !== false) {
  310. Db::name('text_to_image')->where('id', $record['id'])->update([
  311. 'status' => 3,
  312. 'error_msg' => "包含关键词".$keyword,
  313. 'update_time' => date('Y-m-d H:i:s')
  314. ]);
  315. return "包含关键词 - {$keyword}";
  316. }
  317. }
  318. }
  319. $template = Db::name('template')
  320. ->field('id,english_content,content')
  321. ->where('path',$sourceDir)
  322. ->where('ids',1)
  323. ->find();
  324. // AI 图像生成调用
  325. $ai = new AIGatewayService();
  326. $response = $ai->buildRequestData('文生图',$template['content'].$prompt, $selectedOption);
  327. if (isset($response['error'])) {
  328. throw new \Exception("❌ 图像生成失败:" . $response['error']['message']);
  329. }
  330. // 支持 URL 格式(为主)和 base64
  331. $imgData = null;
  332. if (isset($res['candidates'][0]['content']['parts'][0]['text'])) {
  333. $text_content = $res['candidates'][0]['content']['parts'][0]['text'];
  334. // 匹配base64图片数据
  335. preg_match('/data:image\/(png|jpg|jpeg);base64,([^"]+)/', $text_content, $matches);
  336. if (empty($matches)) {
  337. return '未找到图片数据';
  338. }
  339. $image_type = $matches[1];
  340. $base64_data = $matches[2];
  341. // 解码base64数据
  342. $imgData = base64_decode($base64_data);
  343. }else if (isset($response['data'][0]['url'])) {
  344. $imgData = @file_get_contents($response['data'][0]['url']);
  345. } elseif (isset($response['data'][0]['b64_json'])) {
  346. $imgData = base64_decode($response['data'][0]['b64_json']);
  347. }
  348. if (!$imgData || strlen($imgData) < 1000) {
  349. throw new \Exception("❌ 图像内容为空或异常!");
  350. }
  351. // 保存文件路径定义
  352. $img_name = mb_substr(preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name), 0, 30);
  353. $filename = $img_name . '.png';
  354. $path512 = $fullBaseDir . '1024x1024/' . $filename;
  355. $pathCustom = $fullBaseDir . "{$width}x{$height}/" . $filename;
  356. // 保存原图
  357. file_put_contents($path512, $imgData);
  358. // 数据库更新
  359. Db::name('text_to_image')->where('id', $record['id'])->update([
  360. 'new_image_url' => str_replace($rootPath . 'public/', '', $path512),
  361. // 注释以下一行则不保存裁剪路径(适配你的配置)
  362. // 'custom_image_url' => str_replace($rootPath . 'public/', '', $pathCustom),
  363. 'img_name' => $img_name,
  364. 'model' => $selectedOption,
  365. 'status' => trim($img_name) === '' ? 0 : 1,
  366. 'status_name' => "文生图",
  367. 'size' => "{$width}x{$height}",
  368. 'quality' => 'standard',
  369. 'style' => 'vivid',
  370. 'error_msg' => '',
  371. 'update_time' => date('Y-m-d H:i:s')
  372. ]);
  373. return "成功";
  374. }
  375. public function getImageSeed($taskId)
  376. {
  377. // 配置参数
  378. $apiUrl = 'https://chatapi.onechats.ai/mj/task/' . $taskId . '/fetch';
  379. $apiKey = 'sk-iURfrAgzAjhZ4PpPLwzmWIAhM7zKfrkwDvyxk4RVBQ4ouJNK';
  380. try {
  381. // 初始化cURL
  382. $ch = curl_init();
  383. // 设置cURL选项
  384. curl_setopt_array($ch, [
  385. CURLOPT_URL => $apiUrl,
  386. CURLOPT_RETURNTRANSFER => true,
  387. CURLOPT_CUSTOMREQUEST => 'GET', // 明确指定GET方法
  388. CURLOPT_HTTPHEADER => [
  389. 'Authorization: Bearer ' . $apiKey,
  390. 'Accept: application/json',
  391. 'Content-Type: application/json'
  392. ],
  393. CURLOPT_SSL_VERIFYPEER => false,
  394. CURLOPT_SSL_VERIFYHOST => false,
  395. CURLOPT_TIMEOUT => 60,
  396. CURLOPT_FAILONERROR => true // 添加失败时返回错误
  397. ]);
  398. // 执行请求
  399. $response = curl_exec($ch);
  400. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  401. // 错误处理
  402. if (curl_errno($ch)) {
  403. throw new Exception('cURL请求失败: ' . curl_error($ch));
  404. }
  405. // 关闭连接
  406. curl_close($ch);
  407. // 验证HTTP状态码
  408. if ($httpCode < 200 || $httpCode >= 300) {
  409. throw new Exception('API返回错误状态码: ' . $httpCode);
  410. }
  411. // 解析JSON响应
  412. $responseData = json_decode($response, true);
  413. if (json_last_error() !== JSON_ERROR_NONE) {
  414. throw new Exception('JSON解析失败: ' . json_last_error_msg());
  415. }
  416. // 返回结构化数据
  417. return [
  418. 'success' => true,
  419. 'http_code' => $httpCode,
  420. 'data' => $responseData
  421. ];
  422. } catch (Exception $e) {
  423. // 确保关闭cURL连接
  424. if (isset($ch) && is_resource($ch)) {
  425. curl_close($ch);
  426. }
  427. return [
  428. 'success' => false,
  429. 'error' => $e->getMessage(),
  430. 'http_code' => $httpCode ?? 0
  431. ];
  432. }
  433. }
  434. /**
  435. * 发送API请求
  436. * @param string $url 请求地址
  437. * @param array $data 请求数据
  438. * @param string $apiKey API密钥
  439. * @param string $method 请求方法
  440. * @return string 响应内容
  441. * @throws Exception
  442. */
  443. private function sendApiRequest($url, $data, $apiKey, $method = 'POST')
  444. {
  445. $ch = curl_init();
  446. curl_setopt_array($ch, [
  447. CURLOPT_URL => $url,
  448. CURLOPT_RETURNTRANSFER => true,
  449. CURLOPT_CUSTOMREQUEST => $method,
  450. CURLOPT_HTTPHEADER => [
  451. 'Authorization: Bearer '.$apiKey,
  452. 'Accept: application/json',
  453. 'Content-Type: application/json'
  454. ],
  455. CURLOPT_POSTFIELDS => $method === 'POST' ? json_encode($data) : null,
  456. CURLOPT_SSL_VERIFYPEER => false,
  457. CURLOPT_SSL_VERIFYHOST => false,
  458. CURLOPT_TIMEOUT => 60,
  459. CURLOPT_FAILONERROR => true
  460. ]);
  461. $response = curl_exec($ch);
  462. $error = curl_error($ch);
  463. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  464. curl_close($ch);
  465. if ($error) {
  466. throw new Exception('API请求失败: '.$error);
  467. }
  468. if ($httpCode < 200 || $httpCode >= 300) {
  469. throw new Exception('API返回错误状态码: '.$httpCode);
  470. }
  471. return $response;
  472. }
  473. }