PHP code example of thehocinesaad / stability-php

1. Go to this page and download the library: Download thehocinesaad/stability-php 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/ */

    

thehocinesaad / stability-php example snippets


$client = Stability::client('YOUR_API_KEY');

$response = $client->generations()->textToImage('stable-diffusion-xl-1024-v1-0', [
    'text_prompts' => [
        [
            'text' => 'A lighthouse on a cliff',
            'weight' => 0.5
        ],
    ],
    'samples' => 1,
]);

dd($response['artifacts'][0]['base64']);
// "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAM8MmNhQ..." - Image encoded in base64.

$client = Stability::client(YOUR_API_KEY')
    ->withAcceptHeader('image/png')
    ->withOrganizationHeader('org-123456')
    ->withStabilityClientIdHeader('my-great-plugin')
    ->withStabilityClientVersionHeader('1.2.1');

$response = $client->user()->account();

$response['email']; // '[email protected]'
$response['id']; // 'user-xxxxxxxxxxxxxxx'
// ...

$response = $client->user()->balance();

$response['credits']; // 1000000 😍

$response = $client->engines()->list();
// Returns an array of available engines.

foreach ($response as $engine) {
    $engine['description']; // 'Stability-AI Stable Diffusion XL v1.0'
    $engine['id']; // 'stable-diffusion-xl-1024-v1-0'
    $engine['name']; // 'Stable Diffusion XL v1.0'
    $engine['type']; // 'PICTURE'
}

$response = $client->generations()->textToImage('stable-diffusion-xl-1024-v1-0', [
    'text_prompts' => [
        [
            'text' => 'A lighthouse on a cliff',
        ],
    ],
    'cfg_scale' => 7,
    'height' => 1024,
    'width' => 1024,
    'steps' => 30,
    'samples' => 2,
]);

foreach ($response['artifacts'] as $result) {
    $result['base64']; // 'iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAA...'
    $result['seed']; // 6311659811
    $result['finishReason']; // 'SUCCESS'
}

$response = $client->generations()->imageToImage(stable-diffusion-xl-1024-v1-0', [
    'init_image' => 'init_image.png',
    'init_image_mode' => 'IMAGE_STRENGTH',
    'image_strength' => '0.35',
    'text_prompts' => [
        [
            'text' => 'A lighthouse on a cliff',
        ],
    ],
    'cfg_scale' => 7,
    'steps' => 10,
    'samples' => 2,
]);

foreach ($response['artifacts'] as $result) {
    $result['base64']; // 'iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA...'
    $result['seed']; // 4285698893
    $result['finishReason']; // 'SUCCESS'
}

$response = $client->generations()->imageToImageUpscale('esrgan-v1-x2plus', [
    'image' => 'image.png',
    'width' => '1024',
]);

$response['artifacts'][0]['base64']; // 'iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAD...'
$response['artifacts'][0]['seed']; // 0
$response['artifacts'][0]['finishReason']; // 'SUCCESS'

$response = $client->generations()->imageToImageMasking('stable-inpainting-512-v2-0', [
    'mask_source' => 'MASK_IMAGE_BLACK',
    'init_image' => 'init_image.png',
    'mask_image' => 'mask_image.png',
    'text_prompts' => [
        [
            'text' => 'A lighthouse on a cliff',
        ],
    ],
    'cfg_scale' => 7,
    'clip_guidance_preset' => 'FAST_BLUE',
    'steps' => 10,
    'samples' => 2,
]);

foreach ($response['artifacts'] as $result) {
    $result['base64']; // 'iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7...'
    $result['seed']; // 96898585
    $result['finishReason']; // 'SUCCESS'
}
bash
composer