PHPackages                             kdosiodjinud/ai-generate-image-by-reference - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. kdosiodjinud/ai-generate-image-by-reference

ActiveLibrary[Image &amp; Media](/categories/media)

kdosiodjinud/ai-generate-image-by-reference
===========================================

Generate images using OpenAI with style reference

2.0.1(10mo ago)2443MITPHPCI passing

Since Jun 11Pushed 10mo agoCompare

[ Source](https://github.com/kdosiodjinud/ai-generate-image-by-reference)[ Packagist](https://packagist.org/packages/kdosiodjinud/ai-generate-image-by-reference)[ RSS](/packages/kdosiodjinud-ai-generate-image-by-reference/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (5)Versions (7)Used By (0)

🧠🎨 AiGenerateImageByReference
=============================

[](#-aigenerateimagebyreference)

A PHP library to generate images in the style of a reference image using the OpenAI Image API.

---

⚙️ Installation
---------------

[](#️-installation)

```
composer require kdosiodjinud/ai-generate-image-by-reference
```

> Requires PHP 8.1+, `guzzlehttp/guzzle`, and `psr/log`.

---

🚀 Usage
-------

[](#-usage)

```
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)
-------------------------------------

[](#-advanced-usage-custom-http-client)

You can pass a custom Guzzle client (e.g. with a mock handler for testing):

```
use GuzzleHttp\Client;

$customClient = new Client([...]);
$ai = new AiGenerateImageByReference($apiKey, $logger, $customClient);
```

---

🧰 Options (`$options`)
----------------------

[](#-options-options)

Pass an options array to `generate()`:

KeyDescriptionDefault 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
---------------

[](#-running-tests)

```
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
------------

[](#-code-style)

This project uses **PHP-CS-Fixer** with strict PSR-12 rules.

### ▶️ Run it

[](#️-run-it)

```
vendor/bin/php-cs-fixer fix
```

> 💡 If you're on PHP 8.4 (or fighting entropy), run it with:
>
> ```
> PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix
> ```

---

🧠 How It Works
--------------

[](#-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
--------------

[](#-requirements)

- PHP 8.1+
- OpenAI API key with image generation access
- `guzzlehttp/guzzle`
- (optional) `psr/log` for logging

---

📬 Want to contribute?
---------------------

[](#-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
---------

[](#-license)

MIT. Hack it, fork it, print it on a mug.

---

🧪 Real-World Example
--------------------

[](#-real-world-example)

```
