Image compression helpers for Componenta. The package compresses image content without changing its format and selects a concrete compressor by detected MIME type.
composer require componenta/image-compressorThe package declares Componenta\Image\ImageCompressorConfigProvider in extra.componenta.config-providers.
When componenta/composer-plugin is installed, the provider is added to the generated provider list automatically.
The package requires PHP ^8.4, ext-gd, componenta/config, componenta/mimetype-detector, PSR-11, and PSR-7 streams.
use Componenta\Image\ImageCompressionOptions;
use Componenta\Image\ImageCompressor;
if ($compressor->canCompress($source)) {
$image = $compressor->compress($source, new ImageCompressionOptions(quality: 85));
}$source may be a binary string or StreamInterface. CompressedImage contains content, mimeType, extension, and computed size.
Built-in GD compressors support JPEG, PNG, WebP, and AVIF when the local GD extension supports the target operation.
ImageCompressorConfigProvider registers:
ImageCompressorGdJpegCompressorGdPngCompressorGdWebPCompressorGdAvifCompressor
ImageCompressorFactory uses MimeTypeDetectorInterface from the container when available and falls back to FinfoDetector.
ImageCompressionOptions::$quality must be null or an integer from 0 to 100. Invalid quality throws CompressionException::invalidQuality().
ImageCompressor throws CompressionException when the source type cannot be detected or no compressor supports it.
Implement ImageCompressorInterface and call ImageCompressor::addCompressor() to add another compressor.