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); echo $res; } /** * 执行图生文逻辑(图像转文本) */ public function imageToText($sourceDirRaw, $fileName, $prompt, $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); $gptText = trim($gptRes['choices'][0]['message']['content'] ?? ''); //gtp返回内容日志 // file_put_contents( // $logDir . 'img_name_success.txt', // "\n======== " . date('Y-m-d H:i:s') . " ========\n" . // $gptText. "\n\n", // FILE_APPEND // ); // echo "
";print_r($gptText);echo ""; return 0;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);
};
// 防止越界,逐个安全提取
$englishDesc = isset($parts[0]) ? $cleanPrefix(trim($parts[0])) : '';
$chineseDesc = 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);
// 成功后的图生文提示词日志
// file_put_contents(
// $logDir . 'img_name_success.txt',
// "\n======== " . date('Y-m-d H:i:s') . " ========\n" .
// $englishDesc . "\n---json json---\n" .
// $chineseDesc . "\n---json json---\n" .
// $img_name . "\n\n",
// FILE_APPEND
// );
// 成功写入数据库
$record = [
'chinese_description' => $chineseDesc,
'english_description' => $englishDesc,
'old_image_url' => $relativePath,
'new_image_url' => '',
'custom_image_url' => '',
'img_name' => $img_name,
'status' => 0,
'status_name' => "图生文",
'model' => "",
'size' => "",
'error_msg' => '',
'created_time' => date('Y-m-d H:i:s'),
'update_time' => date('Y-m-d H:i:s')
];
Db::name('text_to_image')->insert($record);
return ;
}
}