Skip to content

Commit b7332d7

Browse files
committed
first commit
0 parents  commit b7332d7

File tree

10 files changed

+759
-0
lines changed

10 files changed

+759
-0
lines changed

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Created with the help of https://gitignore.io
2+
3+
### Composer ###
4+
composer.phar
5+
/vendor/
6+
7+
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
8+
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
9+
composer.lock
10+
11+
### PHPCodeSniffer ###
12+
# CodeSniffer
13+
phpcs.xml
14+
15+
/vendor/*
16+
/wpcs/*
17+
18+
### JetBrains ###
19+
.idea
20+
21+
# File-based project format
22+
*.iws
23+
24+
# Editor-based Rest Client
25+
.idea/httpRequests
26+
27+
# Cache file creation bug
28+
# See https://youtrack.jetbrains.com/issue/JBR-2257
29+
.idea/$CACHE_FILE$
30+
31+
### PHPUnit ###
32+
# Covers PHPUnit
33+
# Reference: https://phpunit.de/
34+
35+
# Generated files
36+
.phpunit.result.cache
37+
.phpunit.cache
38+
39+
# PHPUnit
40+
/app/phpunit.xml
41+
/phpunit.xml
42+
43+
# Build data
44+
/build/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Hocine Saad - saadhocine0@gmail.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
![Stability PHP example code.](https://i.imgur.com/4AvgO09.png "Stability PHP example code.")
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.

composer.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "thehocinesaad/stability-php",
3+
"description": "Stability PHP is a comprehensive PHP API client designed for seamless interaction with the Stability AI API.",
4+
"keywords": [
5+
"stability-php",
6+
"ai",
7+
"stability",
8+
"php",
9+
"client",
10+
"stable diffusion"
11+
],
12+
"homepage": "https://github.com/thehocinesaad/stability-php",
13+
"license": "MIT",
14+
"type": "library",
15+
"authors": [
16+
{
17+
"name": "Hocine Saad",
18+
"email": "saadhocine0@gmail.com",
19+
"role": "Creator"
20+
}
21+
],
22+
"require": {
23+
"php": "^8.0",
24+
"guzzlehttp/guzzle": "^7.8"
25+
},
26+
"require-dev": {
27+
"laravel/pint": "^1.13",
28+
"phpstan/phpstan": "^1.10",
29+
"phpunit/phpunit": "^9.0",
30+
"symfony/var-dumper": "^6.3"
31+
},
32+
"autoload": {
33+
"psr-4": {
34+
"Stability\\": "src/"
35+
},
36+
"files": [
37+
"src/Stability.php"
38+
]
39+
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"TheHocineSaad\\StabilityPhp\\Tests\\": "tests"
43+
}
44+
},
45+
"scripts": {
46+
"test": "vendor/bin/phpunit",
47+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
48+
},
49+
"minimum-stability": "dev",
50+
"prefer-stable": true,
51+
"config": {
52+
"sort-packages": true
53+
}
54+
}

0 commit comments

Comments
 (0)