commit 44930ccf78db12a27805c8319d38272f1430d56d Author: markmental Date: Sun Dec 15 18:08:27 2024 -0500 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c1a553a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.jpg +*.json +*.sh +*.bak diff --git a/README.md b/README.md new file mode 100644 index 0000000..02315cb --- /dev/null +++ b/README.md @@ -0,0 +1,87 @@ +# Legacy Stable Diffusion WebUI + +A minimalistic, JavaScript-free web interface and processing server for Stable Diffusion, designed specifically for being accessed by legacy systems and browsers. This interface relies on the API from AUTOMATIC1111's Stable Diffusion WebUI. + +## Features + +Two separate interfaces are provided: +- `index.php`: HTML 4.01+ compliant interface +- `legacy.php`: HTML 3.2 compliant interface for extremely old systems + +Common features across both versions: +- No JavaScript dependencies +- Lightweight and fast loading +- Support for LORA models using square bracket syntax +- Image optimization with automatic PNG to JPG conversion +- Prompt saving and loading functionality +- Full configurability of key generation parameters: + - Image dimensions + - Sampling steps + - CFG Scale + - Choice of sampling methods + +## Browser Compatibility + +- `index.php`: Tested working on IE4+ and comparable browsers +- `legacy.php`: Tested working on IE3 and comparable browsers +- Both versions work on modern browsers + +## Requirements + +- PHP 8+ (Tested on frankenphp running on Debian 12, should also run with traditional PHP setups with apache/nginx) +- ImageMagick for image optimization +- Running instance of AUTOMATIC1111's Stable Diffusion WebUI with API access enabled +- Web server (Apache, nginx, etc.) + +## Installation + +1. Ensure you have AUTOMATIC1111's Stable Diffusion WebUI running with the `--api` flag +2. Install PHP and ImageMagick on your system +3. Copy both `index.php` and `legacy.php` to your web server directory +4. Ensure the directory is writable by the web server for saved prompts and generated images +5. Access through your web browser: + - Use `index.php` for HTML 4.01+ compatible browsers + - Use `legacy.php` for HTML 3.2 compatible browsers + +## Usage + +1. Enter your prompt in the text area +2. Configure generation parameters as needed +3. Click "Generate Image" to create your image +4. Save frequently used prompts server-side using the "Save Prompt" feature +5. Load saved prompts from the dropdown menu + +## LORA Usage + +To use LORA models, include them in your prompt using square brackets: +```[lora-name:weight]``` + +Example: +```a beautiful landscape by [my-artist-lora:0.8]``` + +## Notes + +- Images are automatically optimized and converted from PNG to JPG for faster loading +- The interface works without any client-side scripting +- All processing is done server-side +- Compatible with browsers from the late 1990s to now +- The HTML 3.2 version (`legacy.php`) provides basic functionality for extremely old mid 1990s browsers + +## Why Two Versions? + +- `index.php` provides a more refined interface with better styling and layout options, suitable for browsers supporting HTML 4.01+ +- `legacy.php` strips down the interface to basic HTML 3.2 for maximum compatibility with vintage systems and browsers +- Both achieve the same core functionality with different levels of visual polish + +## Contributing + +Contributions are welcome! Please feel free to submit a Pull Request or fork. + +## License + +This project is MIT licensed. + +## Credits + +Built to work with AUTOMATIC1111's Stable Diffusion WebUI API: +https://github.com/AUTOMATIC1111/stable-diffusion-webui diff --git a/index.php b/index.php new file mode 100644 index 0000000..b45b9e4 --- /dev/null +++ b/index.php @@ -0,0 +1,361 @@ +') !== 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, + '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'])) { + // Load saved prompt into textarea + $saved_prompts = loadSavedPrompts(); + $prompt = isset($saved_prompts[$_GET['load_prompt']]) ? $saved_prompts[$_GET['load_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:
+ Processed Prompt:
+ Size: x
+ Steps:
+ CFG Scale:
+ Sampler: +
+
+ + + +
+

Error:

+

+
+ + + + diff --git a/legacy.php b/legacy.php new file mode 100644 index 0000000..d6e5eda --- /dev/null +++ b/legacy.php @@ -0,0 +1,339 @@ +') !== 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, + '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'])) { + // Load saved prompt into textarea + $saved_prompts = loadSavedPrompts(); + $prompt = isset($saved_prompts[$_GET['load_prompt']]) ? $saved_prompts[$_GET['load_prompt']] : ''; +} +?> + + + +Stable Diffusion Image Generator + + + +

Stable Diffusion Image Generator

+ + + + + + +
+
+ + + + + + + +
+
+ + + + + + +
+

How to use LORAs:

+

Include LORAs using: [lora-name:weight]
+ Example: a beautiful landscape by [my-artist-lora:0.8]
+ Weight values: 0.1 to 1.0
+ Note: Do not use angle brackets (< >). Use square brackets instead.

+
+ +
+ + + + + +
+ Saved Prompts
+
"> + + +
+
+ +
+ +
"> + + + + + + + + + + + + + + + + + + +
Enter your prompt:
+
Width:
+
Height:
+
Steps:
+
CFG Scale:
+
Sampler:
+
+ +
+
+ +
+ + + + + +
+
"> + Save Current Prompt
+ Prompt Name:
+ + +
+
+ + +
+ + + + +
+

Generated Image:

+ Generated image +
+ Download Generated Image + + +

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

+ + +

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

+
+ + + +
+ + + + +
+

Error:

+

+
+ + + + +