TextToImageJob.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. class TextToImageJob
  11. {
  12. public function fire(Job $job, $data)
  13. {
  14. $logId = $data['log_id'] ?? null;
  15. try {
  16. if (!isset($data['type']) || $data['type'] !== '文生图') {
  17. $job->delete();
  18. return;
  19. }
  20. $startTime = date('Y-m-d H:i:s');
  21. echo "━━━━━━━━━━ ▶ 文生图任务开始处理━━━━━━━━━━\n";
  22. echo "处理时间:{$startTime}\n";
  23. // 更新日志状态为处理中
  24. if ($logId) {
  25. Db::name('image_task_log')->where('id', $logId)->update([
  26. 'status' => 1,
  27. 'log' => '文生图处理中',
  28. 'update_time' => $startTime
  29. ]);
  30. }
  31. //文件路径 + 图片名称
  32. $old_image_url = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/');
  33. $list = Db::name("text_to_image")
  34. ->where('old_image_url', $old_image_url)
  35. ->where('img_name', '<>', '')
  36. ->where('status', 0)
  37. ->select();
  38. if (!empty($list)) {
  39. $total = count($list);
  40. echo "📊 共需处理:{$total} 条记录\n\n";
  41. foreach ($list as $index => $row) {
  42. $currentIndex = $index + 1;
  43. $begin = date('Y-m-d H:i:s');
  44. echo "处理时间:{$begin}\n";
  45. echo "👉 正在处理第 {$currentIndex} 条,ID: {$row['id']}\n";
  46. // 调用生成图像方法
  47. $result = $this->textToImage(
  48. $data["file_name"],
  49. $data["outputDir"],
  50. $data["width"],
  51. $data["height"],
  52. $row["english_description"],
  53. $row["img_name"],
  54. $data["selectedOption"] ?? null
  55. );
  56. $resultText = ($result === true || $result === 1 || $result === '成功') ? '成功' : '失败或无返回';
  57. echo "✅ 处理结果:{$resultText}\n";
  58. $end = date('Y-m-d H:i:s');
  59. echo "完成时间:{$end}\n";
  60. echo "Processed: " . static::class . "\n";
  61. echo "文生图已处理完成\n\n";
  62. }
  63. // 更新日志为成功
  64. if ($logId) {
  65. Db::name('image_task_log')->where('id', $logId)->update([
  66. 'status' => 2,
  67. 'log' => '文生图处理成功',
  68. 'update_time' => date('Y-m-d H:i:s')
  69. ]);
  70. }
  71. echo date('Y-m-d H:i:s') . " ✅ 文生图任务全部完成\n";
  72. } else {
  73. echo "⚠ 未找到可处理的数据,跳过执行\n";
  74. if ($logId) {
  75. Db::name('image_task_log')->where('id', $logId)->update([
  76. 'status' => 2,
  77. 'log' => '无数据可处理,已跳过',
  78. 'update_time' => date('Y-m-d H:i:s')
  79. ]);
  80. }
  81. }
  82. // 如果还有链式任务,继续推送
  83. if (!empty($data['chain_next'])) {
  84. $nextType = array_shift($data['chain_next']);
  85. $data['type'] = $nextType;
  86. Queue::push('app\job\ImageArrJob', [
  87. 'task_id' => $data['task_id'],
  88. 'data' => [$data]
  89. ], 'arrimage');
  90. }
  91. $job->delete();
  92. } catch (\Exception $e) {
  93. echo "❌ 异常信息: " . $e->getMessage() . "\n";
  94. echo "📄 文件: " . $e->getFile() . "\n";
  95. echo "📍 行号: " . $e->getLine() . "\n";
  96. if ($logId) {
  97. Db::name('image_task_log')->where('id', $logId)->update([
  98. 'status' => -1,
  99. 'log' => '文生图失败:' . $e->getMessage(),
  100. 'update_time' => date('Y-m-d H:i:s')
  101. ]);
  102. }
  103. $job->delete();
  104. }
  105. }
  106. /**
  107. * 任务失败时的处理
  108. */
  109. public function failed($data)
  110. {
  111. // 记录失败日志或发送通知
  112. echo "ImageJob failed: " . json_encode($data);
  113. }
  114. /**
  115. * 文生图接口
  116. */
  117. public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name,$selectedOption)
  118. {
  119. $rootPath = str_replace('\\', '/', ROOT_PATH);
  120. $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
  121. $dateDir = date('Y-m-d') . '/';
  122. $fullBaseDir = $outputDir . $dateDir;
  123. // 创建输出目录结构
  124. foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
  125. if (!is_dir($dir)) {
  126. mkdir($dir, 0755, true);
  127. }
  128. }
  129. // 查询数据库记录
  130. $record = Db::name('text_to_image')
  131. ->where('old_image_url', 'like', "%{$fileName}")
  132. ->order('id desc')
  133. ->find();
  134. if (!$record) {
  135. return '没有找到匹配的图像记录';
  136. }
  137. // 写入 prompt 日志
  138. $logDir = $rootPath . 'runtime/logs/';
  139. if (!is_dir($logDir)) mkdir($logDir, 0755, true);
  140. // 调用文生图模型接口生成图像
  141. $startTime = microtime(true);
  142. // 清理 prompt 的换行
  143. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  144. // 定义要跳过的关键词(可按需扩展)
  145. $skipKeywords = ['几何', 'geometry', 'geometric'];
  146. foreach ($skipKeywords as $keyword) {
  147. // 判断提示词中是否包含关键词(不区分大小写)
  148. if (stripos($prompt, $keyword) !== false) {
  149. $skipId = $record['id'];
  150. echo "🚫 跳过生成:提示词中包含关键词“{$keyword}”,记录 ID:{$skipId}\n";
  151. $updateRes = Db::name('text_to_image')->where('id', $skipId)->update([
  152. 'status' => 3,
  153. 'error_msg' => "提示词中包含关键词".$keyword,
  154. 'update_time' => date('Y-m-d H:i:s')
  155. ]);
  156. return "跳过生成:记录 ID {$skipId},包含关键词 - {$keyword}";
  157. }
  158. }
  159. //文生图调用
  160. // $dalle1024 = $this->callDalleApi($prompt,$selectedOption);
  161. $ai = new AIGatewayService();
  162. $dalle1024 = $ai->callDalleApi($prompt,$selectedOption);
  163. //检测查询接口调用时长
  164. $endTime = microtime(true);
  165. $executionTime = $endTime - $startTime;
  166. echo "API调用耗时: " . round($executionTime, 3) . " 秒\n";
  167. // 检查 URL 返回是否成功
  168. if (!isset($dalle1024['data'][0]['url']) || empty($dalle1024['data'][0]['url'])) {
  169. $errorText = $dalle1024['error']['message'] ?? '未知错误';
  170. Db::name('text_to_image')->where('id', $record['id'])->update([
  171. 'error_msg' => '生成失败:' . $errorText,
  172. 'status' => 0
  173. ]);
  174. return '生成失败:' . $errorText;
  175. }
  176. // 下载图像
  177. $imgUrl1024 = $dalle1024['data'][0]['url'];
  178. $imgData1024 = @file_get_contents($imgUrl1024);
  179. if (!$imgData1024 || strlen($imgData1024) < 1000) {
  180. return "下载图像失败或内容异常";
  181. }
  182. // 保存原图(1024x1024)
  183. $img_name = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name);
  184. $img_name = mb_substr($img_name, 0, 30); // 限制为前30个字符(避免路径过长)
  185. $filename1024 = $img_name . '.png';
  186. $savePath1024 = $fullBaseDir . '1024x1024/' . $filename1024;
  187. file_put_contents($savePath1024, $imgData1024);
  188. // 处理缩略图
  189. $im = @imagecreatefromstring($imgData1024);
  190. if (!$im) return "图像格式不受支持或已损坏";
  191. $srcWidth = imagesx($im);
  192. $srcHeight = imagesy($im);
  193. $srcRatio = $srcWidth / $srcHeight;
  194. $dstRatio = $width / $height;
  195. // 居中裁剪逻辑
  196. if ($srcRatio > $dstRatio) {
  197. $cropHeight = $srcHeight;
  198. $cropWidth = intval($srcHeight * $dstRatio);
  199. $srcX = intval(($srcWidth - $cropWidth) / 2);
  200. $srcY = 0;
  201. } else {
  202. $cropWidth = $srcWidth;
  203. $cropHeight = intval($srcWidth / $dstRatio);
  204. $srcX = 0;
  205. $srcY = intval(($srcHeight - $cropHeight) / 2);
  206. }
  207. $dstImg = imagecreatetruecolor($width, $height);
  208. imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight);
  209. // 保存裁剪后图像
  210. $filenameCustom = $img_name . ".png";
  211. $savePathCustom = $fullBaseDir . "{$width}x{$height}/" . $filenameCustom;
  212. imagepng($dstImg, $savePathCustom);
  213. imagedestroy($im);
  214. imagedestroy($dstImg);
  215. $status = trim($img_name) === '' ? 0 : 1;
  216. // 更新数据库记录
  217. $updateRes = Db::name('text_to_image')->where('id', $record['id'])->update([
  218. 'new_image_url' => str_replace($rootPath . 'public/', '', $savePath1024),
  219. 'custom_image_url' => str_replace($rootPath . 'public/', '', $savePathCustom),
  220. 'img_name' => $img_name,
  221. 'error_msg' => '',
  222. 'model' => $selectedOption,
  223. 'quality' => 'hd',
  224. 'style' => 'vivid',
  225. 'size' => "{$width}x{$height}",
  226. 'update_time' => date('Y-m-d H:i:s'),
  227. 'status' => $status,
  228. 'status_name' => "文生图"
  229. ]);
  230. return 0;
  231. }
  232. }