adachsoft / image-editor-contracts
Contracts for editing and creating images (operations, DTOs, exceptions).
Package info
gitlab.com/a.adach/image-editor-contracts
pkg:composer/adachsoft/image-editor-contracts
Requires
- php: ^8.3
- adachsoft/image-reader-contracts: ^0.1.0
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 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/#RRGGBBAAparsing. - 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
ImageOperationCollectionwithout 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
InvalidArgumentExceptionat 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-contractsformat contract. Concrete editors may requireext-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
ImageEditLimitExceededExceptionwhen a limit is exceeded,InvalidImageOperationExceptionwhen an operation cannot be applied,UnsupportedSourceImageExceptionwhen the source format is not supported, andImageEditFailedExceptionwhen the underlying library fails. - Return
EditedImageDtowith 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.