JoliMediaBundle manages the media of a Symfony application, from the upload to the <img> tag:
it stores the files with Flysystem, generates optimized
variations of the images (resized, cropped, converted to WebP or AVIF), outputs responsive
markup, and adds a media library to
EasyAdmin,
SonataAdmin and
Sylius / Sylius Stack.
Tip
Try it in your browser: the demo application runs in WebAssembly, with its EasyAdmin, SonataAdmin and Sylius admins. There is nothing to install, and what you upload stays in your browser.
Define a variation:
# config/packages/joli_media.yaml
joli_media:
libraries:
default:
original:
flysystem: media.original.storage
url_generator:
path: /media/original/
cache:
flysystem: media.cache.storage
url_generator:
path: /media/cache/
enable_auto_webp: true
pixel_ratios: [1, 2]
variations:
card:
transformers:
thumbnail:
width: 400
height: 300Use it in a template:
<twig:joli:Picture path="photo.jpg" variation="card" alt="A circle pattern" />And get a WebP alternative, a version for high-density screens, the dimensions and lazy loading:
<picture>
<source
sizes="400px"
srcset="/media/cache/card-webp/photo.4ca76c43.webp 400w, /media/cache/card-2x-webp/photo.4ca76c43.webp 800w"
type="image/webp"
width="400"
height="300"
/>
<img
src="/media/cache/card/photo.jpg"
loading="lazy"
decoding="async"
alt="A circle pattern"
srcset="/media/cache/card/photo.jpg 400w, /media/cache/card-2x/photo.jpg 800w"
sizes="400px"
width="400"
height="300"
/>
</picture>Each file is generated the first time it is requested, then served as a static file.
The EasyAdmin, SonataAdmin and Sylius bridges add a media library to browse, upload and organize the files, and a media picker for your forms.
The bundle also comes with console commands to generate and audit the variations, a profiler panel, events and a Doctrine type to link media to your entities.
- Media are referenced by their path. There is no media entity and no database table: the files in the storage are the only source of truth.
- Variations are defined in the configuration. The URL of a variation contains its name and the path of the media, nothing else, so a visitor cannot request a size you did not define.
- The transformers of a variation are combined into a single operation when possible, which avoids encoding the image several times.
cwebp,mozjpeg,oxipngand other tools are used when they are installed. Without them, the Imagine library and one PHP extension are enough.- The Twig components output
srcset,width,height, a WebP<source>and lazy loading, including for variations that have not been generated yet. - When a media is renamed or moved, the Doctrine columns that reference it are updated. When it is deleted, you choose between emptying the column and refusing the deletion.
JoliMediaBundle requires PHP 8.2 or later, and Symfony 7 or 8:
composer require jolicode/media-bundle
Then follow the getting started guide.
Read the detailed documentation of the bundle.
Its sources are in doc/, and its website is built from them in doc-website/: run
castor doc:start to build it, then castor doc:serve to browse it on http://localhost:8000/.
This software is published under the MIT License.


