TextToImageJob.php 14 KB

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