adachsoft / ai-image-contract
Requires
- adachsoft/collection: ^3.0
Requires (Dev)
- adachsoft/php-code-style: ^0.4.2
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
- rector/rector: ^2.3
This package is not auto-updated.
Last update: 2026-03-11 22:36:29 UTC
README
A clean abstraction layer (interfaces, DTOs, Value Objects, Collections, Exceptions) for AI image generation systems. This library serves as a foundation for various implementations (e.g., OpenAI DALL-E, Stable Diffusion, Midjourney) and supports advanced scenarios such as Multi-Image Prompting, Inpainting, and ControlNet.
Requirements
- PHP 8.2 or higher
adachsoft/collection
Installation
You can install the package via composer:
composer require adachsoft/ai-image-contract
Usage
This library provides the core contract (ImageGeneratorInterface) and necessary data structures. It does not contain any specific API implementation. You should use or create an adapter that implements this contract.
Basic Example
use AdachSoft\AiImageContract\Contracts\ImageGeneratorInterface;
use AdachSoft\AiImageContract\Models\GenerationRequest;
use AdachSoft\AiImageContract\ValueObjects\Prompt;
use AdachSoft\AiImageContract\ValueObjects\ImageSize;
use AdachSoft\AiImageContract\Collections\ImageInputCollection;
/** @var ImageGeneratorInterface $generator */
$generator = ... // Instantiate your specific implementation
$request = new GenerationRequest(
prompt: new Prompt('A futuristic city at sunset, cyberpunk style'),
imageInputs: new ImageInputCollection([]),
size: new ImageSize(1024, 1024),
options: [
'steps' => 30,
'cfg_scale' => 7.5,
]
);
try {
$response = $generator->generate($request);
foreach ($response->images as $image) {
echo "Generated Image URL: " . $image->url . "\n";
if ($image->revisedPrompt) {
echo "Revised Prompt: " . $image->revisedPrompt . "\n";
}
}
} catch (\AdachSoft\AiImageContract\Exceptions\AiImageExceptionInterface $e) {
echo "Generation failed: " . $e->getMessage();
}
Advanced Usage (Image-to-Image / ControlNet)
You can pass input images to the generator using the ImageInputCollection.
use AdachSoft\AiImageContract\ValueObjects\ImageInput;
use AdachSoft\AiImageContract\Models\Enums\ImageInputTypeEnum;
use AdachSoft\AiImageContract\Collections\ImageInputCollection;
$imageInputs = new ImageInputCollection([
new ImageInput(
source: 'https://example.com/base-image.jpg',
type: ImageInputTypeEnum::REFERENCE,
weight: 0.8
),
new ImageInput(
source: '/path/to/mask.png',
type: ImageInputTypeEnum::MASK
)
]);
$request = new GenerationRequest(
prompt: new Prompt('Add a red car to the street'),
imageInputs: $imageInputs
);
Architecture
The library is structured into several key namespaces:
- Contracts: Contains the main
ImageGeneratorInterface. - Models: Contains DTOs like
GenerationRequest,ImageResponse, andGeneratedImage. - ValueObjects: Contains immutable objects like
Prompt,ImageSize, andImageInput. - Collections: Contains strongly-typed collections (
GeneratedImageCollection,ImageInputCollection) based onadachsoft/collection. - Exceptions: Contains standard exceptions (
GenerationFailedException,InvalidInputException) implementingAiImageExceptionInterface.
Testing
composer test
License
The MIT License (MIT).