File: /home/panomity.de/panomity.com/images/image_generator.php
<?php
// Check if the prompt is set in the POST request
if (isset($_POST['prompt'])) {
// Define the URL of the image generation API
$url = "http://plano.panomity.com:7878/sdapi/v1/txt2img";
// Prepare the payload with the prompt received from the AJAX request
$payload = array(
"prompt" => $_POST['prompt'], // Use the user's prompt
"steps" => 25 // Number of steps for image generation, adjust as necessary
);
// Initialize a cURL session
$ch = curl_init($url);
// Specify that we want to send a POST request
curl_setopt($ch, CURLOPT_POST, true);
// Attach our encoded JSON payload
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
// Return the transfer as a string of the return value of curl_exec() instead of outputting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Execute the POST request
$response = curl_exec($ch);
// Close the cURL session
curl_close($ch);
// Decode the JSON response
$responseData = json_decode($response, true);
// Check if 'images' key exists and has at least one image
if(isset($responseData['images'][0])) {
// Generate a unique filename for the image
$uniqueFilename = 'image_' . time() . '_' . rand(1000, 9999) . '.png';
// Decode the first image from base64 and save it
$imageData = base64_decode($responseData['images'][0]);
// Specify the path to save the image
$imagePath = "generated_images/".$uniqueFilename; // Make sure this directory exists and is writable
file_put_contents($imagePath, $imageData);
// Return the path to the generated image
echo $imagePath;
} else {
echo "No images returned in the response.";
}
} else {
// If no prompt is provided, return an error message
echo "Error: No prompt provided.";
}
?>