|
| 1 | + |
| 2 | + |
| 3 | +Stability PHP is a PHP API client offering developers an intuitive and efficient interface to interact seamlessly with the [Stability AI API](https://platform.stability.ai/docs/api-reference "Stability AI API"). |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | +- [ **Installation**](#installation) |
| 7 | +- [**Usage**](#usage) |
| 8 | + - [User](#user) |
| 9 | + - [Account](#account) |
| 10 | + - [Balance](#balance) |
| 11 | + - [Engines](#engines) |
| 12 | + - [List](#list) |
| 13 | + - [Generations](#generations) |
| 14 | + - [Text to Image](#text-to-image) |
| 15 | + - [Image to Image](#image-to-image) |
| 16 | + - [Image to Image Upscale](#image-to-image-upscale) |
| 17 | + - [Image to Image Masking](#image-to-image-masking) |
| 18 | +- [**License**](#license) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +You can install the package via composer: |
| 23 | + |
| 24 | +```bash |
| 25 | +composer require thehocinesaad/stability-php |
| 26 | +``` |
| 27 | +After that, you can start using it: |
| 28 | + |
| 29 | +```php |
| 30 | +$client = Stability::client('YOUR_API_KEY'); |
| 31 | + |
| 32 | +$response = $client->generations()->textToImage('stable-diffusion-xl-1024-v1-0', [ |
| 33 | + 'text_prompts' => [ |
| 34 | + [ |
| 35 | + 'text' => 'A lighthouse on a cliff', |
| 36 | + 'weight' => 0.5 |
| 37 | + ], |
| 38 | + ], |
| 39 | + 'samples' => 1, |
| 40 | +]); |
| 41 | + |
| 42 | +dd($response['artifacts'][0]['base64']); |
| 43 | +// "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAM8MmNhQ..." - Image encoded in base64. |
| 44 | +``` |
| 45 | +You can add additional HTTP headers (which the API supports) to requests: |
| 46 | + |
| 47 | +```php |
| 48 | +$client = Stability::client(YOUR_API_KEY') |
| 49 | + ->withAcceptHeader('image/png') |
| 50 | + ->withOrganizationHeader('org-123456') |
| 51 | + ->withStabilityClientIdHeader('my-great-plugin') |
| 52 | + ->withStabilityClientVersionHeader('1.2.1'); |
| 53 | +``` |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +### User |
| 58 | + |
| 59 | +Manage your Stability.ai account, and view account/organization balances. |
| 60 | + |
| 61 | +#### **`Account`** |
| 62 | +Get information about the account associated with the provided API key. |
| 63 | +* See: https://platform.stability.ai/docs/api-reference#tag/v1user/operation/userAccount |
| 64 | + |
| 65 | +```php |
| 66 | +$response = $client->user()->account(); |
| 67 | + |
| 68 | +$response['email']; // 'your@email.com' |
| 69 | +$response['id']; // 'user-xxxxxxxxxxxxxxx' |
| 70 | +// ... |
| 71 | +``` |
| 72 | + |
| 73 | +------------ |
| 74 | + |
| 75 | +#### **`Balance`** |
| 76 | +Get the credit balance of the account/organization associated with the API key. |
| 77 | +* See: https://platform.stability.ai/docs/api-reference#tag/v1user/operation/userBalance |
| 78 | + |
| 79 | +```php |
| 80 | +$response = $client->user()->balance(); |
| 81 | + |
| 82 | +$response['credits']; // 1000000 😍 |
| 83 | +``` |
| 84 | + |
| 85 | +### Engines |
| 86 | + |
| 87 | +Enumerate available engines. |
| 88 | + |
| 89 | +#### **`List`** |
| 90 | +List all engines available to your organization/user. |
| 91 | +* See: https://platform.stability.ai/docs/api-reference#tag/v1engines/operation/listEngines |
| 92 | + |
| 93 | +```php |
| 94 | +$response = $client->engines()->list(); |
| 95 | +// Returns an array of available engines. |
| 96 | + |
| 97 | +foreach ($response as $engine) { |
| 98 | + $engine['description']; // 'Stability-AI Stable Diffusion XL v1.0' |
| 99 | + $engine['id']; // 'stable-diffusion-xl-1024-v1-0' |
| 100 | + $engine['name']; // 'Stable Diffusion XL v1.0' |
| 101 | + $engine['type']; // 'PICTURE' |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Generations |
| 106 | + |
| 107 | +Generate images from text, existing images, or both. |
| 108 | + |
| 109 | +#### **`Text to Image`** |
| 110 | +Generate a new image from a text prompt. |
| 111 | +* See: https://platform.stability.ai/docs/api-reference#tag/v1generation/operation/textToImage |
| 112 | + |
| 113 | +```php |
| 114 | +$response = $client->generations()->textToImage('stable-diffusion-xl-1024-v1-0', [ |
| 115 | + 'text_prompts' => [ |
| 116 | + [ |
| 117 | + 'text' => 'A lighthouse on a cliff', |
| 118 | + ], |
| 119 | + ], |
| 120 | + 'cfg_scale' => 7, |
| 121 | + 'height' => 1024, |
| 122 | + 'width' => 1024, |
| 123 | + 'steps' => 30, |
| 124 | + 'samples' => 2, |
| 125 | +]); |
| 126 | + |
| 127 | +foreach ($response['artifacts'] as $result) { |
| 128 | + $result['base64']; // 'iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAA...' |
| 129 | + $result['seed']; // 6311659811 |
| 130 | + $result['finishReason']; // 'SUCCESS' |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +------------ |
| 135 | + |
| 136 | +#### **`Image to Image`** |
| 137 | +Modify an image based on a text prompt. |
| 138 | +* See: https://platform.stability.ai/docs/api-reference#tag/v1generation/operation/imageToImage |
| 139 | + |
| 140 | +```php |
| 141 | +$response = $client->generations()->imageToImage(stable-diffusion-xl-1024-v1-0', [ |
| 142 | + 'init_image' => 'init_image.png', |
| 143 | + 'init_image_mode' => 'IMAGE_STRENGTH', |
| 144 | + 'image_strength' => '0.35', |
| 145 | + 'text_prompts' => [ |
| 146 | + [ |
| 147 | + 'text' => 'A lighthouse on a cliff', |
| 148 | + ], |
| 149 | + ], |
| 150 | + 'cfg_scale' => 7, |
| 151 | + 'steps' => 10, |
| 152 | + 'samples' => 2, |
| 153 | +]); |
| 154 | + |
| 155 | +foreach ($response['artifacts'] as $result) { |
| 156 | + $result['base64']; // 'iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA...' |
| 157 | + $result['seed']; // 4285698893 |
| 158 | + $result['finishReason']; // 'SUCCESS' |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +------------ |
| 163 | + |
| 164 | +#### **`Image to Image Upscale`** |
| 165 | +Create a higher resolution version of an input image. |
| 166 | +* See: https://platform.stability.ai/docs/api-reference#tag/v1generation/operation/upscaleImage |
| 167 | + |
| 168 | +```php |
| 169 | +$response = $client->generations()->imageToImageUpscale('esrgan-v1-x2plus', [ |
| 170 | + 'image' => 'image.png', |
| 171 | + 'width' => '1024', |
| 172 | +]); |
| 173 | + |
| 174 | +$response['artifacts'][0]['base64']; // 'iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAD...' |
| 175 | +$response['artifacts'][0]['seed']; // 0 |
| 176 | +$response['artifacts'][0]['finishReason']; // 'SUCCESS' |
| 177 | +``` |
| 178 | + |
| 179 | +------------ |
| 180 | + |
| 181 | +#### **`Image to Image Masking`** |
| 182 | +Selectively modify portions of an image using a mask. |
| 183 | +* See: https://platform.stability.ai/docs/api-reference#tag/v1generation/operation/masking |
| 184 | + |
| 185 | +```php |
| 186 | +$response = $client->generations()->imageToImageMasking('stable-inpainting-512-v2-0', [ |
| 187 | + 'mask_source' => 'MASK_IMAGE_BLACK', |
| 188 | + 'init_image' => 'init_image.png', |
| 189 | + 'mask_image' => 'mask_image.png', |
| 190 | + 'text_prompts' => [ |
| 191 | + [ |
| 192 | + 'text' => 'A lighthouse on a cliff', |
| 193 | + ], |
| 194 | + ], |
| 195 | + 'cfg_scale' => 7, |
| 196 | + 'clip_guidance_preset' => 'FAST_BLUE', |
| 197 | + 'steps' => 10, |
| 198 | + 'samples' => 2, |
| 199 | +]); |
| 200 | + |
| 201 | +foreach ($response['artifacts'] as $result) { |
| 202 | + $result['base64']; // 'iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7...' |
| 203 | + $result['seed']; // 96898585 |
| 204 | + $result['finishReason']; // 'SUCCESS' |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +## License |
| 209 | + |
| 210 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments