- Complete working image generation app using Imagen 3 - PHP backend with Gemini API integration - Dark themed UI with prompt enhancement - Session management and logging system 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
155 lines
5.7 KiB
PHP
155 lines
5.7 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'config.php';
|
|
|
|
echo "<style>
|
|
body { font-family: monospace; background: #000; color: #0f0; padding: 20px; }
|
|
h2, h3 { color: #0ff; }
|
|
pre { background: #111; padding: 15px; border-left: 3px solid #0f0; overflow-x: auto; }
|
|
.error { color: #f00; }
|
|
.success { color: #0f0; }
|
|
.warning { color: #ff0; }
|
|
img { background: #fff; padding: 10px; }
|
|
</style>";
|
|
|
|
echo "<h2>🔍 Gemini API Diagnostic Tool</h2>";
|
|
|
|
// Test 1: Check API key
|
|
echo "<h3>Test 1: API Key Check</h3>";
|
|
$apiKey = GEMINI_API_KEY;
|
|
if (empty($apiKey)) {
|
|
echo "<p class='error'>❌ No API key configured!</p>";
|
|
exit;
|
|
} else {
|
|
echo "<p class='success'>✅ API Key: " . substr($apiKey, 0, 10) . "..." . substr($apiKey, -6) . "</p>";
|
|
}
|
|
|
|
// Test 2: Try to list available models
|
|
echo "<h3>Test 2: Check Available Models</h3>";
|
|
$listUrl = 'https://generativelanguage.googleapis.com/v1beta/models';
|
|
$ch = curl_init($listUrl . '?key=' . $apiKey);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
$modelsResponse = curl_exec($ch);
|
|
$modelsData = json_decode($modelsResponse, true);
|
|
curl_close($ch);
|
|
|
|
if (isset($modelsData['models'])) {
|
|
echo "<p class='success'>✅ API connection successful!</p>";
|
|
echo "<p>Available models with IMAGE generation:</p><ul>";
|
|
foreach ($modelsData['models'] as $model) {
|
|
if (isset($model['supportedGenerationMethods']) &&
|
|
in_array('generateContent', $model['supportedGenerationMethods'])) {
|
|
$modelName = $model['name'];
|
|
$displayName = $model['displayName'] ?? $modelName;
|
|
// Check if model supports image generation
|
|
if (stripos($modelName, 'imagen') !== false ||
|
|
stripos($displayName, 'image') !== false ||
|
|
stripos($modelName, 'gemini') !== false) {
|
|
echo "<li><strong>$modelName</strong> - $displayName</li>";
|
|
}
|
|
}
|
|
}
|
|
echo "</ul>";
|
|
} else {
|
|
echo "<p class='error'>❌ Could not fetch models. Response:</p>";
|
|
echo "<pre>" . json_encode($modelsData, JSON_PRETTY_PRINT) . "</pre>";
|
|
}
|
|
|
|
// Test 3: Try the image generation with the current model
|
|
echo "<h3>Test 3: Image Generation Test</h3>";
|
|
$model = 'gemini-3-pro-image-preview';
|
|
$url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent";
|
|
|
|
$payload = [
|
|
'contents' => [
|
|
[
|
|
'parts' => [
|
|
['text' => 'A simple red circle on white background']
|
|
]
|
|
]
|
|
],
|
|
'generationConfig' => [
|
|
'responseModalities' => ['IMAGE'],
|
|
'imageConfig' => [
|
|
'aspectRatio' => '1:1',
|
|
'imageSize' => '1K'
|
|
]
|
|
]
|
|
];
|
|
|
|
echo "<p><strong>Model:</strong> $model</p>";
|
|
echo "<p><strong>URL:</strong> $url</p>";
|
|
echo "<details><summary>Request Payload (click to expand)</summary>";
|
|
echo "<pre>" . json_encode($payload, JSON_PRETTY_PRINT) . "</pre>";
|
|
echo "</details>";
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'x-goog-api-key: ' . $apiKey
|
|
],
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($payload),
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_SSL_VERIFYPEER => true,
|
|
CURLOPT_TIMEOUT => 120
|
|
]);
|
|
|
|
echo "<p>⏳ Sending request...</p>";
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curlError = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
echo "<p><strong>HTTP Status Code:</strong> ";
|
|
if ($httpCode == 200) {
|
|
echo "<span class='success'>$httpCode ✅</span></p>";
|
|
} else {
|
|
echo "<span class='error'>$httpCode ❌</span></p>";
|
|
}
|
|
|
|
if ($curlError) {
|
|
echo "<p class='error'><strong>cURL Error:</strong> $curlError</p>";
|
|
}
|
|
|
|
$responseData = json_decode($response, true);
|
|
echo "<details><summary>Full Response (click to expand)</summary>";
|
|
echo "<pre>" . json_encode($responseData, JSON_PRETTY_PRINT) . "</pre>";
|
|
echo "</details>";
|
|
|
|
// Check for specific error messages
|
|
if (isset($responseData['error'])) {
|
|
echo "<h3 class='error'>❌ API Error Details:</h3>";
|
|
echo "<pre>" . json_encode($responseData['error'], JSON_PRETTY_PRINT) . "</pre>";
|
|
|
|
if (isset($responseData['error']['message'])) {
|
|
$errorMsg = $responseData['error']['message'];
|
|
echo "<p class='warning'><strong>Error Message:</strong> $errorMsg</p>";
|
|
|
|
// Provide helpful suggestions
|
|
if (stripos($errorMsg, 'not found') !== false || stripos($errorMsg, 'models/') !== false) {
|
|
echo "<p class='warning'>💡 Suggestion: The model name might be incorrect or deprecated.</p>";
|
|
} elseif (stripos($errorMsg, 'quota') !== false || stripos($errorMsg, 'limit') !== false) {
|
|
echo "<p class='warning'>💡 Suggestion: You may have exceeded your API quota.</p>";
|
|
} elseif (stripos($errorMsg, 'internal') !== false) {
|
|
echo "<p class='warning'>💡 Suggestion: This might be a temporary API issue or the model is having problems.</p>";
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check for image in response
|
|
if (isset($responseData['candidates'][0]['content']['parts'][0]['inlineData']['data'])) {
|
|
echo "<h3 class='success'>✅ SUCCESS! Image Generated!</h3>";
|
|
$imageData = $responseData['candidates'][0]['content']['parts'][0]['inlineData']['data'];
|
|
$mimeType = $responseData['candidates'][0]['content']['parts'][0]['inlineData']['mimeType'];
|
|
echo "<p><strong>MIME Type:</strong> $mimeType</p>";
|
|
echo "<p><strong>Data length:</strong> " . strlen($imageData) . " characters</p>";
|
|
echo "<h3>Generated Image:</h3>";
|
|
echo "<img src='data:$mimeType;base64,$imageData' style='max-width: 500px; border: 3px solid #0f0;'>";
|
|
} else {
|
|
echo "<h3 class='error'>❌ No Image Data Found</h3>";
|
|
}
|
|
|
|
echo "<hr><p><em>Debug completed at " . date('Y-m-d H:i:s') . "</em></p>";
|
|
?>
|