TextToImageJob.php 18 KB

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