https://github.com/kdosiodjinud/ai-generate-image-by-reference
A PHP library to generate images in the style of a reference image using the OpenAI Image API.
https://github.com/kdosiodjinud/ai-generate-image-by-reference
ai generator image-processing images openai reference
Last synced: 5 months ago
JSON representation
A PHP library to generate images in the style of a reference image using the OpenAI Image API.
- Host: GitHub
- URL: https://github.com/kdosiodjinud/ai-generate-image-by-reference
- Owner: kdosiodjinud
- Created: 2025-06-11T04:35:32.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-20T21:16:22.000Z (about 1 year ago)
- Last Synced: 2025-10-26T20:51:01.288Z (8 months ago)
- Topics: ai, generator, image-processing, images, openai, reference
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐ง ๐จ AiGenerateImageByReference
A PHP library to generate images in the style of a reference image using the OpenAI Image API.
---
## โ๏ธ Installation
```bash
composer require kdosiodjinud/ai-generate-image-by-reference
```
> Requires PHP 8.1+, `guzzlehttp/guzzle`, and `psr/log`.
---
## ๐ Usage
```php
use AiGenerateImageByReference\AiGenerateImageByReference;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$apiKey = 'sk-...';
$logger = new Logger('ai');
$logger->pushHandler(new StreamHandler('php://stdout'));
$ai = new AiGenerateImageByReference($apiKey, $logger);
$generatedImageBase64 = $ai
->setStyleImage('/path/to/reference-style.png') // reference image for style
->addContentImage('/path/to/content-image.png', 'Add glasses to the person') // content with description
->generate('A sunny outdoor scene with the person reading a book.');
if ($generatedImageBase64) {
file_put_contents('output.png', base64_decode($generatedImageBase64));
}
```
---
## ๐ง Advanced Usage (Custom HTTP Client)
You can pass a custom Guzzle client (e.g. with a mock handler for testing):
```php
use GuzzleHttp\Client;
$customClient = new Client([...]);
$ai = new AiGenerateImageByReference($apiKey, $logger, $customClient);
```
---
## ๐งฐ Options (`$options`)
Pass an options array to `generate()`:
| Key | Description | Default Value |
| ------------ | -------------------------------------------- | ------------- |
| `MODEL` | OpenAI model | `gpt-image-1` |
| `SIZE` | Image size (`1024x1024`, etc.) | `1024x1024` |
| `N` | Number of images | `1` |
| `BACKGROUND` | Background (`transparent`, `opaque`, `auto`) | `transparent` |
Unknown keys will be ignored with a warning if logger is set.
---
## ๐งช Running Tests
```bash
composer install
vendor/bin/phpunit
```
> All tests live in `tests/` and are PSR-4 autoloaded.\
> Uses Guzzle's `MockHandler` for full offline test coverage.
---
## ๐งผ Code Style
This project uses **PHP-CS-Fixer** with strict PSR-12 rules.
### โถ๏ธ Run it
```bash
vendor/bin/php-cs-fixer fix
```
> ๐ก If you're on PHP 8.4 (or fighting entropy), run it with:
>
> ```bash
> PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix
> ```
---
## ๐ง How It Works
- First image set via `setStyleImage()` defines the visual style.
- Additional images via `addContentImage()` define the content (e.g. characters).
- Prompt describes final composition.
- Uses `multipart/form-data` to call OpenAI API.
- Returns base64-encoded image.
---
## ๐ฆ Requirements
- PHP 8.1+
- OpenAI API key with image generation access
- `guzzlehttp/guzzle`
- (optional) `psr/log` for logging
---
## ๐ฌ Want to contribute?
Sure, fork it. Or open a PR.\
Or just write a poem about AI and post it on your fridge. We approve.
---
## โ License
MIT. Hack it, fork it, print it on a mug.
---
## ๐งช Real-World Example
```php
pushHandler(new StreamHandler('php://stdout'));
$ai = new AiGenerateImageByReference($apiKey, $logger);
$generatedImageBase64 = $ai
->setStyleImage('style.png')
->addContentImage('tyna.png', 'girl named Tina')
->addContentImage('fredy.png', 'boy named Fredy')
->generate('Fredy and Tina in a park on a sunny day with blue sky โ Fredy is riding a bicycle');
if ($generatedImageBase64) {
file_put_contents('output.png', base64_decode($generatedImageBase64));
}
```