[ '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) { $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 ]); } $fullPath = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/'); $list = Db::name("text_to_image") ->where('old_image_url', $fullPath) ->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"] ?? null ); $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') ]); } } $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, 'update_time' => date('Y-m-d H:i:s') ]); return "跳过生成:记录 ID {$skipId},包含关键词 - {$keyword}"; } } //文生图调用 $dalle1024 = $this->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; // 更新数据库记录 $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, 'error_msg' => '', 'model' => $selectedOption, 'quality' => 'hd', 'style' => 'vivid', 'size' => "{$width}x{$height}", 'updated_time' => date('Y-m-d H:i:s'), 'status' => $status ]); return 0; } /** * 处理字符串长度,超出限制则截断 * * @param string $str 输入字符串 * @param int $maxLength 最大长度限制(默认200) * @return string 处理后的字符串 */ public function limitStringLength($str, $maxLength = 10) { // 如果字符串长度没有超出限制,直接返回 if (mb_strlen($str, 'UTF-8') <= $maxLength) { return $str; } // 超出限制则截断 return mb_substr($str, 0, $maxLength, 'UTF-8'); } public function cleanImageUrl($input) { // 去除字符串首尾空格和中文引号替换为英文引号 $input = trim($input); $input = str_replace(['“', '”', '‘', '’'], '"', $input); // 判断是否为纯中文文字 if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $input)) { // 纯中文:替换掉不适合用于文件名的字符 $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input); } elseif (preg_match('/[a-zA-Z]/', $input) && !preg_match('/[\x{4e00}-\x{9fa5}]/u', $input)) { // 如果是纯字母和空格,且没有中文字符:保留空格,去掉其他符号 $cleaned = preg_replace('/[^a-zA-Z\s]/', '', $input); } else { // 如果包含中文或是其他混合字符,按照纯中文的规则清理符号 $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input); } return $cleaned; } /** * 文生图模 */ public function callDalleApi($prompt,$selectedOption) { if($selectedOption == 'dall-e-3'){ $data = [ 'prompt' => $prompt, 'model' => $selectedOption, 'n' => 1, 'size' => '1024x1024', 'quality' => 'standard', 'style' => 'vivid', 'response_format' => 'url', 'session_id' => null, 'context_reset' => true ]; }else{ $data = [ 'prompt' => $prompt, 'model' => $selectedOption, 'n' => 1, 'size' => '1024x1024', 'quality' => 'hd', 'style' => 'vivid', 'response_format' => 'url', 'session_id' => null, 'context_reset' => true ]; } return $this->callApi($this->config['dalle']['api_url'], $this->config['dalle']['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}"); } }