TextToImageJob.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. // $text_content = $res['candidates'][0]['content']['parts'][0]['inlineData']['data'];
  202. // // 匹配base64图片数据
  203. // preg_match('/data:image\/(png|jpg|jpeg);base64,([^"]+)/', $text_content, $matches);
  204. // 提取base64图片数据
  205. $text_content = $res['candidates'][0]['content']['parts'][0]['inlineData']['data'];
  206. $str = 'data:image/jpeg;base64,';
  207. $text_content = $str. $text_content;
  208. // 匹配base64图片数据
  209. preg_match('/data:image\/(png|jpg|jpeg);base64,([^"]+)/', $text_content, $matches);
  210. if (empty($matches)) {
  211. return '未找到图片数据';
  212. }
  213. $image_type = $matches[1];
  214. $base64_data = $matches[2];
  215. // 解码base64数据
  216. $image_data = base64_decode($base64_data);
  217. if ($image_data === false) {
  218. return '图片解码失败';
  219. }
  220. // 生成唯一文件名(包含扩展名)
  221. $file_name = uniqid() . '.' . $image_type;
  222. $full_file_path = $save_path . $file_name;
  223. // 保存图片到文件系统
  224. if (!file_put_contents($full_file_path, $image_data)) {
  225. return '图片保存失败';
  226. }
  227. // 生成数据库存储路径(使用正斜杠格式)
  228. $db_img_path = $url_path . $file_name;
  229. Db::name('product')->where('id', $data['id'])->update(['product_new_img' => $db_img_path]);
  230. return '成功';
  231. // } else {
  232. // return 'AI返回格式错误';
  233. // }
  234. }
  235. /**
  236. * 文生图处理函数
  237. * 描述:根据提示词调用图像生成接口,保存图像文件,并更新数据库
  238. */
  239. public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name, $selectedOption,$executeKeywords,$sourceDir)
  240. {
  241. $rootPath = str_replace('\\', '/', ROOT_PATH);
  242. $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
  243. $dateDir = date('Y-m-d') . '/';
  244. $fullBaseDir = $outputDir . $dateDir;
  245. // 创建输出目录
  246. foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
  247. if (!is_dir($dir)) mkdir($dir, 0755, true);
  248. }
  249. // 确保目录存在
  250. if (!is_dir($fullBaseDir . '2048x2048/')) {
  251. mkdir($fullBaseDir . '2048x2048/', 0755, true);
  252. }
  253. // 获取图像记录
  254. $record = Db::name('text_to_image')
  255. ->where('old_image_url', 'like', "%{$fileName}")
  256. ->order('id desc')
  257. ->find();
  258. Db::name('text_to_image')->where('id', $record['id'])->update([
  259. 'new_image_url' => '',
  260. ]);
  261. if (!$record) return '没有找到匹配的图像记录';
  262. //判断是否执行几何图
  263. if($executeKeywords == false){
  264. // 过滤关键词
  265. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  266. foreach (['几何', 'geometry', 'geometric'] as $keyword) {
  267. if (stripos($prompt, $keyword) !== false) {
  268. Db::name('text_to_image')->where('id', $record['id'])->update([
  269. 'status' => 3,
  270. 'error_msg' => "包含关键词".$keyword,
  271. 'update_time' => date('Y-m-d H:i:s')
  272. ]);
  273. return "包含关键词 - {$keyword}";
  274. }
  275. }
  276. }
  277. $template = Db::name('template')
  278. ->field('id,english_content,content')
  279. ->where('path',$sourceDir)
  280. ->where('ids',1)
  281. ->find();
  282. // AI 图像生成调用
  283. $ai = new AIGatewayService();
  284. $response = $ai->callDalleApi($template['content'].$prompt, $selectedOption);
  285. if (isset($response['error'])) {
  286. throw new \Exception("❌ 图像生成失败:" . $response['error']['message']);
  287. }
  288. // 支持 URL 格式(为主)和 base64
  289. $imgData = null;
  290. if (isset($res['candidates'][0]['content']['parts'][0]['text'])) {
  291. $text_content = $res['candidates'][0]['content']['parts'][0]['text'];
  292. // 匹配base64图片数据
  293. preg_match('/data:image\/(png|jpg|jpeg);base64,([^"]+)/', $text_content, $matches);
  294. if (empty($matches)) {
  295. return '未找到图片数据';
  296. }
  297. $image_type = $matches[1];
  298. $base64_data = $matches[2];
  299. // 解码base64数据
  300. $imgData = base64_decode($base64_data);
  301. }else if (isset($response['data'][0]['url'])) {
  302. $imgData = @file_get_contents($response['data'][0]['url']);
  303. } elseif (isset($response['data'][0]['b64_json'])) {
  304. $imgData = base64_decode($response['data'][0]['b64_json']);
  305. }
  306. if (!$imgData || strlen($imgData) < 1000) {
  307. throw new \Exception("❌ 图像内容为空或异常!");
  308. }
  309. // 保存文件路径定义
  310. $img_name = mb_substr(preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name), 0, 30);
  311. $filename = $img_name . '.png';
  312. $path512 = $fullBaseDir . '1024x1024/' . $filename;
  313. $pathCustom = $fullBaseDir . "{$width}x{$height}/" . $filename;
  314. // 保存原图
  315. file_put_contents($path512, $imgData);
  316. // 数据库更新
  317. Db::name('text_to_image')->where('id', $record['id'])->update([
  318. 'new_image_url' => str_replace($rootPath . 'public/', '', $path512),
  319. // 注释以下一行则不保存裁剪路径(适配你的配置)
  320. // 'custom_image_url' => str_replace($rootPath . 'public/', '', $pathCustom),
  321. 'img_name' => $img_name,
  322. 'model' => $selectedOption,
  323. 'status' => trim($img_name) === '' ? 0 : 1,
  324. 'status_name' => "文生图",
  325. 'size' => "{$width}x{$height}",
  326. 'quality' => 'standard',
  327. 'style' => 'vivid',
  328. 'error_msg' => '',
  329. 'update_time' => date('Y-m-d H:i:s')
  330. ]);
  331. return "成功";
  332. }
  333. public function getImageSeed($taskId)
  334. {
  335. // 配置参数
  336. $apiUrl = 'https://chatapi.onechats.ai/mj/task/' . $taskId . '/fetch';
  337. $apiKey = 'sk-iURfrAgzAjhZ4PpPLwzmWIAhM7zKfrkwDvyxk4RVBQ4ouJNK';
  338. try {
  339. // 初始化cURL
  340. $ch = curl_init();
  341. // 设置cURL选项
  342. curl_setopt_array($ch, [
  343. CURLOPT_URL => $apiUrl,
  344. CURLOPT_RETURNTRANSFER => true,
  345. CURLOPT_CUSTOMREQUEST => 'GET', // 明确指定GET方法
  346. CURLOPT_HTTPHEADER => [
  347. 'Authorization: Bearer ' . $apiKey,
  348. 'Accept: application/json',
  349. 'Content-Type: application/json'
  350. ],
  351. CURLOPT_SSL_VERIFYPEER => false,
  352. CURLOPT_SSL_VERIFYHOST => false,
  353. CURLOPT_TIMEOUT => 60,
  354. CURLOPT_FAILONERROR => true // 添加失败时返回错误
  355. ]);
  356. // 执行请求
  357. $response = curl_exec($ch);
  358. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  359. // 错误处理
  360. if (curl_errno($ch)) {
  361. throw new Exception('cURL请求失败: ' . curl_error($ch));
  362. }
  363. // 关闭连接
  364. curl_close($ch);
  365. // 验证HTTP状态码
  366. if ($httpCode < 200 || $httpCode >= 300) {
  367. throw new Exception('API返回错误状态码: ' . $httpCode);
  368. }
  369. // 解析JSON响应
  370. $responseData = json_decode($response, true);
  371. if (json_last_error() !== JSON_ERROR_NONE) {
  372. throw new Exception('JSON解析失败: ' . json_last_error_msg());
  373. }
  374. // 返回结构化数据
  375. return [
  376. 'success' => true,
  377. 'http_code' => $httpCode,
  378. 'data' => $responseData
  379. ];
  380. } catch (Exception $e) {
  381. // 确保关闭cURL连接
  382. if (isset($ch) && is_resource($ch)) {
  383. curl_close($ch);
  384. }
  385. return [
  386. 'success' => false,
  387. 'error' => $e->getMessage(),
  388. 'http_code' => $httpCode ?? 0
  389. ];
  390. }
  391. }
  392. /**
  393. * 发送API请求
  394. * @param string $url 请求地址
  395. * @param array $data 请求数据
  396. * @param string $apiKey API密钥
  397. * @param string $method 请求方法
  398. * @return string 响应内容
  399. * @throws Exception
  400. */
  401. private function sendApiRequest($url, $data, $apiKey, $method = 'POST')
  402. {
  403. $ch = curl_init();
  404. curl_setopt_array($ch, [
  405. CURLOPT_URL => $url,
  406. CURLOPT_RETURNTRANSFER => true,
  407. CURLOPT_CUSTOMREQUEST => $method,
  408. CURLOPT_HTTPHEADER => [
  409. 'Authorization: Bearer '.$apiKey,
  410. 'Accept: application/json',
  411. 'Content-Type: application/json'
  412. ],
  413. CURLOPT_POSTFIELDS => $method === 'POST' ? json_encode($data) : null,
  414. CURLOPT_SSL_VERIFYPEER => false,
  415. CURLOPT_SSL_VERIFYHOST => false,
  416. CURLOPT_TIMEOUT => 60,
  417. CURLOPT_FAILONERROR => true
  418. ]);
  419. $response = curl_exec($ch);
  420. $error = curl_error($ch);
  421. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  422. curl_close($ch);
  423. if ($error) {
  424. throw new Exception('API请求失败: '.$error);
  425. }
  426. if ($httpCode < 200 || $httpCode >= 300) {
  427. throw new Exception('API返回错误状态码: '.$httpCode);
  428. }
  429. return $response;
  430. }
  431. }