[ 'api_key' => 'sk-scyWT2YPrPOIzralcb6WzCeFdAZvl91rh1JcuuaNMrhEJNDZ', 'api_url' => 'https://niubi.zeabur.app/v1/chat/completions' ], //文生文 'txttotxt' => [ 'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73', 'api_url' => 'https://one.opengptgod.com/v1/chat/completions' ], //文生图 'txttoimg' => [ 'api_key' => 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY', 'api_url' => 'https://niubi.zeabur.app/v1/images/generations' ], // //文生文-超结损分析 // 'chaojiesun' => [ // 'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73', // 'api_url' => 'https://one.opengptgod.com/v1/chat/completions' // ] ]; /** * 图生文 * @param string $imageUrl 图像 URL,支持公网可访问地址 * @param string $prompt 对图像的提问内容或提示文本 */ public function callGptApi($imageUrl, $prompt) { $data = [ "model" => "gemini-2.5-pro-preview-05-06", "messages" => [[ "role" => "user", "content" => [ ["type" => "text", "text" => $prompt], ["type" => "image_url", "image_url" => [ "url" => $imageUrl, "detail" => "auto" ]] ] ]], "max_tokens" => 1000 ]; return $this->callApi($this->config['imgtotxt']['api_url'], $this->config['imgtotxt']['api_key'], $data); } /** * 图生文-方法二 * @param string $imageUrl 图像 URL,支持公网可访问地址 * @param string $prompt 对图像的提问内容或提示文本 */ // public function callGptApi($imageUrl, $prompt) // { // $data = [ // "model" => "gpt-4-vision-preview", // "messages" => [[ // "role" => "user", // "content" => [ // ["type" => "text", "text" => $prompt], // ["type" => "image_url", "image_url" => [ // "url" => $imageUrl, // "detail" => "auto" // ]] // ] // ]], // "max_tokens" => 1000 // ]; // return $this->callApi($this->config['imgtotxt']['api_url'], $this->config['imgtotxt']['api_key'], $data); // } /** * 文生文 * @param string $prompt 用户输入的文本提示内容 */ public function txtGptApi($prompt) { $data = [ 'prompt' => $prompt, 'model' => 'gpt-4', 'session_id' => null, 'context_reset' => true ]; return $this->callApi( $this->config['txttotxt']['api_url'], $this->config['txttotxt']['api_key'], $data ); } /** * 文生文-超结损分析 * @param string $prompt 用户输入的文本提示内容 */ // public function chaojiesunGptApi($prompt) // { // $data = [ // 'prompt' => $prompt, // 'model' => 'gpt-4', // 'session_id' => null, // 'context_reset' => true // ]; // return $this->callApi( // $this->config['chaojiesun']['api_url'], // $this->config['chaojiesun']['api_key'], // $data // ); // } /** * 文生图 * @param string $prompt 提示文本,用于指导图像生成 * @param string $selectedOption 模型名称,例如 'dall-e-3' 或其他兼容模型 */ 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['txttoimg']['api_url'], $this->config['txttoimg']['api_key'], $data); } public function imgtoimgGptApi($prompt, $new_image_url) { $imgPath = ROOT_PATH . 'public/' . $new_image_url; if (!file_exists($imgPath)) { return ['code' => 1, 'msg' => '原图不存在:' . $new_image_url]; } // 原图 base64 编码 $imgData = file_get_contents($imgPath); $base64Img = base64_encode($imgData); $initImage = 'data:image/png;base64,' . $base64Img; // 构建 POST 请求参数 $postData = json_encode([ 'prompt' => $prompt, 'sampler_name' => 'DPM++ 2M SDE Heun', 'seed' => -1, 'steps' => 20, 'cfg_scale' => 7, 'denoising_strength' => 0.6, 'width' => 1024, 'height' => 2048, 'resize_mode' => 0, 'inpaint_full_res' => true, 'inpainting_fill' => 1, 'init_images' => [$initImage], 'override_settings' => [ 'sd_model_checkpoint' => 'realisticVisionV51_v51VAE-inpainting.safetensors [f0d4872d24]', 'sd_vae' => 'anything-v4.5.vae.pt' ] ]); $apiUrl = "http://20.0.17.233:45001/sdapi/v1/img2img"; $headers = ['Content-Type: application/json']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_TIMEOUT, 90); $response = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if ($error) { return ['code' => 1, 'msg' => '请求失败:' . $error]; } $data = json_decode($response, true); if (!isset($data['images'][0])) { return ['code' => 1, 'msg' => 'API未返回图像数据']; } return [ 'code' => 0, 'msg' => '图像生成成功', 'data' => [ 'url' => $data['images'][0] ] ]; } /** * 通用 API 调用方法(支持重试机制) * * @param string $url 接口地址 * @param string $apiKey 授权密钥(Bearer Token) * @param array $data 请求数据(JSON 格式) * * 功能说明: * - 使用 cURL 发送 POST 请求到指定 API 接口 * - 设置请求头和超时时间等参数 * - 支持最多重试 2 次,当接口调用失败时自动重试 * - 返回成功时解析 JSON 响应为数组 * * 异常处理: * - 若全部重试失败,将抛出异常并包含最后一次错误信息 * * @return array 接口响应数据(成功时返回解析后的数组) * @throws \Exception 接口请求失败时抛出异常 */ 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}"); } }