where('id', $logId)->update([ 'status' => 1, 'log' => '图生文处理中', 'update_time' => date('Y-m-d H:i:s') ]); } //执行逻辑 $result = $this->processImage($data); echo $result; // 更新日志状态:成功 if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => 2, 'log' => '图生文处理成功', 'update_time' => date('Y-m-d H:i:s') ]); } echo date('Y-m-d H:i:s')."图生文结束\n"; //链式任务:图生文成功后继续推送文生文 if (!empty($data['chain_next'])) { // 获取下一个任务类型 $nextType = array_shift($data['chain_next']); $data['type'] = $nextType; // 保留剩余链,继续传下去 Queue::push('app\job\ImageArrJob', [ 'task_id' => $data['task_id'], 'data' => [ $data ] ], 'arrimage'); } // 删除当前任务 $job->delete(); } catch (\Exception $e) { //异常处理,记录失败日志 echo "错误信息: " . $e->getMessage() . "\n"; echo "文件: " . $e->getFile() . "\n"; echo "行号: " . $e->getLine() . "\n"; if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => -1, 'log' => '图生文失败:' . $e->getMessage(), 'update_time' => date('Y-m-d H:i:s') ]); } // 删除当前任务 $job->delete(); } } /** * 失败回调(可用于后续通知或重试机制) */ public function failed($data) { echo "ImageJob failed: " . json_encode($data); } public function processImage($data) { // 根据传入的数据处理图片 $res = $this->imageToText($data["sourceDir"],$data["file_name"],$data["prompt"],$data["sys_id"],$data["imgtotxt_selectedOption"],$data); echo $res; } /** * 执行图生文逻辑(图像转文本) */ public function imageToText($sourceDirRaw, $fileName, $prompt,$sys_id,$imgtotxt_selectedOption,$call_data) { // 自动拆分文件名 if (!$fileName && preg_match('/([^\/]+\.(jpg|jpeg|png))$/i', $sourceDirRaw, $matches)) { $fileName = $matches[1]; $sourceDirRaw = preg_replace('/\/' . preg_quote($fileName, '/') . '$/', '', $sourceDirRaw); } if ($sourceDirRaw === '' || $fileName === '') { return '参数错误:原图路径 或 图片名称 不能为空'; } // 构建路径 $rootPath = str_replace('\\', '/', ROOT_PATH); $sourceDir = rtrim($rootPath . 'public/' . $sourceDirRaw, '/') . '/'; $filePath = $sourceDir . $fileName; $relativePath = $sourceDirRaw . '/' . $fileName; // 文件夹是否存在(绝对路径检查) if (!is_dir($sourceDir)) { return '源目录不存在:' . $sourceDir; } // 文件是否存在 if (!is_file($filePath)) { return '文件不存在:' . $filePath; } // 获取图片信息 $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); $mime = ($ext === 'jpg' || $ext === 'jpeg') ? 'jpeg' : $ext; list($width, $height) = getimagesize($filePath); $imageData = base64_encode(file_get_contents($filePath)); if (!$imageData || strlen($imageData) < 1000) { throw new \Exception('图片内容读取失败'); } $imageUrl = "data:image/{$mime};base64,{$imageData}"; // 记录提示词日志 $logDir = $rootPath . 'runtime/logs/'; if (!is_dir($logDir)) mkdir($logDir, 0755, true); // 调用 图生文 接口 $ai = new AIGatewayService(); $gptRes = $ai->callGptApi($imageUrl, $prompt,$imgtotxt_selectedOption); $gptText = trim($gptRes['choices'][0]['message']['content'] ?? ''); //图生文返回内容日志runtime/logs/img_to_txt.txt // file_put_contents( // $logDir . 'img_to_txt.txt', // "\n======== " . date('Y-m-d H:i:s') . " ========\n" . // $gptText. "\n\n", // FILE_APPEND // );die; // 验证 GPT 返回格式 if (strpos($gptText, '---json json---') === false) { return 'GPT 返回格式不正确,缺少分隔符'; } // 以 ---json json--- 分割 $parts = array_map('trim', explode('---json json---', $gptText)); // 清理“第一段”、“第二段”等标签前缀 $cleanPrefix = function ($text) { return preg_replace('/^第[一二三四五六七八九十]+段[::]?\s*/u', '', $text); }; // 防止越界,逐个安全提取 $chineseDesc = isset($parts[0]) ? $cleanPrefix(trim($parts[0])) : ''; $englishDesc = isset($parts[1]) ? $cleanPrefix(trim($parts[1])) : ''; $part2 = isset($parts[2]) ? $cleanPrefix(trim($parts[2])) : ''; // 只保留中英文、数字、下划线、短横线、空格 $img_name = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $part2); //图生文返回日志拆分后内容runtime/logs/img_to_txt.txt // file_put_contents( // $logDir . 'img_to_txt.txt', // "\n======== " . date('Y-m-d H:i:s') . " ========\n" . // $englishDesc . "\n--- 中文 ---\n" . // $chineseDesc . "\n---英文 ---\n" . // $img_name . "\n--- 命名 ---\n" , // FILE_APPEND // ); $now = date('Y-m-d H:i:s'); $record = [ 'chinese_description' => $chineseDesc, 'english_description' => $englishDesc, 'img_name' => $img_name, 'sys_id' => $sys_id, 'status' => 0, 'status_name' => "图生文", 'model' => "", 'size' => "", 'error_msg' => '', 'update_time' => $now ]; // 查询是否存在相同的原图记录 $exists = Db::name('text_to_image')->where('old_image_url', $relativePath)->find(); if ($exists) { // 如果已存在,执行更新 Db::name('text_to_image')->where('id', $exists['id'])->update($record); } else { // 不存在则添加必要字段并插入 $record['old_image_url'] = $relativePath; $record['new_image_url'] = ''; $record['custom_image_url'] = ''; $record['created_time'] = $now; Db::name('text_to_image')->insert($record); } return ; } }