$prompt_text, 'negative_prompt' => $negative_prompt_text ); file_put_contents($saved_prompts_file, json_encode($prompts, JSON_PRETTY_PRINT)); } $saved_prompts = loadSavedPrompts(); // List of available samplers $samplers = array( "Euler", "Euler a", "Heun", "DPM2", "DPM2 a", "DPM++ 2S a", "DPM++ 2M", "DPM++ SDE", "DPM++ 2M SDE", "DPM++ 3M SDE", "DPM fast", "DPM adaptive", "LMS", "DPM++ SDE Karras", "DPM++ 2M SDE Karras", "DPM++ 3M SDE Karras", "DDIM", "PLMS" ); if ($_SERVER["REQUEST_METHOD"] == "POST") { // All requests to the server to save or execute prompts will be POST requests to itself // Handle prompt saving if (isset($_POST['save_prompt']) && isset($_POST['prompt_name'])) { $prompt_to_save = trim($_POST['prompt']); $negative_prompt_to_save = trim($_POST['negative_prompt']); $prompt_name = trim($_POST['prompt_name']); if (!empty($prompt_to_save) && !empty($prompt_name)) { savePrompt($prompt_to_save, $negative_prompt_to_save, $prompt_name); $success_message = "Prompt saved successfully!"; $saved_prompts = loadSavedPrompts(); // Reload prompts } else { $error_message = "Both prompt and prompt name are required to save."; } } // Handle image generation else { $prompt = isset($_POST['prompt']) ? $_POST['prompt'] : ''; $negative_prompt = isset($_POST['negative_prompt']) ? $_POST['negative_prompt'] : ''; $width = isset($_POST['width']) ? (int)$_POST['width'] : 512; $height = isset($_POST['height']) ? (int)$_POST['height'] : 512; $steps = isset($_POST['steps']) ? (int)$_POST['steps'] : 20; $cfg_scale = isset($_POST['cfg_scale']) ? (float)$_POST['cfg_scale'] : 7.5; $sampler = isset($_POST['sampler']) ? $_POST['sampler'] : 'Euler'; // Check for angle brackets in the prompt if (strpos($prompt, '<') !== false || strpos($prompt, '>') !== false) { $error_message = "Error: Angle brackets are not allowed in the prompt. Please use square brackets [lora-name:weight] for LORAs."; } // Only proceed if there's no error elseif (!empty($prompt)) { // Process the prompt to handle LORA syntax $processed_prompt = preg_replace('/\[([\w\s\-]+?):([\d\.]+)\]/', '', $prompt); $data = array( 'prompt' => $processed_prompt, 'negative_prompt' => $negative_prompt, 'steps' => $steps, 'width' => $width, 'height' => $height, 'cfg_scale' => $cfg_scale, 'sampler_name' => $sampler, 'override_settings' => new stdClass(), 'override_settings_restore_afterwards' => true ); // Replace 127.0.0.1 with your Stable diffusion server's IP if running externally $ch = curl_init('http://127.0.0.1:7860/sdapi/v1/txt2img'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $result = curl_exec($ch); if (curl_errno($ch)) { $response = 'Error: ' . curl_error($ch); } else { $decoded = json_decode($result, true); if (isset($decoded['images'][0])) { $image_data = base64_decode($decoded['images'][0]); // Save original PNG temporarily $temp_png = 'temp_' . time() . '_' . bin2hex(random_bytes(8)) . '.png'; file_put_contents($temp_png, $image_data); // Create output JPG filename $generated_image = 'generated_' . time() . '_' . bin2hex(random_bytes(8)) . '.jpg'; // Build ImageMagick convert command with optimization $quality = 85; // Balanced quality setting $cmd = "convert \"$temp_png\" -strip -interlace Plane -gaussian-blur 0.05 -quality $quality "; $cmd .= "-sampling-factor 4:2:0 \"$generated_image\" 2>&1"; // Execute conversion $output = []; $returnVar = 0; exec($cmd, $output, $returnVar); if ($returnVar === 0 && file_exists($generated_image)) { // Get file sizes for comparison $originalSize = filesize($temp_png); $convertedSize = filesize($generated_image); // Clean up temporary PNG file if (file_exists($temp_png)) { unlink($temp_png); } // Clean up old generated files foreach (glob("generated_*.png") as $file) { if ($file != $generated_image && (time() - filemtime($file)) > 3600) { unlink($file); } } // Store size information for display $size_info = array( 'original' => round($originalSize / 1024, 2), 'converted' => round($convertedSize / 1024, 2), 'reduction' => round((($originalSize - $convertedSize) / $originalSize) * 100, 2) ); } else { // If conversion fails, keep the original PNG if (file_exists($temp_png)) { $generated_image = $temp_png; } $error_message = "Image conversion failed. Using original PNG format."; } } } curl_close($ch); } } } else if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['load_prompt'])) { // For loading prompts we use GET method to send the client's choice of prompt to load // Load saved prompt into textarea $saved_prompts = loadSavedPrompts(); if (isset($saved_prompts[$_GET['load_prompt']])) { // Check if the saved prompt is in the new format (array) or old format (string) if (is_array($saved_prompts[$_GET['load_prompt']])) { $prompt = $saved_prompts[$_GET['load_prompt']]['prompt']; $negative_prompt = $saved_prompts[$_GET['load_prompt']]['negative_prompt']; } else { // Handle old format $prompt = $saved_prompts[$_GET['load_prompt']]; $negative_prompt = ''; } } } ?> Stable Diffusion Image Generator

Stable Diffusion Image Generator

How to use LORAs:

Include LORAs in your prompt using this syntax: [lora-name:weight]

Example: a beautiful landscape by [my-artist-lora:0.8]

Weight values typically range from 0.1 to 1.0

Note: Angle brackets (< >) are not allowed in prompts. Use square brackets instead.

Saved Prompts

">
">


Save Current Prompt

">

Generated Image:

Generated image
Download Generated Image

Original size: KB
Converted size: KB
Size reduction: %

Parameters used:
Prompt:
Negative Prompt:
Processed Prompt:
Size: x
Steps:
CFG Scale:
Sampler:

Error: