162 lines
5.8 KiB
PHP
Executable File
162 lines
5.8 KiB
PHP
Executable File
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$prompt = $input['prompt'] ?? '';
|
|
$provider = $input['provider'] ?? 'pollinations'; // gratuit!
|
|
$size = $input['size'] ?? '1024x1024';
|
|
|
|
if (empty($prompt)) {
|
|
echo json_encode(['error' => 'Prompt requis']);
|
|
exit;
|
|
}
|
|
|
|
$result = ['success' => false];
|
|
|
|
switch ($provider) {
|
|
case 'pollinations':
|
|
// API 100% gratuite, sans clé!
|
|
$encodedPrompt = urlencode($prompt);
|
|
$imageUrl = "https://image.pollinations.ai/prompt/{$encodedPrompt}?width=1024&height=1024&nologo=true";
|
|
|
|
// Télécharger l'image
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $imageUrl,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_TIMEOUT => 60,
|
|
CURLOPT_USERAGENT => 'WEVAL MIND-IA/2.0'
|
|
]);
|
|
$imageData = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode == 200 && $imageData) {
|
|
// Sauvegarder localement
|
|
$filename = date("Ymd_His") . "_" . substr(md5($prompt), 0, 8) . ".png";
|
|
$filepath = "/opt/wevads/public/hamid-files/" . $filename;
|
|
file_put_contents($filepath, $imageData);
|
|
|
|
$result = [
|
|
'success' => true,
|
|
'provider' => 'pollinations',
|
|
'prompt' => $prompt,
|
|
'url' => '/hamid-files/' . $filename,
|
|
'external_url' => $imageUrl,
|
|
'size' => strlen($imageData)
|
|
];
|
|
} else {
|
|
$result['error'] = "Erreur génération image (HTTP $httpCode)";
|
|
}
|
|
break;
|
|
|
|
case 'stability':
|
|
// Stability AI (nécessite clé API)
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
|
|
$stmt = $pdo->query("SELECT config_value FROM admin.hamid_config WHERE config_key = 'stability_api_key'");
|
|
$apiKey = $stmt->fetchColumn();
|
|
|
|
if (empty($apiKey)) {
|
|
$result['error'] = "Clé API Stability non configurée";
|
|
break;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => 'https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image',
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'Authorization: Bearer ' . $apiKey
|
|
],
|
|
CURLOPT_POSTFIELDS => json_encode([
|
|
'text_prompts' => [['text' => $prompt]],
|
|
'cfg_scale' => 7,
|
|
'width' => 1024,
|
|
'height' => 1024,
|
|
'samples' => 1
|
|
]),
|
|
CURLOPT_TIMEOUT => 60
|
|
]);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$data = json_decode($response, true);
|
|
if (isset($data['artifacts'][0]['base64'])) {
|
|
$imageData = base64_decode($data['artifacts'][0]['base64']);
|
|
$filename = date("Ymd_His") . "_stability_" . substr(md5($prompt), 0, 8) . ".png";
|
|
$filepath = "/opt/wevads/public/hamid-files/" . $filename;
|
|
file_put_contents($filepath, $imageData);
|
|
|
|
$result = [
|
|
'success' => true,
|
|
'provider' => 'stability',
|
|
'prompt' => $prompt,
|
|
'url' => '/hamid-files/' . $filename,
|
|
'size' => strlen($imageData)
|
|
];
|
|
} else {
|
|
$result['error'] = $data['message'] ?? 'Erreur Stability AI';
|
|
}
|
|
break;
|
|
|
|
case 'together':
|
|
// Together AI (FLUX gratuit)
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
|
|
$stmt = $pdo->query("SELECT config_value FROM admin.hamid_config WHERE config_key = 'together_api_key'");
|
|
$apiKey = $stmt->fetchColumn();
|
|
|
|
if (empty($apiKey)) {
|
|
// Fallback à pollinations
|
|
$result['error'] = "Together API non configuré, utilisez pollinations";
|
|
break;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => 'https://api.together.xyz/v1/images/generations',
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'Authorization: Bearer ' . $apiKey
|
|
],
|
|
CURLOPT_POSTFIELDS => json_encode([
|
|
'model' => 'black-forest-labs/FLUX.1-schnell-Free',
|
|
'prompt' => $prompt,
|
|
'width' => 1024,
|
|
'height' => 1024,
|
|
'n' => 1
|
|
]),
|
|
CURLOPT_TIMEOUT => 60
|
|
]);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$data = json_decode($response, true);
|
|
if (isset($data['data'][0]['url'])) {
|
|
$imageUrl = $data['data'][0]['url'];
|
|
// Télécharger
|
|
$imageData = file_get_contents($imageUrl);
|
|
$filename = date("Ymd_His") . "_flux_" . substr(md5($prompt), 0, 8) . ".png";
|
|
$filepath = "/opt/wevads/public/hamid-files/" . $filename;
|
|
file_put_contents($filepath, $imageData);
|
|
|
|
$result = [
|
|
'success' => true,
|
|
'provider' => 'together/FLUX',
|
|
'prompt' => $prompt,
|
|
'url' => '/hamid-files/' . $filename,
|
|
'size' => strlen($imageData)
|
|
];
|
|
} else {
|
|
$result['error'] = $data['error']['message'] ?? 'Erreur Together AI';
|
|
}
|
|
break;
|
|
}
|
|
|
|
echo json_encode($result);
|
|
|