Search by

adachsoft / image-editor-contracts

Arkadiusz Adach

Contracts for editing and creating images (operations, DTOs, exceptions).

Package info

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

Issues

pkg:composer/adachsoft/image-editor-contracts

Statistics

Installs: 36

Dependents: 2

Suggesters: 0

Stars: 0

v0.2.0 2026-11-29 16:00 UTC

This package is auto-updated.

Last update: 2026-11-29 15:00:29 UTC


README

Purpose

This package defines a framework-independent API for editing image bytes or creating images on a new canvas. It contains immutable request and result DTOs, operation DTOs, an image-editor interface, enums, and domain exceptions. The package depends only on PHP and the image-reader format contract.

Main capabilities

  • Immutable request contract (ImageEditRequestDto) that carries exactly one image source: encoded source bytes or a canvas.
  • Canvas and color value objects (CanvasDto, ColorDto) with hexadecimal #RRGGBB / #RRGGBBAA parsing.
  • Sixteen operation DTOs: crop, resize, clockwise rotation, flip, grayscale, negate, brightness, contrast, colorize, color replacement, blur, text, rectangle, arrow, image overlay, and fit.
  • Ordered, immutable ImageOperationCollection without a third-party collection dependency.
  • Explicit resource limits (ImageEditLimitsDto): input bytes, input pixels, output pixels, operation count, text length, and output bytes.
  • Encoded result contract (EditedImageDto) with format, bytes, final dimensions, and the number of applied operations.
  • Self-validating DTOs: invalid values are rejected with InvalidArgumentException at construction time.
  • Domain exceptions for failures, exceeded limits, invalid operations, and unsupported source images.
  • A single entry point, ImageEditorInterface, that implementation packages (for example GD-based ones) implement.

Installation

composer require adachsoft/image-editor-contracts

Requirements

  • PHP 8.3 or newer.
  • The package itself needs no image extension; it depends only on the adachsoft/image-reader-contracts format contract. Concrete editors may require ext-gd.

Integration

Depend on this package instead of a concrete editor. Consumers build an ImageEditRequestDto and call ImageEditorInterface::edit(), while an implementation package provides the actual image library.

use AdachSoft\ImageEditorContracts\EditedImageDto;
use AdachSoft\ImageEditorContracts\ImageEditorInterface;
use AdachSoft\ImageEditorContracts\ImageEditRequestDto;

final class MyImageEditor implements ImageEditorInterface
{
    public function edit(ImageEditRequestDto $request): EditedImageDto
    {
        // Decode, apply $request->operations in order, encode to $request->outputFormat,
        // and enforce $request->limits during processing.
    }
}

Contract rules for implementations:

  • Validate nothing twice: DTO constructors already reject impossible values, so implementations only enforce resource limits while processing.
  • Throw ImageEditLimitExceededException when a limit is exceeded, InvalidImageOperationException when an operation cannot be applied, UnsupportedSourceImageException when the source format is not supported, and ImageEditFailedException when the underlying library fails.
  • Return EditedImageDto with the encoded bytes, final dimensions, and the number of applied operations.

API

Create an ImageEditRequestDto with exactly one of source bytes or a CanvasDto, an ordered ImageOperationCollection, an output ImageFormatEnum, validated ImageEditLimitsDto limits, and an optional output quality from 1 through 100.

use AdachSoft\ImageEditorContracts\ImageEditLimitsDto;
use AdachSoft\ImageEditorContracts\ImageEditRequestDto;
use AdachSoft\ImageEditorContracts\ImageOperationCollection;
use AdachSoft\ImageReaderContracts\ImageFormatEnum;

$request = new ImageEditRequestDto(
    sourceBytes: $imageBytes,
    canvas: null,
    operations: new ImageOperationCollection([]),
    outputFormat: ImageFormatEnum::Png,
    limits: new ImageEditLimitsDto(
        maxInputBytes: 10_000_000,
        maxInputPixels: 50_000_000,
        maxOutputPixels: 50_000_000,
        maxOperations: 20,
        maxTextLength: 2_000,
        maxOutputBytes: 5_000_000,
    ),
);

Supported operation DTOs include crop, resize, clockwise rotation, flip, grayscale, negate, brightness, contrast, colorize, color replacement, blur, text, rectangle, arrow, image overlay, and fit operations.

Brand and social image operations

Watermark a photo with a logo

Place a logo at the bottom-right corner, sized to 15% of the photo width, with a soft black shadow:

use AdachSoft\ImageEditorContracts\AnchorEnum;
use AdachSoft\ImageEditorContracts\ImageOperationCollection;
use AdachSoft\ImageEditorContracts\Operation\OverlayImageOperationDto;
use AdachSoft\ImageEditorContracts\ShadowDto;

$watermark = new OverlayImageOperationDto(
    imageBytes: $logoPngBytes,
    anchor: AnchorEnum::BOTTOM_RIGHT,
    marginX: 24,
    marginY: 24,
    widthPercent: 15.0,
    shadow: new ShadowDto(offsetX: 1, offsetY: 2, blur: 8, opacity: 35),
);

$operations = new ImageOperationCollection([$watermark]);

Create a Pinterest pin without cropping the subject

Fit a landscape 16:9 photo to a 1000×1500 portrait canvas. The full photo remains visible and fills the padding with a blurred, cover-scaled version of itself:

use AdachSoft\ImageEditorContracts\FitModeEnum;
use AdachSoft\ImageEditorContracts\ImageOperationCollection;
use AdachSoft\ImageEditorContracts\Operation\FitOperationDto;

$pin = new FitOperationDto(
    width: 1000,
    height: 1500,
    mode: FitModeEnum::CONTAIN_BLURRED_BACKGROUND,
    blurStrength: 12,
);

$operations = new ImageOperationCollection([$pin]);

Examples

License

MIT. See the composer.json license field.