[ '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 "已经到了文生图阶段\n"; try { $str = $this->textToImage( $data["fileName"], $data["outputDir"], $data["width"], $data["height"], $data["englishDesc"], $data["img_name"] ); echo $str; echo "文生图结束\n"; $job->delete(); } catch (\Exception $e) { echo "Error message: " . $e->getMessage() . "\n"; echo "Error file: " . $e->getFile() . "\n"; echo "Error line: " . $e->getLine() . "\n"; echo "Stack trace: " . $e->getTraceAsString() . "\n"; // 如果有 log_id,更新任务状态为“执行失败”并记录错误信息 // 最多重试一次(总执行两次) if ($job->attempts() < 2) { $job->release(30); // 延迟30秒再次执行 } else { $job->failed(); // 达到最大尝试次数,标记失败 } } } /** * 任务失败时的处理 */ public function failed($data) { // 记录失败日志或发送通知 echo "ImageJob failed: " . json_encode($data); } /** * 文生图接口 */ public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name) { $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); } } // 清理 prompt 的换行 $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt); // 查询数据库记录 $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); // echo 2345; // 调用文生图模型接口生成图像 $startTime = microtime(true); $dalle1024 = $this->callDalleApi($prompt); // $dalle1024 = json_decode('{"created":1747932746,"data":[{"revised_prompt":"**First paragraph:** A geometric abstract design with a central motif consisting of curved and angular shapes in a symmetrical arrangement. The color scheme predominantly features shades of blue, with hints of white and black creating a contrast. The design incorporates smooth, flowing lines mixed with sharp angles. The overall style has a modern, minimalist aesthetic, with a focus on balance and clean shapes.","url":"https:\/\/filesystem.site\/cdn\/20250523\/3NVcCUaZDkLimWjtgOwJYniGezDX8d.png"}],"usage":{"total_tokens":4250,"input_tokens":75,"output_tokens":4175,"input_tokens_details":{"text_tokens":75}}}',true); $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'] ?? '未知错误'; echo '生成失败:' . $errorText; } // echo 342342; // 下载图像 $imgUrl1024 = $dalle1024['data'][0]['url']; $imgData1024 = @file_get_contents($imgUrl1024); if (!$imgData1024 || strlen($imgData1024) < 1000) { return "下载图像失败或内容异常"; } // echo 3423424444; // file_put_contents( // $logDir . 'img_name.txt', // "\n====图片 " . date('Y-m-d H:i:s') . " ====\n" . $img_name . "\n\n", // FILE_APPEND // ); // 保存原图(1024x1024) // $img_name = $this->limitStringLength($img_name); $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); // echo 342344543532; // 处理缩略图 $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); } // echo 789; $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); // file_put_contents( // $logDir . 'image_url.txt', // "\n====图片路径 " . date('Y-m-d H:i:s') . " ====\n" . str_replace($rootPath . 'public/', '', $savePath1024) . "\n\n", // "\n====图片路径 " . date('Y-m-d H:i:s') . " ====\n" . str_replace($rootPath . 'public/', '', $savePathCustom) . "\n\n", // FILE_APPEND // ); $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' => '', '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) { $data = [ 'prompt' => $prompt, // 'model' => 'dall-e-2', 'model' => 'gpt-image-1', 'n' => 1, 'size' => '1024x1024', 'quality' => 'standard', 'style' => 'vivid', 'response_format' => 'url' ]; 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}"); } }