Search by

adachsoft / image-reader-contracts

Arkadiusz Adach

Contracts for reading and preparing images for AI models (interfaces, DTOs, exceptions).

Package info

gitlab.com/a.adach/image-reader-contracts

Issues

pkg:composer/adachsoft/image-reader-contracts

Statistics

Installs: 45

Dependents: 5

Suggesters: 0

Stars: 0

v0.1.0 2026-11-27 23:39 UTC

This package is auto-updated.

Last update: 2026-11-27 22:39:45 UTC


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

  • ImageReaderInterface as the single entry point for preparing image bytes.
  • ImagePrepareOptionsDto with validated limits for dimension, input bytes, output bytes, and decoded pixels.
  • PreparedImageDto describing prepared bytes, format, prepared dimensions, and original-image metadata.
  • ImageFormatEnum for 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.
  • ImageReaderInterface must 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.

  1. Require this package and depend on ImageReaderInterface in your services.
  2. Implement ImageReaderInterface::prepare() in an infrastructure package, for example an adapter built on GD or ImageMagick, and validate input against ImagePrepareOptionsDto.
  3. Return a PreparedImageDto with the retained format, prepared bytes, prepared dimensions, and original-image metadata.
  4. Throw UnsupportedImageFormatException, InvalidImageException, or ImageTooLargeException for the corresponding failures, so callers can react without knowing the adapter.

Practical examples are available in docs/examples.