TextToImageJob.php 20 KB

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