| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace app\job;
- use app\service\AIGatewayService;
- use think\Db;
- use think\Queue;
- use think\queue\Job;
- /**
- * 文生图任务处理类
- * 描述:接收提示词,通过模型生成图像,保存图像并记录数据库信息,是链式任务中的最后一环
- */
- class TextToImageJob
- {
- /**
- * 队列入口方法
- * @param Job $job 队列任务对象
- * @param array $data 任务传参,包含图像文件名、路径、尺寸、提示词等
- */
- public function fire(Job $job, $data)
- {
- $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["english_description"],
- $row["img_name"],
- $data["selectedOption"]
- );
- // 标准化结果文本
- 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();
- }
- }
- /**
- * 任务失败时的处理
- */
- public function failed($data)
- {
- // 记录失败日志或发送通知
- echo "ImageJob failed: " . json_encode($data);
- }
- /**
- * 文生图处理函数
- * 描述:根据提示词调用图像生成接口,保存图像文件,并更新数据库
- */
- public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name,$selectedOption)
- {
- $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);
- }
- }
- // 查询数据库记录
- $record = Db::name('text_to_image')
- ->where('old_image_url', 'like', "%{$fileName}")
- ->order('id desc')
- ->find();
- if (!$record) {
- return '没有找到匹配的图像记录';
- }
- // 写入 prompt 日志
- $logDir = $rootPath . 'runtime/logs/';
- if (!is_dir($logDir)) mkdir($logDir, 0755, true);
- // 调用文生图模型接口生成图像
- $startTime = microtime(true);
- // 清理 prompt 的换行
- $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
- // 定义要跳过的关键词(可按需扩展)
- $skipKeywords = ['几何', 'geometry', 'geometric'];
- foreach ($skipKeywords as $keyword) {
- // 判断提示词中是否包含关键词(不区分大小写)
- if (stripos($prompt, $keyword) !== false) {
- $skipId = $record['id'];
- echo "🚫 跳过生成:包含关键词“{$keyword}”,记录 ID:{$skipId}\n";
- Db::name('text_to_image')->where('id', $skipId)->update([
- 'status' => 3,
- 'error_msg' => "包含关键词".$keyword,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- return "包含关键词 - {$keyword}";
- }
- }
- //文生图调用
- $startTime = microtime(true);
- $ai = new AIGatewayService();
- $dalle1024 = $ai->callDalleApi($prompt,$selectedOption);
- $endTime = microtime(true);
- $executionTime = $endTime - $startTime;
- echo "✅ API 调用耗时: " . round($executionTime, 3) . " 秒\n";
- // 错误检查
- if (isset($dalle1024['error'])) {
- throw new \Exception("❌ 图像生成接口错误:" . ($dalle1024['error']['message'] ?? '未知错误'));
- }
- // 提取 url 图像
- // $base64Image = $dalle1024['data'][0]['url'] ?? null;
- // 提取 base64 图像
- $base64Image = $dalle1024['data'][0]['b64_json'] ?? null;
- if (!$base64Image || strlen($base64Image) < 1000) {
- file_put_contents('/tmp/empty_image_base64.txt', json_encode($dalle1024, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
- throw new \Exception("❌ 图像内容为空或异常,详情写入 /tmp/empty_image_base64.txt");
- }
- // 解码图片
- $imgData = base64_decode($base64Image);
- // 判断是否为图像
- $finfo = finfo_open(FILEINFO_MIME_TYPE);
- $mimeType = finfo_buffer($finfo, $imgData);
- finfo_close($finfo);
- // 保存图片路径
- $img_name = mb_substr(preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name), 0, 30);
- $filename = $img_name . '.png';
- $saveDir1024 = $fullBaseDir . '1024x1024/';
- $saveDirCustom = $fullBaseDir . "{$width}x{$height}/";
- @mkdir($saveDir1024, 0755, true);
- @mkdir($saveDirCustom, 0755, true);
- $path1024 = $saveDir1024 . $filename;
- $pathCustom = $saveDirCustom . $filename;
- file_put_contents($path1024, $imgData);
- // 解析图像内容
- try {
- $im = \imagecreatefromstring($imgData);
- if (!$im) {
- file_put_contents('/tmp/corrupted.png', $imgData);
- throw new \Exception("❌ 图像无法解析,写入 /tmp/corrupted.png");
- }
- } catch (\Throwable $e) {
- file_put_contents('/tmp/corrupted.png', $imgData);
- throw new \Exception("❌ 图像处理异常:" . $e->getMessage());
- }
- // 裁剪
- $srcW = imagesx($im);
- $srcH = imagesy($im);
- $srcRatio = $srcW / $srcH;
- $dstRatio = $width / $height;
- if ($srcRatio > $dstRatio) {
- $cropW = intval($srcH * $dstRatio);
- $cropH = $srcH;
- $srcX = intval(($srcW - $cropW) / 2);
- $srcY = 0;
- } else {
- $cropW = $srcW;
- $cropH = intval($srcW / $dstRatio);
- $srcX = 0;
- $srcY = intval(($srcH - $cropH) / 2);
- }
- $dstImg = imagecreatetruecolor($width, $height);
- imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropW, $cropH);
- // 保存裁剪图
- imagepng($dstImg, $pathCustom);
- imagedestroy($im);
- imagedestroy($dstImg);
- // 写入数据库
- Db::name('text_to_image')->where('id', $record['id'])->update([
- 'new_image_url' => str_replace($rootPath . 'public/', '', $path1024),
- '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 "成功";
- }
- }
|