adachsoft / image-editor-lib
GD-based PHP image editor for cropping, resizing, rotating, flipping, filters, text, and shapes.
Requires
- php: ^8.3
- ext-gd: *
- adachsoft/image-editor-contracts: ^0.2
- adachsoft/image-reader-contracts: ^0.1.0
- adachsoft/image-reader-lib: ^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 implements the image-editor contracts with PHP GD. It accepts encoded image bytes or canvas settings, applies ordered image operations, enforces configured byte and pixel limits, and returns encoded image bytes. Supported formats are PNG, JPEG, GIF, and WebP.
The library uses the public ImageFormatDetector from the image-reader library to identify input formats from magic bytes. Source dimensions and input pixel limits are checked before decoding. PNG, WebP, and GIF output retain transparency where the target format supports it; JPEG output is flattened onto white.
Text drawing uses the bundled DejaVu Sans font and supports UTF-8 text, including Polish characters.
Installation
Install the library with Composer:
composer require adachsoft/image-editor-lib
PHP GD must be enabled. Composer installs the image editor contracts and image reader dependencies required by the library.
Integration
Create a GdImageEditor instance, build an ImageEditRequestDto with encoded source bytes, the requested operations and output settings, then call edit(). The returned EditedImageDto contains the encoded result. See the usage example for a complete integration snippet.
Requirements
- PHP 8.3 or later
- PHP GD extension
Usage
Pass encoded image bytes and an ordered collection of operations to the editor, then use the returned encoded bytes in your application. The complete request example is shown in the API section below and in Basic Usage.
API
use AdachSoft\ImageEditorContracts\ImageEditLimitsDto;
use AdachSoft\ImageEditorContracts\ImageEditRequestDto;
use AdachSoft\ImageEditorContracts\ImageOperationCollection;
use AdachSoft\ImageEditorLib\GdImageEditor;
use AdachSoft\ImageReaderContracts\ImageFormatEnum;
$editor = new GdImageEditor();
$result = $editor->edit(new ImageEditRequestDto(
sourceBytes: $imageBytes,
canvas: null,
operations: new ImageOperationCollection([]),
outputFormat: ImageFormatEnum::Png,
limits: new ImageEditLimitsDto(10_000_000, 50_000_000, 50_000_000, 20, 2_000, 5_000_000),
));
Brand and social image operations
Add a logo watermark
Place a logo in the bottom-right corner of a photo, sized to 15% of its 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 1000×1500 Pinterest pin from a 16:9 photo
Keep the full landscape photo visible on a portrait pin and fill the remaining space with its blurred, cover-scaled background:
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]);