TextToImageJob.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. public function fire(Job $job, $data)
  19. {
  20. echo "已经到了文生图阶段\n";
  21. try {
  22. $str = $this->textToImage(
  23. $data["fileName"],
  24. $data["outputDir"],
  25. $data["width"],
  26. $data["height"],
  27. $data["englishDesc"],
  28. $data["img_name"]
  29. );
  30. echo $str;
  31. echo "文生图结束\n";
  32. $job->delete();
  33. } catch (\Exception $e) {
  34. echo "Error message: " . $e->getMessage() . "\n";
  35. echo "Error file: " . $e->getFile() . "\n";
  36. echo "Error line: " . $e->getLine() . "\n";
  37. echo "Stack trace: " . $e->getTraceAsString() . "\n";
  38. // 如果有 log_id,更新任务状态为“执行失败”并记录错误信息
  39. // 最多重试一次(总执行两次)
  40. if ($job->attempts() < 2) {
  41. $job->release(30); // 延迟30秒再次执行
  42. } else {
  43. $job->failed(); // 达到最大尝试次数,标记失败
  44. }
  45. }
  46. }
  47. /**
  48. * 任务失败时的处理
  49. */
  50. public function failed($data)
  51. {
  52. // 记录失败日志或发送通知
  53. echo "ImageJob failed: " . json_encode($data);
  54. }
  55. /**
  56. * 文生图接口
  57. */
  58. public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name)
  59. {
  60. $rootPath = str_replace('\\', '/', ROOT_PATH);
  61. $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
  62. $dateDir = date('Y-m-d') . '/';
  63. $fullBaseDir = $outputDir . $dateDir;
  64. // 创建输出目录结构
  65. foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
  66. if (!is_dir($dir)) {
  67. mkdir($dir, 0755, true);
  68. }
  69. }
  70. // 清理 prompt 的换行
  71. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  72. // 查询数据库记录
  73. $record = Db::name('text_to_image')
  74. ->where('old_image_url', 'like', "%{$fileName}")
  75. ->order('id desc')
  76. ->find();
  77. if (!$record) {
  78. return '没有找到匹配的图像记录';
  79. }
  80. // 写入 prompt 日志
  81. $logDir = $rootPath . 'runtime/logs/';
  82. if (!is_dir($logDir)) mkdir($logDir, 0755, true);
  83. // echo 2345;
  84. // 调用文生图模型接口生成图像
  85. $startTime = microtime(true);
  86. $dalle1024 = $this->callDalleApi($prompt);
  87. // $dalle1024 = json_decode('{"created":1747932746,"data":[{"revised_prompt":"**First paragraph:** A geometric abstract design with a central motif consisting of curved and angular shapes in a symmetrical arrangement. The color scheme predominantly features shades of blue, with hints of white and black creating a contrast. The design incorporates smooth, flowing lines mixed with sharp angles. The overall style has a modern, minimalist aesthetic, with a focus on balance and clean shapes.","url":"https:\/\/filesystem.site\/cdn\/20250523\/3NVcCUaZDkLimWjtgOwJYniGezDX8d.png"}],"usage":{"total_tokens":4250,"input_tokens":75,"output_tokens":4175,"input_tokens_details":{"text_tokens":75}}}',true);
  88. $endTime = microtime(true);
  89. $executionTime = $endTime - $startTime;
  90. echo "API调用耗时: " . round($executionTime, 3) . " 秒\n";
  91. // 检查 URL 返回是否成功
  92. if (!isset($dalle1024['data'][0]['url']) || empty($dalle1024['data'][0]['url'])) {
  93. $errorText = $dalle1024['error']['message'] ?? '未知错误';
  94. echo '生成失败:' . $errorText;
  95. }
  96. // echo 342342;
  97. // 下载图像
  98. $imgUrl1024 = $dalle1024['data'][0]['url'];
  99. $imgData1024 = @file_get_contents($imgUrl1024);
  100. if (!$imgData1024 || strlen($imgData1024) < 1000) {
  101. return "下载图像失败或内容异常";
  102. }
  103. // echo 3423424444;
  104. // file_put_contents(
  105. // $logDir . 'img_name.txt',
  106. // "\n====图片 " . date('Y-m-d H:i:s') . " ====\n" . $img_name . "\n\n",
  107. // FILE_APPEND
  108. // );
  109. // 保存原图(1024x1024)
  110. // $img_name = $this->limitStringLength($img_name);
  111. $img_name = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name);
  112. $img_name = mb_substr($img_name, 0, 30); // 限制为前30个字符(避免路径过长)
  113. $filename1024 = $img_name . '.png';
  114. $savePath1024 = $fullBaseDir . '1024x1024/' . $filename1024;
  115. file_put_contents($savePath1024, $imgData1024);
  116. // echo 342344543532;
  117. // 处理缩略图
  118. $im = @imagecreatefromstring($imgData1024);
  119. if (!$im) return "图像格式不受支持或已损坏";
  120. $srcWidth = imagesx($im);
  121. $srcHeight = imagesy($im);
  122. $srcRatio = $srcWidth / $srcHeight;
  123. $dstRatio = $width / $height;
  124. // 居中裁剪逻辑
  125. if ($srcRatio > $dstRatio) {
  126. $cropHeight = $srcHeight;
  127. $cropWidth = intval($srcHeight * $dstRatio);
  128. $srcX = intval(($srcWidth - $cropWidth) / 2);
  129. $srcY = 0;
  130. } else {
  131. $cropWidth = $srcWidth;
  132. $cropHeight = intval($srcWidth / $dstRatio);
  133. $srcX = 0;
  134. $srcY = intval(($srcHeight - $cropHeight) / 2);
  135. }
  136. // echo 789;
  137. $dstImg = imagecreatetruecolor($width, $height);
  138. imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight);
  139. // 保存裁剪后图像
  140. $filenameCustom = $img_name . ".png";
  141. $savePathCustom = $fullBaseDir . "{$width}x{$height}/" . $filenameCustom;
  142. imagepng($dstImg, $savePathCustom);
  143. imagedestroy($im);
  144. imagedestroy($dstImg);
  145. // file_put_contents(
  146. // $logDir . 'image_url.txt',
  147. // "\n====图片路径 " . date('Y-m-d H:i:s') . " ====\n" . str_replace($rootPath . 'public/', '', $savePath1024) . "\n\n",
  148. // "\n====图片路径 " . date('Y-m-d H:i:s') . " ====\n" . str_replace($rootPath . 'public/', '', $savePathCustom) . "\n\n",
  149. // FILE_APPEND
  150. // );
  151. $status = trim($img_name) === '' ? 0 : 1;
  152. // 更新数据库记录
  153. $updateRes = Db::name('text_to_image')->where('id', $record['id'])->update([
  154. 'new_image_url' => str_replace($rootPath . 'public/', '', $savePath1024),
  155. 'custom_image_url' => str_replace($rootPath . 'public/', '', $savePathCustom),
  156. 'img_name' => $img_name,
  157. 'error_msg' => '',
  158. 'size' => "{$width}x{$height}",
  159. 'updated_time' => date('Y-m-d H:i:s'),
  160. 'status' => $status
  161. ]);
  162. return 0;
  163. }
  164. /**
  165. * 处理字符串长度,超出限制则截断
  166. *
  167. * @param string $str 输入字符串
  168. * @param int $maxLength 最大长度限制(默认200)
  169. * @return string 处理后的字符串
  170. */
  171. public function limitStringLength($str, $maxLength = 10)
  172. {
  173. // 如果字符串长度没有超出限制,直接返回
  174. if (mb_strlen($str, 'UTF-8') <= $maxLength) {
  175. return $str;
  176. }
  177. // 超出限制则截断
  178. return mb_substr($str, 0, $maxLength, 'UTF-8');
  179. }
  180. public function cleanImageUrl($input) {
  181. // 去除字符串首尾空格和中文引号替换为英文引号
  182. $input = trim($input);
  183. $input = str_replace(['“', '”', '‘', '’'], '"', $input);
  184. // 判断是否为纯中文文字
  185. if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $input)) {
  186. // 纯中文:替换掉不适合用于文件名的字符
  187. $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input);
  188. } elseif (preg_match('/[a-zA-Z]/', $input) && !preg_match('/[\x{4e00}-\x{9fa5}]/u', $input)) {
  189. // 如果是纯字母和空格,且没有中文字符:保留空格,去掉其他符号
  190. $cleaned = preg_replace('/[^a-zA-Z\s]/', '', $input);
  191. } else {
  192. // 如果包含中文或是其他混合字符,按照纯中文的规则清理符号
  193. $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input);
  194. }
  195. return $cleaned;
  196. }
  197. /**
  198. * 文生图模型
  199. */
  200. public function callDalleApi($prompt)
  201. {
  202. $data = [
  203. 'prompt' => $prompt,
  204. // 'model' => 'dall-e-2',
  205. 'model' => 'gpt-image-1',
  206. 'n' => 1,
  207. 'size' => '1024x1024',
  208. 'quality' => 'standard',
  209. 'style' => 'vivid',
  210. 'response_format' => 'url'
  211. ];
  212. return $this->callApi($this->config['dalle']['api_url'], $this->config['dalle']['api_key'], $data);
  213. }
  214. /**
  215. * 通用API调用方法
  216. */
  217. public function callApi($url, $apiKey, $data)
  218. {
  219. $maxRetries = 2;
  220. $attempt = 0;
  221. $lastError = '';
  222. while ($attempt <= $maxRetries) {
  223. $ch = curl_init();
  224. curl_setopt_array($ch, [
  225. CURLOPT_URL => $url,
  226. CURLOPT_RETURNTRANSFER => true,
  227. CURLOPT_POST => true,
  228. CURLOPT_POSTFIELDS => json_encode($data),
  229. CURLOPT_HTTPHEADER => [
  230. 'Content-Type: application/json',
  231. 'Authorization: Bearer ' . $apiKey
  232. ],
  233. CURLOPT_TIMEOUT => 120,
  234. CURLOPT_SSL_VERIFYPEER => false,
  235. CURLOPT_SSL_VERIFYHOST => 0,
  236. CURLOPT_TCP_KEEPALIVE => 1,
  237. CURLOPT_FORBID_REUSE => false
  238. ]);
  239. $response = curl_exec($ch);
  240. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  241. $curlError = curl_error($ch);
  242. curl_close($ch);
  243. if ($response !== false && $httpCode === 200) {
  244. $result = json_decode($response, true);
  245. return $result;
  246. }
  247. $lastError = $curlError ?: "HTTP错误:{$httpCode}";
  248. $attempt++;
  249. sleep(1);
  250. }
  251. throw new \Exception("请求失败(重试{$maxRetries}次):{$lastError}");
  252. }
  253. }