TextToImageJob.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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)) {
  137. mkdir($dir, 0755, true);
  138. }
  139. }
  140. // 查询数据库记录
  141. $record = Db::name('text_to_image')
  142. ->where('old_image_url', 'like', "%{$fileName}")
  143. ->order('id desc')
  144. ->find();
  145. if (!$record) {
  146. return '没有找到匹配的图像记录';
  147. }
  148. // 写入 prompt 日志
  149. $logDir = $rootPath . 'runtime/logs/';
  150. if (!is_dir($logDir)) mkdir($logDir, 0755, true);
  151. // 调用文生图模型接口生成图像
  152. $startTime = microtime(true);
  153. // 清理 prompt 的换行
  154. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  155. // 定义要跳过的关键词(可按需扩展)
  156. $skipKeywords = ['几何', 'geometry', 'geometric'];
  157. foreach ($skipKeywords as $keyword) {
  158. // 判断提示词中是否包含关键词(不区分大小写)
  159. if (stripos($prompt, $keyword) !== false) {
  160. $skipId = $record['id'];
  161. echo "🚫 跳过生成:包含关键词“{$keyword}”,记录 ID:{$skipId}\n";
  162. Db::name('text_to_image')->where('id', $skipId)->update([
  163. 'status' => 3,
  164. 'error_msg' => "包含关键词".$keyword,
  165. 'update_time' => date('Y-m-d H:i:s')
  166. ]);
  167. return "包含关键词 - {$keyword}";
  168. }
  169. }
  170. //文生图调用
  171. $startTime = microtime(true);
  172. $ai = new AIGatewayService();
  173. $dalle1024 = $ai->callDalleApi($prompt,$selectedOption);
  174. $endTime = microtime(true);
  175. $executionTime = $endTime - $startTime;
  176. echo "✅ API 调用耗时: " . round($executionTime, 3) . " 秒\n";
  177. // 错误检查
  178. if (isset($dalle1024['error'])) {
  179. throw new \Exception("❌ 图像生成接口错误:" . ($dalle1024['error']['message'] ?? '未知错误'));
  180. }
  181. // 提取 url 图像
  182. // $base64Image = $dalle1024['data'][0]['url'] ?? null;
  183. // 提取 base64 图像
  184. $base64Image = $dalle1024['data'][0]['b64_json'] ?? null;
  185. if (!$base64Image || strlen($base64Image) < 1000) {
  186. file_put_contents('/tmp/empty_image_base64.txt', json_encode($dalle1024, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  187. throw new \Exception("❌ 图像内容为空或异常,详情写入 /tmp/empty_image_base64.txt");
  188. }
  189. // 解码图片
  190. $imgData = base64_decode($base64Image);
  191. // 判断是否为图像
  192. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  193. $mimeType = finfo_buffer($finfo, $imgData);
  194. finfo_close($finfo);
  195. // 保存图片路径
  196. $img_name = mb_substr(preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name), 0, 30);
  197. $filename = $img_name . '.png';
  198. $saveDir1024 = $fullBaseDir . '1024x1024/';
  199. $saveDirCustom = $fullBaseDir . "{$width}x{$height}/";
  200. @mkdir($saveDir1024, 0755, true);
  201. @mkdir($saveDirCustom, 0755, true);
  202. $path1024 = $saveDir1024 . $filename;
  203. $pathCustom = $saveDirCustom . $filename;
  204. file_put_contents($path1024, $imgData);
  205. // 解析图像内容
  206. try {
  207. $im = \imagecreatefromstring($imgData);
  208. if (!$im) {
  209. file_put_contents('/tmp/corrupted.png', $imgData);
  210. throw new \Exception("❌ 图像无法解析,写入 /tmp/corrupted.png");
  211. }
  212. } catch (\Throwable $e) {
  213. file_put_contents('/tmp/corrupted.png', $imgData);
  214. throw new \Exception("❌ 图像处理异常:" . $e->getMessage());
  215. }
  216. // 裁剪
  217. $srcW = imagesx($im);
  218. $srcH = imagesy($im);
  219. $srcRatio = $srcW / $srcH;
  220. $dstRatio = $width / $height;
  221. if ($srcRatio > $dstRatio) {
  222. $cropW = intval($srcH * $dstRatio);
  223. $cropH = $srcH;
  224. $srcX = intval(($srcW - $cropW) / 2);
  225. $srcY = 0;
  226. } else {
  227. $cropW = $srcW;
  228. $cropH = intval($srcW / $dstRatio);
  229. $srcX = 0;
  230. $srcY = intval(($srcH - $cropH) / 2);
  231. }
  232. $dstImg = imagecreatetruecolor($width, $height);
  233. imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropW, $cropH);
  234. // 保存裁剪图
  235. imagepng($dstImg, $pathCustom);
  236. imagedestroy($im);
  237. imagedestroy($dstImg);
  238. // 写入数据库
  239. Db::name('text_to_image')->where('id', $record['id'])->update([
  240. 'new_image_url' => str_replace($rootPath . 'public/', '', $path1024),
  241. 'custom_image_url' => str_replace($rootPath . 'public/', '', $pathCustom),
  242. 'img_name' => $img_name,
  243. 'model' => $selectedOption,
  244. 'status' => trim($img_name) === '' ? 0 : 1,
  245. 'status_name' => "文生图",
  246. 'size' => "{$width}x{$height}",
  247. 'quality' => 'standard',
  248. 'style' => 'vivid',
  249. 'error_msg' => '',
  250. 'update_time' => date('Y-m-d H:i:s')
  251. ]);
  252. return "成功";
  253. }
  254. }