TextToImageJob.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace app\job;
  3. use app\service\AIGatewayService;
  4. use think\Db;
  5. use think\Queue;
  6. use think\queue\Job;
  7. /**
  8. * 文生图任务处理类
  9. * 描述:接收提示词,通过模型生成图像,保存图像并记录数据库信息,是链式任务中的最后一环
  10. */
  11. class TextToImageJob
  12. {
  13. /**
  14. * 队列入口方法
  15. * @param Job $job 队列任务对象
  16. * @param array $data 任务传参,包含图像文件名、路径、尺寸、提示词等
  17. */
  18. public function fire(Job $job, $data)
  19. {
  20. $logId = $data['log_id'] ?? null;
  21. try {
  22. // 任务类型校验(必须是文生图)
  23. if (!isset($data['type']) || $data['type'] !== '文生图') {
  24. $job->delete();
  25. return;
  26. }
  27. $startTime = date('Y-m-d H:i:s');
  28. echo "━━━━━━━━━━ ▶ 文生图任务开始处理━━━━━━━━━━\n";
  29. echo "处理时间:{$startTime}\n";
  30. // 更新日志状态:处理中
  31. if ($logId) {
  32. Db::name('image_task_log')->where('id', $logId)->update([
  33. 'status' => 1,
  34. 'log' => '文生图处理中',
  35. 'update_time' => $startTime
  36. ]);
  37. }
  38. // 拼接原图路径
  39. $old_image_url = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/');
  40. $list = Db::name("text_to_image")
  41. ->where('old_image_url', $old_image_url)
  42. ->where('img_name', '<>', '')
  43. ->where('status', 0)
  44. ->select();
  45. if (!empty($list)) {
  46. $total = count($list);
  47. echo "📊 共需处理:{$total} 条记录\n";
  48. foreach ($list as $index => $row) {
  49. $currentIndex = $index + 1;
  50. $begin = date('Y-m-d H:i:s');
  51. echo "👉 正在处理第 {$currentIndex} 条,ID: {$row['id']}\n";
  52. // 图像生成
  53. $result = $this->textToImage(
  54. $data["file_name"],
  55. $data["outputDir"],
  56. $data["width"],
  57. $data["height"],
  58. $row["english_description"],
  59. $row["img_name"],
  60. $data["selectedOption"]
  61. );
  62. // 标准化结果文本
  63. if ($result === true || $result === 1 || $result === '成功') {
  64. $resultText = '成功';
  65. // 日志状态设置为成功(仅在未提前失败时)
  66. if ($logId) {
  67. Db::name('image_task_log')->where('id', $logId)->update([
  68. 'status' => 2,
  69. 'log' => '文生图处理成功',
  70. 'update_time' => date('Y-m-d H:i:s')
  71. ]);
  72. }
  73. } else {
  74. $resultText = (string) $result ?: '失败或无返回';
  75. }
  76. echo "✅ 处理结果:{$resultText}\n";
  77. echo "完成时间:" . date('Y-m-d H:i:s') . "\n";
  78. echo "文生图已处理完成\n";
  79. // 若包含关键词,日志状态标为失败(-1)
  80. if (strpos($resultText, '包含关键词') !== false) {
  81. // 命中关键词类错误,状态设为失败
  82. if ($logId) {
  83. Db::name('image_task_log')->where('id', $logId)->update([
  84. 'status' => -1,
  85. 'log' => $resultText,
  86. 'update_time' => date('Y-m-d H:i:s')
  87. ]);
  88. }
  89. }
  90. }
  91. }
  92. // 处理链式任务(如果有)
  93. if (!empty($data['chain_next'])) {
  94. $nextType = array_shift($data['chain_next']);
  95. $data['type'] = $nextType;
  96. Queue::push('app\job\ImageArrJob', [
  97. 'task_id' => $data['task_id'],
  98. 'data' => [$data]
  99. ], 'arrimage');
  100. }
  101. $job->delete();
  102. } catch (\Exception $e) {
  103. echo "❌ 异常信息: " . $e->getMessage() . "\n";
  104. echo "📄 文件: " . $e->getFile() . "\n";
  105. echo "📍 行号: " . $e->getLine() . "\n";
  106. if ($logId) {
  107. Db::name('image_task_log')->where('id', $logId)->update([
  108. 'status' => -1,
  109. 'log' => '文生图失败:' . $e->getMessage(),
  110. 'update_time' => date('Y-m-d H:i:s')
  111. ]);
  112. }
  113. $job->delete();
  114. }
  115. }
  116. /**
  117. * 任务失败时的处理
  118. */
  119. public function failed($data)
  120. {
  121. // 记录失败日志或发送通知
  122. echo "ImageJob failed: " . json_encode($data);
  123. }
  124. /**
  125. * 文生图处理函数
  126. * 描述:根据提示词调用图像生成接口,保存图像文件,并更新数据库
  127. */
  128. public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name, $selectedOption)
  129. {
  130. $rootPath = str_replace('\\', '/', ROOT_PATH);
  131. $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
  132. $dateDir = date('Y-m-d') . '/';
  133. $fullBaseDir = $outputDir . $dateDir;
  134. // 创建输出目录
  135. foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
  136. if (!is_dir($dir)) mkdir($dir, 0755, true);
  137. }
  138. // 获取图像记录
  139. $record = Db::name('text_to_image')
  140. ->where('old_image_url', 'like', "%{$fileName}")
  141. ->order('id desc')
  142. ->find();
  143. if (!$record) return '没有找到匹配的图像记录';
  144. // 过滤关键词
  145. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  146. foreach (['几何', 'geometry', 'geometric'] as $keyword) {
  147. if (stripos($prompt, $keyword) !== false) {
  148. Db::name('text_to_image')->where('id', $record['id'])->update([
  149. 'status' => 3,
  150. 'error_msg' => "包含关键词".$keyword,
  151. 'update_time' => date('Y-m-d H:i:s')
  152. ]);
  153. return "包含关键词 - {$keyword}";
  154. }
  155. }
  156. // AI 图像生成调用
  157. $ai = new AIGatewayService();
  158. $response = $ai->callDalleApi($prompt, $selectedOption);
  159. if (isset($response['error'])) {
  160. throw new \Exception("❌ 图像生成失败:" . $response['error']['message']);
  161. }
  162. // 支持 URL 格式(为主)和 base64
  163. $imgData = null;
  164. if (isset($response['data'][0]['url'])) {
  165. $imgData = @file_get_contents($response['data'][0]['url']);
  166. } elseif (isset($response['data'][0]['b64_json'])) {
  167. $imgData = base64_decode($response['data'][0]['b64_json']);
  168. }
  169. if (!$imgData || strlen($imgData) < 1000) {
  170. throw new \Exception("❌ 图像内容为空或异常!");
  171. }
  172. // 保存文件路径定义
  173. $img_name = mb_substr(preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name), 0, 30);
  174. $filename = $img_name . '.png';
  175. $path512 = $fullBaseDir . '1024x1024/' . $filename;
  176. $pathCustom = $fullBaseDir . "{$width}x{$height}/" . $filename;
  177. // 保存原图
  178. file_put_contents($path512, $imgData);
  179. // ➤ 是否执行裁剪(如不想裁剪,可注释以下 try-catch 整块)
  180. try {
  181. $im = imagecreatefromstring($imgData);
  182. if (!$im) throw new \Exception("图像无法解析");
  183. $srcW = imagesx($im);
  184. $srcH = imagesy($im);
  185. $srcRatio = $srcW / $srcH;
  186. $dstRatio = $width / $height;
  187. if ($srcRatio > $dstRatio) {
  188. $cropW = intval($srcH * $dstRatio);
  189. $cropH = $srcH;
  190. $srcX = intval(($srcW - $cropW) / 2);
  191. $srcY = 0;
  192. } else {
  193. $cropW = $srcW;
  194. $cropH = intval($srcW / $dstRatio);
  195. $srcX = 0;
  196. $srcY = intval(($srcH - $cropH) / 2);
  197. }
  198. $dstImg = imagecreatetruecolor($width, $height);
  199. imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropW, $cropH);
  200. imagepng($dstImg, $pathCustom);
  201. imagedestroy($im);
  202. imagedestroy($dstImg);
  203. } catch (\Throwable $e) {
  204. file_put_contents('/tmp/crop_error.png', $imgData);
  205. throw new \Exception("图像裁剪失败:" . $e->getMessage());
  206. }
  207. // 数据库更新
  208. Db::name('text_to_image')->where('id', $record['id'])->update([
  209. 'new_image_url' => str_replace($rootPath . 'public/', '', $path512),
  210. // 注释以下一行则不保存裁剪路径(适配你的配置)
  211. 'custom_image_url' => str_replace($rootPath . 'public/', '', $pathCustom),
  212. 'img_name' => $img_name,
  213. 'model' => $selectedOption,
  214. 'status' => trim($img_name) === '' ? 0 : 1,
  215. 'status_name' => "文生图",
  216. 'size' => "{$width}x{$height}",
  217. 'quality' => 'standard',
  218. 'style' => 'vivid',
  219. 'error_msg' => '',
  220. 'update_time' => date('Y-m-d H:i:s')
  221. ]);
  222. return "成功";
  223. }
  224. }