thehocinesaad / stability-php
Stability PHP is a comprehensive PHP API client designed for seamless interaction with the Stability AI API.
Installs: 1 476
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- laravel/pint: ^1.13
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^6.3
README
Stability PHP is a PHP API client offering developers an intuitive and efficient interface to interact seamlessly with the Stability AI API.
Table of Contents
Installation
You can install the package via composer:
composer require thehocinesaad/stability-php
After that, you can start using it:
$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.
You can add additional HTTP headers (which the API supports) to requests:
$client = Stability::client(YOUR_API_KEY') ->withAcceptHeader('image/png') ->withOrganizationHeader('org-123456') ->withStabilityClientIdHeader('my-great-plugin') ->withStabilityClientVersionHeader('1.2.1');
Usage
User
Manage your Stability.ai account, and view account/organization balances.
Account
Get information about the account associated with the provided API key.
$response = $client->user()->account(); $response['email']; // 'your@email.com' $response['id']; // 'user-xxxxxxxxxxxxxxx' // ...
Balance
Get the credit balance of the account/organization associated with the API key.
$response = $client->user()->balance(); $response['credits']; // 1000000 😍
Engines
Enumerate available engines.
List
List all engines available to your organization/user.
$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' }
Generations
Generate images from text, existing images, or both.
Text to Image
Generate a new image from a text prompt.
$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' }
Image to Image
Modify an image based on a text prompt.
$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' }
Image to Image Upscale
Create a higher resolution version of an input image.
$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'
Image to Image Masking
Selectively modify portions of an image using a mask.
$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' }
License
The MIT License (MIT). Please see License File for more information.