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\n"; foreach ($list as $index => $row) { $currentIndex = $index + 1; $begin = date('Y-m-d H:i:s'); echo "处理时间:{$begin}\n"; 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"] ); $resultText = ($result === true || $result === 1 || $result === '成功') ? '成功' : '失败或无返回'; echo "✅ 处理结果:{$resultText}\n"; $end = date('Y-m-d H:i:s'); echo "完成时间:{$end}\n"; echo "Processed: " . static::class . "\n"; echo "文生图已处理完成\n\n"; } // 更新日志状态:成功 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"; } else { echo "⚠ 未找到可处理的数据,跳过执行\n"; if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => 2, 'log' => '无数据可处理,已跳过', '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"; $updateRes = Db::name('text_to_image')->where('id', $skipId)->update([ 'status' => 3, 'error_msg' => "提示词中包含关键词".$keyword, 'update_time' => date('Y-m-d H:i:s') ]); return "跳过生成:记录 ID {$skipId},包含关键词 - {$keyword}"; } } //文生图调用 $ai = new AIGatewayService(); $dalle1024 = $ai->callDalleApi($prompt,$selectedOption); //检测查询接口调用时长 $endTime = microtime(true); $executionTime = $endTime - $startTime; echo "API调用耗时: " . round($executionTime, 3) . " 秒\n"; // 检查 URL 返回是否成功 if (!isset($dalle1024['data'][0]['url']) || empty($dalle1024['data'][0]['url'])) { $errorText = $dalle1024['error']['message'] ?? '未知错误'; Db::name('text_to_image')->where('id', $record['id'])->update([ 'error_msg' => '生成失败:' . $errorText, 'status' => 0 ]); return '生成失败:' . $errorText; } // 下载图像 $imgUrl1024 = $dalle1024['data'][0]['url']; $imgData1024 = @file_get_contents($imgUrl1024); if (!$imgData1024 || strlen($imgData1024) < 1000) { return "下载图像失败或内容异常"; } // 保存原图(1024x1024) $img_name = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name); $img_name = mb_substr($img_name, 0, 30); // 限制为前30个字符(避免路径过长) $filename1024 = $img_name . '.png'; $savePath1024 = $fullBaseDir . '1024x1024/' . $filename1024; file_put_contents($savePath1024, $imgData1024); // 图像裁剪生成自定义尺寸图 $im = @imagecreatefromstring($imgData1024); if (!$im) return "图像损坏或格式不支持"; $srcWidth = imagesx($im); $srcHeight = imagesy($im); $srcRatio = $srcWidth / $srcHeight; $dstRatio = $width / $height; // 居中裁剪逻辑 if ($srcRatio > $dstRatio) { $cropHeight = $srcHeight; $cropWidth = intval($srcHeight * $dstRatio); $srcX = intval(($srcWidth - $cropWidth) / 2); $srcY = 0; } else { $cropWidth = $srcWidth; $cropHeight = intval($srcWidth / $dstRatio); $srcX = 0; $srcY = intval(($srcHeight - $cropHeight) / 2); } $dstImg = imagecreatetruecolor($width, $height); imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight); // 保存裁剪后图像 $filenameCustom = $img_name . ".png"; $savePathCustom = $fullBaseDir . "{$width}x{$height}/" . $filenameCustom; imagepng($dstImg, $savePathCustom); imagedestroy($im); imagedestroy($dstImg); // 成功写入数据库记录 $status = trim($img_name) === '' ? 0 : 1; // 根据 selectedOption 设置 quality 和 style $quality = 'hd'; $style = 'vivid'; if ($selectedOption === 'dall-e-3') { $quality = 'standard'; $style = 'vivid'; } $updateRes = Db::name('text_to_image')->where('id', $record['id'])->update([ 'new_image_url' => str_replace($rootPath . 'public/', '', $savePath1024), 'custom_image_url' => str_replace($rootPath . 'public/', '', $savePathCustom), 'img_name' => $img_name, 'model' => $selectedOption, 'status' => $status, 'status_name' => "文生图", 'size' => "{$width}x{$height}", 'quality' => $quality, 'style' => $style, 'error_msg' => '', 'update_time' => date('Y-m-d H:i:s') ]); return 0; } }