PHP code example of florinnichifiriuc / stable-diffusion-php-client

1. Go to this page and download the library: Download florinnichifiriuc/stable-diffusion-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

florinnichifiriuc / stable-diffusion-php-client example snippets




lorinnichifiriuc\StableDiffusionPhpClient\StableDiffusionClient;

$sdClient = new StableDiffusionClient();

$params = [
    'prompt' => 'A beautiful sunset over the mountains',
    'steps' => 30,
    'cfg_scale' => 7.5,
];

$images = $sdClient->txt2img($params);

if ($images) {
    file_put_contents('generated_image.png', base64_decode($images[0]));
}

$sdClient->setModel('v1-5-pruned-emaonly');



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Florinnichifiriuc\StableDiffusionPhpClient\StableDiffusionClient;
use Illuminate\Support\Facades\Storage;

class ImageGenerationController extends Controller
{
    public function generateImage(Request $request)
    {
        $request->validate([
            'prompt' => 't = new StableDiffusionClient();

        try {
            $images = $sdClient->txt2img($params);

            if ($images) {
                $decodedImage = base64_decode($images[0]);

                // Save the image to storage (e.g., public disk)
                $filename = 'generated_' . time() . '.png';
                Storage::disk('public')->put($filename, $decodedImage);

                return response()->json(['image_url' => Storage::url($filename)], 200);
            } else {
                return response()->json(['error' => 'Failed to generate image'], 500);
            }
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}
bash
   git clone https://github.com/florinnichifiriuc/stable-diffusion-php-client.git