| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- namespace app\job;
- use think\Db;
- use think\queue\Job;
- use think\Queue;
- class ImageJob{
- protected $config = [
- 'gpt' => [
- 'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73',
- 'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
- ],
- 'dalle' => [
- 'api_key' => 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY',
- 'api_url' => 'https://niubi.zeabur.app/v1/images/generations'
- ]
- ];
- /**
- * 图生文
- */
- public function fire(Job $job, $data)
- {
- // echo "<pre>";
- // print_r($data);
- // echo "<pre>";die;
- $logId = $data['log_id'] ?? null;
- try {
- echo date('Y-m-d H:i:s')."图生文开始\n";
- if ($logId) {
- Db::name('image_task_log')->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";
- $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);
- // 调用图生文
- $gptRes = $this->callGptApi($imageUrl, $prompt);
- $gptText = trim($gptRes['choices'][0]['message']['content'] ?? '');
- // 提取英文描述
- $patternEnglish = '/^([\s\S]+?)---json json---/';
- preg_match($patternEnglish, $gptText, $matchEn);
- $englishDesc = isset($matchEn[1]) ? trim($matchEn[1]) : '';
- // 提取中文描述
- $patternChinese = '/---json json---\s*([\x{4e00}-\x{9fa5}][\s\S]+?)---json json---/u';
- preg_match($patternChinese, $gptText, $matchZh);
- $chineseDesc = isset($matchZh[1]) ? trim($matchZh[1]) : '';
- // 提取图片名(可能是中文短句,也可能是关键词)
- $patternName = '/---json json---\s*(.+)$/s';
- preg_match($patternName, $gptText, $matchName);
- $rawName = isset($matchName[1]) ? trim($matchName[1]) : '';
- $img_name = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $rawName);
- // 验证 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
- // );
- // 成功写入数据库
- $this->logToDatabase([
- 'img_name' => $img_name,
- 'old_image_url' => $relativePath,
- 'chinese_description' => $chineseDesc,
- 'english_description' => $englishDesc,
- 'size' => "",
- 'status' => 0
- ]);
- return ;
- }
- /**
- * 文生文接口
- */
- public function textToTxt($prompt,$id)
- {
- // 查询数据库记录
- $record = Db::name('text_to_image')
- ->field('id,english_description')
- ->where('id',$id)
- ->order('id desc')
- ->find();
- if (!$record) {
- return '没有找到匹配的图像记录';
- }
- // 调用文生文
- $gptRes = $this->TxtGptApi($prompt.$record['english_description']);
- $gptText = trim($gptRes['choices'][0]['message']['content'] ?? '');
- // 更新数据库记录
- Db::name('text_to_image')->where('id', $record['id'])->update([
- 'english_description' => $gptText
- ]);
- return 0;
- }
- public function logToDatabase($data)
- {
- $record = [
- 'old_image_url' => $data['old_image_url'] ?? '',
- 'new_image_url' => $data['new_image_url'] ?? '',
- 'custom_image_url' => $data['custom_image_url'] ?? '',
- 'img_name' => $data['img_name'],
- 'model' => '',
- 'size' => isset($data['image_width'], $data['image_height']) ? $data['image_width'] . 'x' . $data['image_height'] : '',
- 'chinese_description' => $data['chinese_description'] ?? '',
- 'english_description' => $data['english_description'] ?? '',
- 'status' => $data['status'] ?? 0,
- 'error_msg' => $data['error_msg'] ?? '',
- 'created_time' => date('Y-m-d H:i:s'),
- 'updated_time' => date('Y-m-d H:i:s')
- ];
- if (isset($data['id'])) {
- Db::name('text_to_image')->where('id', $data['id'])->update($record);
- } else {
- Db::name('text_to_image')->insert($record);
- }
- }
- /**
- * 图升文模型
- */
- public function callGptApi($imageUrl, $prompt)
- {
- $data = [
- "model" => "gpt-4-vision-preview",
- "messages" => [[
- "role" => "user",
- "content" => [
- ["type" => "text", "text" => $prompt],
- ["type" => "image_url", "image_url" => [
- "url" => $imageUrl,
- "detail" => "auto" // ✅ 显式添加 detail 字段,兼容 vision API
- ]]
- ]
- ]],
- "max_tokens" => 1000
- ];
- return $this->callApi($this->config['gpt']['api_url'], $this->config['gpt']['api_key'], $data);
- }
- /**
- * 文升文模型
- */
- public function TxtGptApi($prompt)
- {
- $data = [
- 'prompt' => $prompt,
- 'model' => 'gpt-4',
- 'session_id' => null,
- 'context_reset' => true
- ];
- return $this->callApi($this->config['gpt']['api_url'],$this->config['gpt']['api_key'],$data);
- }
- /**
- * 通用API调用方法
- */
- public function callApi($url, $apiKey, $data)
- {
- $maxRetries = 2;
- $attempt = 0;
- $lastError = '';
- while ($attempt <= $maxRetries) {
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_HTTPHEADER => [
- 'Content-Type: application/json',
- 'Authorization: Bearer ' . $apiKey
- ],
- CURLOPT_TIMEOUT => 120,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_TCP_KEEPALIVE => 1,
- CURLOPT_FORBID_REUSE => false
- ]);
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $curlError = curl_error($ch);
- curl_close($ch);
- if ($response !== false && $httpCode === 200) {
- $result = json_decode($response, true);
- return $result;
- }
- $lastError = $curlError ?: "HTTP错误:{$httpCode}";
- $attempt++;
- sleep(1);
- }
- throw new \Exception("请求失败(重试{$maxRetries}次):{$lastError}");
- }
- }
|