ImageJob.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. // 1. 正确的队列任务类 application/job/ImageJob.php
  3. namespace app\job;
  4. use think\Db;
  5. use think\queue\Job;
  6. class ImageJob
  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. * fire方法是队列默认调用的方法
  20. * @param Job $job 当前的任务对象
  21. * @param array|mixed $data 发布任务时自定义的数据
  22. */
  23. public function fire(Job $job, $data)
  24. {
  25. echo "队列任务开始执行\n";
  26. echo "接收的数据: " . json_encode($data) . "\n";
  27. $logId = $data['log_id'] ?? null;
  28. try {
  29. if ($logId) {
  30. Db::name('queue_log')->where('id', $logId)->update([
  31. 'status' => 1, // 正在处理
  32. 'updated_at' => date('Y-m-d H:i:s')
  33. ]);
  34. }
  35. // 执行业务逻辑
  36. $this->processImage($data);
  37. if ($logId) {
  38. Db::name('queue_log')->where('id', $logId)->update([
  39. 'status' => 2,
  40. 'log' => '执行成功',
  41. 'updated_at' => date('Y-m-d H:i:s')
  42. ]);
  43. }
  44. $job->delete();
  45. } catch (\Exception $e) {
  46. if ($logId) {
  47. Db::name('queue_log')->where('id', $logId)->update([
  48. 'status' => 3,
  49. 'log' => $e->getMessage(),
  50. 'updated_at' => date('Y-m-d H:i:s')
  51. ]);
  52. }
  53. // 重试
  54. if ($job->attempts() < 3) {
  55. $job->release(30);
  56. } else {
  57. $job->failed();
  58. }
  59. }
  60. }
  61. // public function fire(Job $job, $data)
  62. // {
  63. // echo "队列任务开始执行\n";
  64. // echo "接收的数据: " . json_encode($data) . "\n";
  65. //
  66. // try {
  67. // // 执行实际的业务逻辑
  68. // $this->processImage($data);
  69. //
  70. // // 任务执行成功后删除
  71. // $job->delete();
  72. // echo "任务执行成wwww功\n";
  73. //
  74. // } catch (\Exception $e) {
  75. // echo "任务执行失败: " . $e->getMessage() . "\n";
  76. //
  77. // // 重试机制
  78. // if ($job->attempts() < 3) {
  79. // $job->release(30); // 30秒后重试
  80. // echo "任务重新入队,重试次数: " . $job->attempts() . "\n";
  81. // } else {
  82. // $job->failed();
  83. // echo "任务最终失败\n";
  84. // }
  85. // }
  86. // }
  87. /**
  88. * 任务失败时的处理
  89. */
  90. public function failed($data)
  91. {
  92. // 记录失败日志或发送通知
  93. \think\Log::error("ImageJob failed: " . json_encode($data));
  94. }
  95. /**
  96. * 处理图片的具体逻辑
  97. */
  98. public function processImage($data)
  99. {
  100. // 根据传入的数据处理图片
  101. $res = $this->imageToText($data["sourceDir"],$data["file_name"],$data["prompt"],$data);
  102. echo $res;
  103. }
  104. public function imageToText($sourceDirRaw,$fileName,$prompt,$call_data)
  105. {
  106. // 自动拆分文件名
  107. if (!$fileName && preg_match('/([^\/]+\.(jpg|jpeg|png))$/i', $sourceDirRaw, $matches)) {
  108. $fileName = $matches[1];
  109. $sourceDirRaw = preg_replace('/\/' . preg_quote($fileName, '/') . '$/', '', $sourceDirRaw);
  110. }
  111. // 参数校验
  112. if ($sourceDirRaw === '' || $fileName === '') {
  113. return '参数错误:原图路径 或 图片名称 不能为空';
  114. }
  115. // 构建路径
  116. $rootPath = str_replace('\\', '/', ROOT_PATH);
  117. $sourceDir = rtrim($rootPath . 'public/' . $sourceDirRaw, '/') . '/';
  118. $filePath = $sourceDir . $fileName;
  119. $relativePath = $sourceDirRaw . '/' . $fileName;
  120. // 文件检查
  121. if (!is_dir($sourceDir)) {
  122. return '源目录不存在:' . $sourceDir;
  123. }
  124. if (!is_file($filePath)) {
  125. return '文件不存在:' . $filePath;
  126. }
  127. // 获取图片信息
  128. $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
  129. $mime = ($ext === 'jpg' || $ext === 'jpeg') ? 'jpeg' : $ext;
  130. list($width, $height) = getimagesize($filePath);
  131. $imageData = base64_encode(file_get_contents($filePath));
  132. if (!$imageData || strlen($imageData) < 1000) {
  133. throw new \Exception('图片内容读取失败');
  134. }
  135. $imageUrl = "data:image/{$mime};base64,{$imageData}";
  136. // 构建严格格式的提示词
  137. //请严格按以下要求分析图案:只提取图案本身的视觉元素(图形、字母、文字、符号),忽略所有背景和载体信息(如壁画载体、衣服等), 描述必须包含: 主体图形特征(形状/颜色/材质感),文字内容(字母/单词/数字及其样式),空间排列关系,整体艺术风格---json json--- 格式:{纯图案的客观中文描述,不包含任何背景说明}---json json---{ "prompt": "English description focusing only on graphic elements with style details, on pure black background","size": "1024x1024","color_palette": ["主色1", "主色2"],"style": "图案风格"}---json json---
  138. $userPrompt = preg_replace('/\s+/u', '', $prompt); // 移除所有空白字符
  139. $strictPrompt = "严格遵守以下规则:
  140. 1. 只返回三段内容:
  141. 第一段:纯中文图案描述
  142. 第二段:---json json---
  143. 第三段:纯英文图案描述
  144. 2. 描述中必须体现图案的类型、颜色、风格等关键信息
  145. 3. 不允许添加任何解释、引导、说明、示例等文字,必须只包含图案描述内容本身
  146. 4. 示例:
  147. 这张图中的图案是代表达拉斯足球队的标志,包括一个头盔图形和围绕它的文字。头盔以灰色和白色为主,有蓝色和黑色的细节。
  148. ---json json---
  149. The pattern in this picture is the logo representing the Dallas football team, including a helmet figure and the text around it. The helmet is mainly gray and white, with blue and black details.
  150. 请直接描述这个图案:
  151. " . $userPrompt;
  152. // 调用图生文
  153. $gptRes = $this->callGptApi($imageUrl, $strictPrompt);
  154. $gptText = trim($gptRes['choices'][0]['message']['content'] ?? '');
  155. // 验证 GPT 返回格式
  156. if (strpos($gptText, '---json json---') === false) {
  157. return 'GPT 返回格式不正确,缺少分隔符';
  158. }
  159. list($chineseDesc, $englishDesc) = array_map('trim', explode('---json json---', $gptText));
  160. if ($chineseDesc === '' || $englishDesc === '') {
  161. return '描述内容为空,请检查 GPT 返回';
  162. }
  163. // 插入数据库(成功时才插入)
  164. $this->logToDatabase([
  165. 'old_image_url' => $relativePath,
  166. 'chinese_description' => $chineseDesc,
  167. 'english_description' => $englishDesc,
  168. 'size' => "",
  169. 'status' => 1
  170. ]);
  171. //进行文字转图片
  172. $res = $this->textToImage($fileName,$call_data["outputDir"],$call_data["width"],$call_data["height"],$chineseDesc.$englishDesc);
  173. return $res;
  174. }
  175. public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt)
  176. {
  177. // 统一路径分隔符为 /
  178. $rootPath = str_replace('\\', '/', ROOT_PATH);
  179. $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
  180. $dateDir = date('Y-m-d') . '/';
  181. $fullBaseDir = $outputDir . $dateDir;
  182. // 创建所需目录
  183. foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
  184. if (!is_dir($dir)) {
  185. mkdir($dir, 0755, true);
  186. }
  187. }
  188. // 规范化提示词
  189. $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
  190. // 查询数据库记录
  191. $record = Db::name('text_to_image')
  192. ->where('old_image_url', 'like', "%{$fileName}")
  193. ->order('id desc')
  194. ->find();
  195. if (!$record) {
  196. return '没有找到匹配的图像记录';
  197. }
  198. // 记录提示词日志
  199. $logDir = $rootPath . 'runtime/logs/';
  200. if (!is_dir($logDir)) mkdir($logDir, 0755, true);
  201. file_put_contents($logDir . 'prompt_log.txt', date('Y-m-d H:i:s') . " prompt: {$prompt}\n", FILE_APPEND);
  202. // 调用 DALL·E 接口
  203. $dalle1024 = $this->callDalleApi($prompt);
  204. file_put_contents($logDir . 'dalle_response.log', date('Y-m-d H:i:s') . "\n" . print_r($dalle1024, true) . "\n", FILE_APPEND);
  205. // 校验返回链接
  206. if (!isset($dalle1024['data'][0]['url']) || empty($dalle1024['data'][0]['url'])) {
  207. $errorText = $dalle1024['error']['message'] ?? '未知错误';
  208. throw new \Exception('DALL·E 生成失败:' . $errorText);
  209. }
  210. $imgUrl1024 = $dalle1024['data'][0]['url'];
  211. $imgData1024 = @file_get_contents($imgUrl1024);
  212. if (!$imgData1024 || strlen($imgData1024) < 1000) {
  213. return "下载图像失败或内容异常";
  214. }
  215. // 保存原图
  216. $filename1024 = 'dalle_' . md5($record['old_image_url'] . microtime()) . '_1024.png';
  217. $savePath1024 = $fullBaseDir . '1024x1024/' . $filename1024;
  218. file_put_contents($savePath1024, $imgData1024);
  219. // 创建图像资源
  220. $im = @imagecreatefromstring($imgData1024);
  221. if (!$im) {
  222. return "图像格式不受支持或已损坏";
  223. }
  224. // 获取原图尺寸
  225. $srcWidth = imagesx($im);
  226. $srcHeight = imagesy($im);
  227. // 创建目标图像(缩放到目标尺寸,无裁剪)
  228. $dstImg = imagecreatetruecolor($width, $height);
  229. imagecopyresampled($dstImg, $im, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  230. // 保存缩放图
  231. $filenameCustom = 'dalle_' . md5($record['old_image_url'] . microtime()) . "_custom.png";
  232. $savePathCustom = $fullBaseDir . "{$width}x{$height}/" . $filenameCustom;
  233. imagepng($dstImg, $savePathCustom);
  234. // 释放资源
  235. imagedestroy($im);
  236. imagedestroy($dstImg);
  237. // 更新数据库
  238. Db::name('text_to_image')->where('id', $record['id'])->update([
  239. 'new_image_url' => str_replace($rootPath . 'public/', '', $savePath1024),
  240. 'custom_image_url' => str_replace($rootPath . 'public/', '', $savePathCustom),
  241. 'error_msg' => '',
  242. 'size' => "{$width}x{$height}",
  243. 'updated_time' => date('Y-m-d H:i:s')
  244. ]);
  245. return 0;
  246. }
  247. public function callDalleApi($prompt)
  248. {
  249. $data = [
  250. 'prompt' => $prompt,
  251. 'model' => 'dall-e-2',
  252. 'n' => 1,
  253. 'size' => '1024x1024'
  254. ];
  255. return $this->callApi($this->config['dalle']['api_url'], $this->config['dalle']['api_key'], $data);
  256. }
  257. public function logToDatabase($data)
  258. {
  259. $record = [
  260. 'old_image_url' => $data['old_image_url'] ?? '',
  261. 'new_image_url' => $data['new_image_url'] ?? '',
  262. 'custom_image_url' => $data['custom_image_url'] ?? '',
  263. 'size' => isset($data['image_width'], $data['image_height']) ?
  264. $data['image_width'] . 'x' . $data['image_height'] : '',
  265. 'chinese_description' => $data['chinese_description'] ?? '',
  266. 'english_description' => $data['english_description'] ?? '',
  267. 'model' => 'dall-e-2',
  268. 'quality' => 'standard',
  269. 'style' => 'vivid',
  270. 'status' => $data['status'] ?? 0,
  271. 'error_msg' => $data['error_msg'] ?? '',
  272. 'created_time' => date('Y-m-d H:i:s'),
  273. 'updated_time' => date('Y-m-d H:i:s')
  274. ];
  275. if (isset($data['id'])) {
  276. Db::name('text_to_image')->where('id', $data['id'])->update($record);
  277. } else {
  278. Db::name('text_to_image')->insert($record);
  279. }
  280. }
  281. public function callGptApi($imageUrl, $prompt)
  282. {
  283. $data = [
  284. "model" => "gpt-4-vision-preview",
  285. "messages" => [[
  286. "role" => "user",
  287. "content" => [
  288. ["type" => "text", "text" => $prompt],
  289. ["type" => "image_url", "image_url" => [
  290. "url" => $imageUrl,
  291. "detail" => "auto" // ✅ 显式添加 detail 字段,兼容 vision API
  292. ]]
  293. ]
  294. ]],
  295. "max_tokens" => 1000
  296. ];
  297. return $this->callApi($this->config['gpt']['api_url'], $this->config['gpt']['api_key'], $data);
  298. }
  299. /**
  300. * 通用API调用方法
  301. */
  302. public function callApi($url, $apiKey, $data)
  303. {
  304. $maxRetries = 2;
  305. $attempt = 0;
  306. $lastError = '';
  307. while ($attempt <= $maxRetries) {
  308. $ch = curl_init();
  309. curl_setopt_array($ch, [
  310. CURLOPT_URL => $url,
  311. CURLOPT_RETURNTRANSFER => true,
  312. CURLOPT_POST => true,
  313. CURLOPT_POSTFIELDS => json_encode($data),
  314. CURLOPT_HTTPHEADER => [
  315. 'Content-Type: application/json',
  316. 'Authorization: Bearer ' . $apiKey
  317. ],
  318. CURLOPT_TIMEOUT => 120,
  319. CURLOPT_SSL_VERIFYPEER => false,
  320. CURLOPT_SSL_VERIFYHOST => 0,
  321. CURLOPT_TCP_KEEPALIVE => 1,
  322. CURLOPT_FORBID_REUSE => false
  323. ]);
  324. $response = curl_exec($ch);
  325. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  326. $curlError = curl_error($ch);
  327. curl_close($ch);
  328. if ($response !== false && $httpCode === 200) {
  329. $result = json_decode($response, true);
  330. return $result;
  331. }
  332. $lastError = $curlError ?: "HTTP错误:{$httpCode}";
  333. $attempt++;
  334. sleep(1);
  335. }
  336. throw new \Exception("请求失败(重试{$maxRetries}次):{$lastError}");
  337. }
  338. }