utafitilabs / image-bundle
Image renditions for Symfony: named profiles turn one kept original into fixed renditions on Flysystem storages, made at once or through Messenger, on imagine/imagine.
Package info
github.com/utafitilabs/image-bundle
Type:symfony-bundle
pkg:composer/utafitilabs/image-bundle
Requires
- php: >=8.4
- ext-dom: *
- ext-exif: *
- ext-fileinfo: *
- ext-libxml: *
- imagine/imagine: ^1.5
- league/flysystem: ^3.25.1
- psr/container: ^2.0
- psr/event-dispatcher: ^1.0
- symfony/config: ^7.3 || ^8.0
- symfony/console: ^7.3 || ^8.0
- symfony/dependency-injection: ^7.3 || ^8.0
- symfony/framework-bundle: ^7.3 || ^8.0
- symfony/http-foundation: ^7.3 || ^8.0
- symfony/http-kernel: ^7.3 || ^8.0
- symfony/messenger: ^7.3 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- league/flysystem-bundle: ^3.4
- league/flysystem-memory: ^3.25.1
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^13.2
- symfony/twig-bundle: ^7.3 || ^8.0
- twig/twig: ^3.21
Suggests
- ext-gd: The gd driver (the default).
- ext-imagick: The imagick driver.
- twig/twig: The image_url, image_srcset and image_fallback functions.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-25 11:48:14 UTC
README
This bundle turns one kept original into fixed renditions, described by named
profiles in YAML: widths, heights or a crop to an aspect, JPEG, PNG, WebP or AVIF,
transparency kept or flattened onto a background, the EXIF orientation applied and
no metadata copied, a clear margin trimmed, an SVG cleaned before it is served.
Renditions are made ahead of time, at once or by a Messenger worker, and written to
Flysystem storages, so a public page reads files from a bucket and never runs PHP for a
picture. Name a profile, store an upload, print its srcset.
Part of the utafiti tools.
Contents
Why
Every application that takes pictures ends up writing the same few hundred lines: decode, turn, crop, scale, encode, write three sizes, delete them again, answer their addresses, and check the upload before any of it. The usual bundles make images on request behind a resolve route, which puts PHP in front of every new size and ties addresses to their cache layout. This bundle keeps the original private, writes every rendition once under a name you can predict, and leaves your records, screens and words to you.
Install
Applications using Symfony Flex:
composer require utafitilabs/image-bundle
Applications without Symfony Flex — after requiring the package, enable the bundle:
// config/bundles.php return [ // ... UtafitiLabs\ImageBundle\UtafitiLabsImageBundle::class => ['all' => true], ];
That one line registers the images service (ImagesInterface), the upload checker, the
MakeRenditions handler, the image:renditions command and, when Twig is installed,
image_url, image_srcset and image_fallback. Then name your two storages and a
profile:
# config/packages/utafitilabs_image.yaml utafitilabs_image: storages: originals: app.storage.originals # private: kept exactly as uploaded renditions: app.storage.media # public: its public_url gives the addresses profiles: photo: accept: [image/jpeg, image/png] max_bytes: 20M max_pixels: 40000000 sizes: [320, 800, 1280, 2000] format: webp background: '#FFFFFF' fallback: { size: 1280, format: jpeg, interlace: true } generate: async
# config/packages/messenger.yaml framework: messenger: routing: UtafitiLabs\ImageBundle\Message\MakeRenditions: async
The storages are Flysystem operators, as flysystem-bundle names them:
# config/packages/flysystem.yaml flysystem: storages: app.storage.originals: adapter: local options: { directory: '%kernel.project_dir%/var/originals' } app.storage.media: adapter: local options: { directory: '%kernel.project_dir%/public/media' } public_url: '%env(MEDIA_PUBLIC_URL)%'
A taste
// src/Controller/PhotoUploadController.php namespace App\Controller; use Symfony\Component\HttpFoundation\File\UploadedFile; use UtafitiLabs\ImageBundle\Model\RefusalReasonEnum; use UtafitiLabs\ImageBundle\Service\ImagesInterface; final readonly class PhotoUploadController { public function __construct(private ImagesInterface $images, private PhotoStore $photos) { } public function upload(UploadedFile $file): string { // The bundle answers with a code and the limit; the words are yours. if (null !== $refusal = $this->images->check('photo', $file)) { return match ($refusal->reason) { RefusalReasonEnum::Bytes => sprintf('Over %d MB', $refusal->limit / 1024 / 1024), RefusalReasonEnum::Type => 'A JPEG or a PNG, please', default => 'This picture cannot be used', }; } $stored = $this->images->store('photo', $file); // "3f9c…e1.jpg"; renditions queued $this->photos->add($stored->name, $stored->width, $stored->height, $stored->takenAt); return $stored->name; } }
{# templates/photo/_picture.html.twig #} <picture> <source type="image/webp" srcset="{{ image_srcset('photo', photo.name, photo.width) }}" sizes="(max-width: 700px) 100vw, 700px"> <img src="{{ image_fallback('photo', photo.name) }}" alt="{{ photo.caption }}" loading="lazy"> </picture>
php bin/console image:renditions photo --all
Fits: width, height, contain (the longest side), cover (a crop to aspect from the
middle). Formats: jpeg, png, webp, avif, or original. Checks: type by content,
max_bytes, max_pixels, min_width, min_height, min_side, max_side,
require_clear_edge. Service: ImagesInterface (check, store, storeAs, url,
fallbackUrl, srcset, redraw, delete). Events: RenditionsWritten,
RenditionsFailed. Message: MakeRenditions. Drivers: gd (default) and imagick.
Documentation
Read the documentation at docs/index.md — every profile key, paths and names, fits, formats and transparency, orientation and metadata, trimming, SVG cleaning and its limits, upload checks and their codes, synchronous and asynchronous generation, the command, events, and memory.
Requirements
- PHP 8.4+ with exif, fileinfo and dom, and gd or imagick
- Symfony 7.3+ / 8
- imagine/imagine 1.5+ and league/flysystem 3.25.1+ (installed with the bundle)
- AVIF needs a GD or ImageMagick built with it (
gd_info()['AVIF Support'],Imagick::queryFormats('AVIF'))
Contributing
See CONTRIBUTING.md. CI enforces the standard (php-cs-fixer, PHPStan max, PHPUnit against real GD and Imagick with pictures the tests draw themselves, a booted kernel with Messenger and Twig, lowest→newest dependency matrix, 95 % coverage) on every pull request.
Credits
License
MIT License (MIT): see the LICENSE file for more details.