adachsoft / image-reader-lib
GD-based image reader with magic-byte format detection, size limits, and proportional downscaling.
Requires
- php: ^8.3
- ext-gd: *
- 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 implements the image-reader contracts with PHP GD. It detects PNG, JPEG, GIF, and WebP from magic bytes, enforces encoded input/output byte limits and a decoded-pixel limit before GD decoding (50,000,000 pixels by default), and proportionally resizes images while retaining their format and supported transparency.
Animated GIF input is decoded and re-encoded as its first frame.
Requirements
The package requires PHP with the GD extension enabled. Composer installs the image-reader contracts dependency.
Installation
Install the library with Composer:
composer require adachsoft/image-reader-lib
PHP GD must be enabled in the PHP runtime that uses the package.
Usage
Integration
Create a GdImageReader and pass the encoded image bytes together with an ImagePrepareOptionsDto from the contracts package. The reader returns a PreparedImageDto containing the prepared bytes, detected format, resulting dimensions, and original-image metadata. Catch the image-reader contract exceptions to handle invalid, unsupported, or oversized input.
API
use AdachSoft\ImageReaderContracts\ImagePrepareOptionsDto;
use AdachSoft\ImageReaderLib\GdImageReader;
$reader = new GdImageReader();
$options = new ImagePrepareOptionsDto(
maxDimension: 2048,
maxInputBytes: 10_000_000,
maxOutputBytes: 2_000_000,
maxInputPixels: 50_000_000,
);
$prepared = $reader->prepare($imageBytes, $options);