delete(); return; } if (empty($Id)) { $job->delete(); return; } //连接Redis(配置见 application/extra/queue.php) $redis = getTaskRedis(); echo "\n" . date('Y-m-d H:i:s') . " 任务开始:{$taskId}\n"; // 更新任务状态为处理中 $redis->set("text_to_image_task:{$taskId}", json_encode([ 'status' => 'processing', 'started_at' => date('Y-m-d H:i:s') ]), ['EX' => 300]); // 5分钟过期 try { // 执行图片生成 $result = $this->get_txt_to_img($data); // sleep(10); // 更新Redis中的任务状态为成功 $redis->set("text_to_image_task:{$taskId}", json_encode([ 'status' => 'completed', // 'image_url' => "/uploads/merchant/690377511/6903775111138/newimg/698550113c2b8.jpeg", 'image_url' => $result, 'completed_at' => date('Y-m-d H:i:s') ]), ['EX' => 300]); // 5分钟过期 echo "🎉 任务 {$taskId} 执行完成,图片生成成功!\n"; $job->delete(); } catch (\Exception $e) { echo "❌ 任务执行失败:" . $e->getMessage() . "\n"; // 检查是否是网络超时错误 if (strpos($e->getMessage(), 'Connection timed out') !== false) { // 对于超时错误,保持任务状态为处理中,让前端继续查询 echo "⚠️ 检测到网络超时,任务可能仍在执行中\n"; $redis->set("text_to_image_task:{$taskId}", json_encode([ 'status' => 'processing', 'error' => '网络连接超时,正在重试...', 'updated_at' => date('Y-m-d H:i:s') ]), ['EX' => 300]); // 5分钟过期 } else { // 其他错误,标记为失败 $redis->set("text_to_image_task:{$taskId}", json_encode([ 'status' => 'failed', 'error' => $e->getMessage(), 'completed_at' => date('Y-m-d H:i:s') ]), ['EX' => 300]); // 5分钟过期 } $job->delete(); } finally { $job->delete(); } } else { $logId = $data['log_id'] ?? null; try { // 任务类型校验(必须是文生图) if (!isset($data['type']) || $data['type'] !== '文生图') { $job->delete(); return; } $startTime = date('Y-m-d H:i:s'); echo "━━━━━━━━━━ ▶ 文生图任务开始处理━━━━━━━━━━\n"; echo "处理时间:{$startTime}\n"; // 更新日志状态:处理中 if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => 1, 'log' => '文生图处理中', 'update_time' => $startTime ]); } // 拼接原图路径 $old_image_url = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/'); $list = Db::name("text_to_image") ->where('old_image_url', $old_image_url) ->where('img_name', '<>', '') // ->where('status', 0) ->select(); if (!empty($list)) { $total = count($list); echo "📊 共需处理:{$total} 条记录\n"; foreach ($list as $index => $row) { $currentIndex = $index + 1; $begin = date('Y-m-d H:i:s'); echo "👉 正在处理第 {$currentIndex} 条,ID: {$row['id']}\n"; // 图像生成 $result = $this->textToImage( $data["file_name"], $data["outputDir"], $data["width"], $data["height"], $row["chinese_description"], $row["img_name"], $data["selectedOption"], $data["executeKeywords"], $data['sourceDir'] ); // 标准化结果文本 if ($result === true || $result === 1 || $result === '成功') { $resultText = '成功'; // 日志状态设置为成功(仅在未提前失败时) if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => 2, 'log' => '文生图处理成功', 'update_time' => date('Y-m-d H:i:s') ]); } } else { $resultText = (string) $result ?: '失败或无返回'; } echo "✅ 处理结果:{$resultText}\n"; echo "完成时间:" . date('Y-m-d H:i:s') . "\n"; echo "文生图已处理完成\n"; // 若包含关键词,日志状态标为失败(-1) if (strpos($resultText, '包含关键词') !== false) { // 命中关键词类错误,状态设为失败 if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => -1, 'log' => $resultText, 'update_time' => date('Y-m-d H:i:s') ]); } } } } // 处理链式任务(如果有) 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(); } } $job->delete(); } /** * 任务失败时的处理 */ public function failed($data) { // 记录失败日志或发送通知 echo "ImageJob failed: " . json_encode($data); } public function get_txt_to_img($data){ $status_val = trim($data['status_val']); $prompt = trim($data['prompt']); $model = trim($data['model']); $size = trim($data['size']); // 获取产品信息 $product = Db::name('product')->where('id', $data['id'])->find(); if (empty($product)) { return '产品不存在'; } // 调用AI生成图片 $aiGateway = new AIGatewayService(); $res = $aiGateway->buildRequestData($status_val,$model,$prompt,$size); // 提取base64图片数据,兼容两种返回格式 $base64_data = null; $image_type = 'png'; // 新格式:data[0].b64_json 或 data[0].url if (isset($res['data'][0]['b64_json']) && $res['data'][0]['b64_json']) { $base64_data = preg_replace('/\s+/', '', $res['data'][0]['b64_json']); } elseif (isset($res['data'][0]['url']) && $res['data'][0]['url']) { $imageContent = file_get_contents($res['data'][0]['url']); $base64_data = base64_encode($imageContent); } // 旧格式:candidates[0].content.parts[0].inlineData.data 或 text elseif (isset($res['candidates'][0]['content']['parts'][0]['inlineData']['data'])) { $text_content = $res['candidates'][0]['content']['parts'][0]['inlineData']['data']; // 带前缀 data:image/xxx;base64, 或 裸 base64 都能识别 if (preg_match('/data:image\/(png|jpg|jpeg|webp);base64,(.+)$/is', $text_content, $matches)) { $image_type = ($matches[1] == 'jpg' ? 'jpeg' : $matches[1]); $base64_data = preg_replace('/\s+/', '', $matches[2]); } else { $base64_data = preg_replace('/\s+/', '', $text_content); } } elseif (isset($res['candidates'][0]['content']['parts'][0]['text'])) { $text_content = $res['candidates'][0]['content']['parts'][0]['text']; if (preg_match('/data:image\/(png|jpg|jpeg|webp);base64,(.+)$/is', $text_content, $matches)) { $image_type = ($matches[1] == 'jpg' ? 'jpeg' : $matches[1]); $base64_data = preg_replace('/\s+/', '', $matches[2]); } else { $base64_data = preg_replace('/\s+/', '', $text_content); } } if (empty($base64_data)) { return '未找到图片数据'; } $image_data = base64_decode($base64_data); if ($image_data === false) { return '图片解码失败'; } $rootPath = str_replace('\\', '/', ROOT_PATH); $relDir = '/uploads/ceshi/'; // 生产:$code = $product['product_code']; // 生产:$relDir = '/uploads/merchant/' . substr($code, 0, 9) . '/' . $code . '/newimg/'; $saveDir = rtrim($rootPath, '/') . '/public' . $relDir; if (!is_dir($saveDir)) { mkdir($saveDir, 0755, true); } $file_name = uniqid() . '.' . $image_type; if (!file_put_contents($saveDir . $file_name, $image_data)) { return '图片保存失败'; } $db_img_path = $relDir . $file_name; echo "
";
print_r($db_img_path);
echo "";die;
Db::name('product')->where('id', $data['id'])->update([
'createTime' => date('Y-m-d H:i:s'),
'content' => $data['prompt'],
'product_new_img' => $db_img_path
]);
$record = [
'product_id' => $data['id'],
'product_new_img' => $db_img_path,
'product_content' => $data['prompt'],
'template_id' => $data['template_id'] ?? 0,
'createTime' => date('Y-m-d H:i:s'),
];
Db::name('product_image')->insert($record);
return $db_img_path;
}
/**
* 文生图处理函数
* 描述:根据提示词调用图像生成接口,保存图像文件,并更新数据库
*/
public function textToImage($data,$prompt,$img_name)
{
$fileName = $data["file_name"];
$outputDirRaw = $data["outputDir"];
$width = $data["width"];
$height = $data["height"];
$selectedOption = $data["selectedOption"];
$executeKeywords = $data["executeKeywords"];
$sourceDir = $data["sourceDir"];
$rootPath = str_replace('\\', '/', ROOT_PATH);
$outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
$dateDir = date('Y-m-d') . '/';
$fullBaseDir = $outputDir . $dateDir;
// 创建输出目录
foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
if (!is_dir($dir)) mkdir($dir, 0755, true);
}
// 确保目录存在
if (!is_dir($fullBaseDir . '2048x2048/')) {
mkdir($fullBaseDir . '2048x2048/', 0755, true);
}
// 获取图像记录
$record = Db::name('text_to_image')
->where('old_image_url', 'like', "%{$fileName}")
->order('id desc')
->find();
Db::name('text_to_image')->where('id', $record['id'])->update([
'new_image_url' => '',
]);
if (!$record) return '没有找到匹配的图像记录';
//判断是否执行几何图
if($executeKeywords == false){
// 过滤关键词
$prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
foreach (['几何', 'geometry', 'geometric'] as $keyword) {
if (stripos($prompt, $keyword) !== false) {
Db::name('text_to_image')->where('id', $record['id'])->update([
'status' => 3,
'error_msg' => "包含关键词".$keyword,
'update_time' => date('Y-m-d H:i:s')
]);
return "包含关键词 - {$keyword}";
}
}
}
$template = Db::name('template')
->field('id,english_content,content')
->where('path',$sourceDir)
->where('ids',1)
->find();
// AI 图像生成调用
$ai = new AIGatewayService();
$response = $ai->buildRequestData('文生图',$template['content'].$prompt, $selectedOption);
if (isset($response['error'])) {
throw new \Exception("❌ 图像生成失败:" . $response['error']['message']);
}
// 支持 URL 格式(为主)和 base64
$imgData = null;
if (isset($res['candidates'][0]['content']['parts'][0]['text'])) {
$text_content = $res['candidates'][0]['content']['parts'][0]['text'];
// 匹配base64图片数据
preg_match('/data:image\/(png|jpg|jpeg);base64,([^"]+)/', $text_content, $matches);
if (empty($matches)) {
return '未找到图片数据';
}
$image_type = $matches[1];
$base64_data = $matches[2];
// 解码base64数据
$imgData = base64_decode($base64_data);
}else if (isset($response['data'][0]['url'])) {
$imgData = @file_get_contents($response['data'][0]['url']);
} elseif (isset($response['data'][0]['b64_json'])) {
$imgData = base64_decode($response['data'][0]['b64_json']);
}
if (!$imgData || strlen($imgData) < 1000) {
throw new \Exception("❌ 图像内容为空或异常!");
}
// 保存文件路径定义
$img_name = mb_substr(preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name), 0, 30);
$filename = $img_name . '.png';
$path512 = $fullBaseDir . '1024x1024/' . $filename;
$pathCustom = $fullBaseDir . "{$width}x{$height}/" . $filename;
// 保存原图
file_put_contents($path512, $imgData);
// 数据库更新
Db::name('text_to_image')->where('id', $record['id'])->update([
'new_image_url' => str_replace($rootPath . 'public/', '', $path512),
// 注释以下一行则不保存裁剪路径(适配你的配置)
// 'custom_image_url' => str_replace($rootPath . 'public/', '', $pathCustom),
'img_name' => $img_name,
'model' => $selectedOption,
'status' => trim($img_name) === '' ? 0 : 1,
'status_name' => "文生图",
'size' => "{$width}x{$height}",
'quality' => 'standard',
'style' => 'vivid',
'error_msg' => '',
'update_time' => date('Y-m-d H:i:s')
]);
return "成功";
}
public function getImageSeed($taskId)
{
// 配置参数
$apiUrl = 'https://chatapi.onechats.ai/mj/task/' . $taskId . '/fetch';
$apiKey = 'sk-iURfrAgzAjhZ4PpPLwzmWIAhM7zKfrkwDvyxk4RVBQ4ouJNK';
try {
// 初始化cURL
$ch = curl_init();
// 设置cURL选项
curl_setopt_array($ch, [
CURLOPT_URL => $apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET', // 明确指定GET方法
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Accept: application/json',
'Content-Type: application/json'
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_TIMEOUT => 60,
CURLOPT_FAILONERROR => true // 添加失败时返回错误
]);
// 执行请求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 错误处理
if (curl_errno($ch)) {
throw new Exception('cURL请求失败: ' . curl_error($ch));
}
// 关闭连接
curl_close($ch);
// 验证HTTP状态码
if ($httpCode < 200 || $httpCode >= 300) {
throw new Exception('API返回错误状态码: ' . $httpCode);
}
// 解析JSON响应
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('JSON解析失败: ' . json_last_error_msg());
}
// 返回结构化数据
return [
'success' => true,
'http_code' => $httpCode,
'data' => $responseData
];
} catch (Exception $e) {
// 确保关闭cURL连接
if (isset($ch) && is_resource($ch)) {
curl_close($ch);
}
return [
'success' => false,
'error' => $e->getMessage(),
'http_code' => $httpCode ?? 0
];
}
}
/**
* 发送API请求
* @param string $url 请求地址
* @param array $data 请求数据
* @param string $apiKey API密钥
* @param string $method 请求方法
* @return string 响应内容
* @throws Exception
*/
private function sendApiRequest($url, $data, $apiKey, $method = 'POST')
{
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer '.$apiKey,
'Accept: application/json',
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => $method === 'POST' ? json_encode($data) : null,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_TIMEOUT => 60,
CURLOPT_FAILONERROR => true
]);
$response = curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($error) {
throw new Exception('API请求失败: '.$error);
}
if ($httpCode < 200 || $httpCode >= 300) {
throw new Exception('API返回错误状态码: '.$httpCode);
}
return $response;
}
}