adachsoft / image-reader-contracts
Contracts for reading and preparing images for AI models (interfaces, DTOs, exceptions).
Package info
gitlab.com/a.adach/image-reader-contracts
pkg:composer/adachsoft/image-reader-contracts
Requires
- php: ^8.3
Requires (Dev)
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12.5
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Purpose
This package defines the framework-independent API for preparing supported image bytes. It contains only PHP interfaces, immutable DTOs, an image-format enum, and domain exceptions. Preparation options limit image dimensions, encoded input/output bytes, and decoded pixels; the decoded-pixel limit defaults to 50,000,000.
API
Implement ImageReaderInterface::prepare() to accept raw image bytes and validated ImagePrepareOptionsDto limits, including maxInputPixels, then return a PreparedImageDto with the image format, prepared bytes, dimensions, and original-image metadata.
$options = new ImagePrepareOptionsDto(
maxDimension: 2048,
maxInputBytes: 10_000_000,
maxOutputBytes: 2_000_000,
maxInputPixels: 50_000_000,
);
$prepared = $imageReader->prepare($imageBytes, $options);
Usage
Depend on the interface and build validated options before each preparation call.
use AdachSoft\ImageReaderContracts\ImagePrepareOptionsDto;
use AdachSoft\ImageReaderContracts\ImageReaderInterface;
final class ImagePreparer
{
public function __construct(
private readonly ImageReaderInterface $imageReader,
) {
}
public function prepare(string $bytes): string
{
$options = new ImagePrepareOptionsDto(
maxDimension: 1024,
maxInputBytes: 8_000_000,
maxOutputBytes: 1_000_000,
);
return $this->imageReader->prepare($bytes, $options)->bytes;
}
}
Features
ImageReaderInterfaceas the single entry point for preparing image bytes.ImagePrepareOptionsDtowith validated limits for dimension, input bytes, output bytes, and decoded pixels.PreparedImageDtodescribing prepared bytes, format, prepared dimensions, and original-image metadata.ImageFormatEnumfor PNG, JPEG, GIF, and WebP with MIME type lookup.- A dedicated exception hierarchy rooted at
AbstractImageReaderException. - No runtime dependencies and no extension requirements beyond PHP itself.
Installation
composer require adachsoft/image-reader-contracts
Requirements
- PHP 8.3 or newer.
- Composer 2 to install the package.
- No PHP image extension is required by this package; every implementation declares its own extension needs.
ImageReaderInterfacemust be implemented by the application or by a companion adapter package.
Integration
The package contains contracts only, so an application provides the implementation and the contracts describe what that implementation must do.
- Require this package and depend on
ImageReaderInterfacein your services. - Implement
ImageReaderInterface::prepare()in an infrastructure package, for example an adapter built on GD or ImageMagick, and validate input againstImagePrepareOptionsDto. - Return a
PreparedImageDtowith the retained format, prepared bytes, prepared dimensions, and original-image metadata. - Throw
UnsupportedImageFormatException,InvalidImageException, orImageTooLargeExceptionfor the corresponding failures, so callers can react without knowing the adapter.
Practical examples are available in docs/examples.