TextToImageJob.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace app\job;
  3. use think\Db;
  4. use think\Queue;
  5. use think\queue\Job;
  6. class TextToImageJob
  7. {
  8. protected $config = [
  9. 'gpt' => [
  10. 'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73',
  11. 'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
  12. ],
  13. 'dalle' => [
  14. 'api_key' => 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY',
  15. 'api_url' => 'https://niubi.zeabur.app/v1/images/generations'
  16. ]
  17. ];
  18. /**
  19. * 文生图队列任务
  20. */
  21. public function fire(Job $job, $data)
  22. {
  23. $logId = $data['log_id'] ?? null;
  24. try {
  25. if (!isset($data['type']) || $data['type'] !== '文生图') {
  26. $job->delete();
  27. return;
  28. }
  29. $startTime = date('Y-m-d H:i:s');
  30. echo "━━━━━━━━━━ ▶ 文生图任务开始处理━━━━━━━━━━\n";
  31. echo "处理时间:{$startTime}\n";
  32. // 更新日志状态为处理中
  33. if ($logId) {
  34. Db::name('image_task_log')->where('id', $logId)->update([
  35. 'status' => 1,
  36. 'log' => '文生图处理中',
  37. 'update_time' => $startTime
  38. ]);
  39. }
  40. $fullPath = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/');
  41. $list = Db::name("text_to_image")
  42. ->where('old_image_url', $fullPath)
  43. ->where('img_name', '<>', '')
  44. ->where('status', 0)
  45. ->select();
  46. if (!empty($list)) {
  47. $total = count($list);
  48. echo "📊 共需处理:{$total} 条记录\n\n";
  49. foreach ($list as $index => $row) {
  50. $currentIndex = $index + 1;
  51. $begin = date('Y-m-d H:i:s');
  52. echo "处理时间:{$begin}\n";
  53. echo "👉 正在处理第 {$currentIndex} 条,ID: {$row['id']}\n";
  54. // 调用生成图像方法
  55. $result = $this->textToImage(
  56. $data["file_name"],
  57. $data["outputDir"],
  58. $data["width"],
  59. $data["height"],
  60. $row["english_description"],
  61. $row["img_name"],
  62. $data["selectedOption"] ?? null
  63. );
  64. $resultText = ($result === true || $result === 1 || $result === '成功') ? '成功' : '失败或无返回';
  65. echo "✅ 处理结果:{$resultText}\n";
  66. $end = date('Y-m-d H:i:s');
  67. echo "完成时间:{$end}\n";
  68. echo "Processed: " . static::class . "\n";
  69. echo "文生图已处理完成\n\n";
  70. }
  71. // 更新日志为成功
  72. if ($logId) {
  73. Db::name('image_task_log')->where('id', $logId)->update([
  74. 'status' => 2,
  75. 'log' => '文生图执行成功',
  76. 'update_time' => date('Y-m-d H:i:s')
  77. ]);
  78. }
  79. echo date('Y-m-d H:i:s') . " ✅ 文生图任务全部完成\n";
  80. } else {
  81. echo "⚠ 未找到可处理的数据,跳过执行\n";
  82. if ($logId) {
  83. Db::name('image_task_log')->where('id', $logId)->update([
  84. 'status' => 2,
  85. 'log' => '无数据可处理,已跳过',
  86. 'update_time' => date('Y-m-d H:i:s')
  87. ]);
  88. }
  89. }
  90. $job->delete();
  91. } catch (\Exception $e) {
  92. echo "❌ 异常信息: " . $e->getMessage() . "\n";
  93. echo "📄 文件: " . $e->getFile() . "\n";
  94. echo "📍 行号: " . $e->getLine() . "\n";
  95. if ($logId) {
  96. Db::name('image_task_log')->where('id', $logId)->update([
  97. 'status' => -1,
  98. 'log' => '文生图失败:' . $e->getMessage(),
  99. 'update_time' => date('Y-m-d H:i:s')
  100. ]);
  101. }
  102. $job->delete();
  103. }
  104. }
  105. /**
  106. * 任务失败时的处理
  107. */
  108. public function failed($data)
  109. {
  110. // 记录失败日志或发送通知
  111. echo "ImageJob failed: " . json_encode($data);
  112. }
  113. /**
  114. * 文生图接口
  115. */
  116. public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name,$selectedOption)
  117. {
  118. $rootPath = str_replace('\\', '/', ROOT_PATH);
  119. $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
  120. $dateDir = date('Y-m-d') . '/';
  121. $fullBaseDir = $outputDir . $dateDir;
  122. // 创建输出目录结构
  123. foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
  124. if (!is_dir($dir)) {
  125. mkdir($dir, 0755, true);
  126. }
  127. }
  128. // 查询数据库记录
  129. $record = Db::name('text_to_image')
  130. ->where('old_image_url', 'like', "%{$fileName}")
  131. ->order('id desc')
  132. ->find();
  133. if (!$record) {
  134. return '没有找到匹配的图像记录';
  135. }
  136. // 写入 prompt 日志
  137. $logDir = $rootPath . 'runtime/logs/';
  138. if (!is_dir($logDir)) mkdir($logDir, 0755, true);
  139. // 调用文生图模型接口生成图像
  140. $startTime = microtime(true);
  141. // 清理 prompt 的换行
  142. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  143. // 定义要跳过的关键词(可按需扩展)
  144. $skipKeywords = ['几何', 'geometry', 'geometric'];
  145. foreach ($skipKeywords as $keyword) {
  146. // 判断提示词中是否包含关键词(不区分大小写)
  147. if (stripos($prompt, $keyword) !== false) {
  148. $skipId = $record['id'];
  149. echo "🚫 跳过生成:提示词中包含关键词“{$keyword}”,记录 ID:{$skipId}\n";
  150. $updateRes = Db::name('text_to_image')->where('id', $skipId)->update([
  151. 'status' => 3,
  152. 'update_time' => date('Y-m-d H:i:s')
  153. ]);
  154. return "跳过生成:记录 ID {$skipId},包含关键词 - {$keyword}";
  155. }
  156. }
  157. //文生图调用
  158. $dalle1024 = $this->callDalleApi($prompt,$selectedOption);
  159. //查询接口调用时长
  160. $endTime = microtime(true);
  161. $executionTime = $endTime - $startTime;
  162. echo "API调用耗时: " . round($executionTime, 3) . " 秒\n";
  163. // 检查 URL 返回是否成功
  164. if (!isset($dalle1024['data'][0]['url']) || empty($dalle1024['data'][0]['url'])) {
  165. $errorText = $dalle1024['error']['message'] ?? '未知错误';
  166. Db::name('text_to_image')->where('id', $record['id'])->update([
  167. 'error_msg' => '生成失败:' . $errorText,
  168. 'status' => 0
  169. ]);
  170. return '生成失败:' . $errorText;
  171. }
  172. // 下载图像
  173. $imgUrl1024 = $dalle1024['data'][0]['url'];
  174. $imgData1024 = @file_get_contents($imgUrl1024);
  175. if (!$imgData1024 || strlen($imgData1024) < 1000) {
  176. return "下载图像失败或内容异常";
  177. }
  178. // 保存原图(1024x1024)
  179. $img_name = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name);
  180. $img_name = mb_substr($img_name, 0, 30); // 限制为前30个字符(避免路径过长)
  181. $filename1024 = $img_name . '.png';
  182. $savePath1024 = $fullBaseDir . '1024x1024/' . $filename1024;
  183. file_put_contents($savePath1024, $imgData1024);
  184. // 处理缩略图
  185. $im = @imagecreatefromstring($imgData1024);
  186. if (!$im) return "图像格式不受支持或已损坏";
  187. $srcWidth = imagesx($im);
  188. $srcHeight = imagesy($im);
  189. $srcRatio = $srcWidth / $srcHeight;
  190. $dstRatio = $width / $height;
  191. // 居中裁剪逻辑
  192. if ($srcRatio > $dstRatio) {
  193. $cropHeight = $srcHeight;
  194. $cropWidth = intval($srcHeight * $dstRatio);
  195. $srcX = intval(($srcWidth - $cropWidth) / 2);
  196. $srcY = 0;
  197. } else {
  198. $cropWidth = $srcWidth;
  199. $cropHeight = intval($srcWidth / $dstRatio);
  200. $srcX = 0;
  201. $srcY = intval(($srcHeight - $cropHeight) / 2);
  202. }
  203. $dstImg = imagecreatetruecolor($width, $height);
  204. imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight);
  205. // 保存裁剪后图像
  206. $filenameCustom = $img_name . ".png";
  207. $savePathCustom = $fullBaseDir . "{$width}x{$height}/" . $filenameCustom;
  208. imagepng($dstImg, $savePathCustom);
  209. imagedestroy($im);
  210. imagedestroy($dstImg);
  211. $status = trim($img_name) === '' ? 0 : 1;
  212. // 更新数据库记录
  213. $updateRes = Db::name('text_to_image')->where('id', $record['id'])->update([
  214. 'new_image_url' => str_replace($rootPath . 'public/', '', $savePath1024),
  215. 'custom_image_url' => str_replace($rootPath . 'public/', '', $savePathCustom),
  216. 'img_name' => $img_name,
  217. 'error_msg' => '',
  218. 'model' => $selectedOption,
  219. 'quality' => 'hd',
  220. 'style' => 'vivid',
  221. 'size' => "{$width}x{$height}",
  222. 'updated_time' => date('Y-m-d H:i:s'),
  223. 'status' => $status
  224. ]);
  225. return 0;
  226. }
  227. /**
  228. * 处理字符串长度,超出限制则截断
  229. *
  230. * @param string $str 输入字符串
  231. * @param int $maxLength 最大长度限制(默认200)
  232. * @return string 处理后的字符串
  233. */
  234. public function limitStringLength($str, $maxLength = 10)
  235. {
  236. // 如果字符串长度没有超出限制,直接返回
  237. if (mb_strlen($str, 'UTF-8') <= $maxLength) {
  238. return $str;
  239. }
  240. // 超出限制则截断
  241. return mb_substr($str, 0, $maxLength, 'UTF-8');
  242. }
  243. public function cleanImageUrl($input) {
  244. // 去除字符串首尾空格和中文引号替换为英文引号
  245. $input = trim($input);
  246. $input = str_replace(['“', '”', '‘', '’'], '"', $input);
  247. // 判断是否为纯中文文字
  248. if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $input)) {
  249. // 纯中文:替换掉不适合用于文件名的字符
  250. $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input);
  251. } elseif (preg_match('/[a-zA-Z]/', $input) && !preg_match('/[\x{4e00}-\x{9fa5}]/u', $input)) {
  252. // 如果是纯字母和空格,且没有中文字符:保留空格,去掉其他符号
  253. $cleaned = preg_replace('/[^a-zA-Z\s]/', '', $input);
  254. } else {
  255. // 如果包含中文或是其他混合字符,按照纯中文的规则清理符号
  256. $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input);
  257. }
  258. return $cleaned;
  259. }
  260. /**
  261. * 文生图模
  262. */
  263. public function callDalleApi($prompt,$selectedOption)
  264. {
  265. if($selectedOption == 'dall-e-3'){
  266. $data = [
  267. 'prompt' => $prompt,
  268. 'model' => $selectedOption,
  269. 'n' => 1,
  270. 'size' => '1024x1024',
  271. 'quality' => 'standard',
  272. 'style' => 'vivid',
  273. 'response_format' => 'url',
  274. 'session_id' => null,
  275. 'context_reset' => true
  276. ];
  277. }else{
  278. $data = [
  279. 'prompt' => $prompt,
  280. 'model' => $selectedOption,
  281. 'n' => 1,
  282. 'size' => '1024x1024',
  283. 'quality' => 'hd',
  284. 'style' => 'vivid',
  285. 'response_format' => 'url',
  286. 'session_id' => null,
  287. 'context_reset' => true
  288. ];
  289. }
  290. return $this->callApi($this->config['dalle']['api_url'], $this->config['dalle']['api_key'], $data);
  291. }
  292. /**
  293. * 通用API调用方法
  294. */
  295. public function callApi($url, $apiKey, $data)
  296. {
  297. $maxRetries = 2;
  298. $attempt = 0;
  299. $lastError = '';
  300. while ($attempt <= $maxRetries) {
  301. $ch = curl_init();
  302. curl_setopt_array($ch, [
  303. CURLOPT_URL => $url,
  304. CURLOPT_RETURNTRANSFER => true,
  305. CURLOPT_POST => true,
  306. CURLOPT_POSTFIELDS => json_encode($data),
  307. CURLOPT_HTTPHEADER => [
  308. 'Content-Type: application/json',
  309. 'Authorization: Bearer ' . $apiKey
  310. ],
  311. CURLOPT_TIMEOUT => 120,
  312. CURLOPT_SSL_VERIFYPEER => false,
  313. CURLOPT_SSL_VERIFYHOST => 0,
  314. CURLOPT_TCP_KEEPALIVE => 1,
  315. CURLOPT_FORBID_REUSE => false
  316. ]);
  317. $response = curl_exec($ch);
  318. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  319. $curlError = curl_error($ch);
  320. curl_close($ch);
  321. if ($response !== false && $httpCode === 200) {
  322. $result = json_decode($response, true);
  323. return $result;
  324. }
  325. $lastError = $curlError ?: "HTTP错误:{$httpCode}";
  326. $attempt++;
  327. sleep(1);
  328. }
  329. throw new \Exception("请求失败(重试{$maxRetries}次):{$lastError}");
  330. }
  331. }