Search by

padam87 / image-filter-bundle

Padam87

Image presets (thumbnails, previews, pages of PDFs) generated on upload through Messenger, with optional mutool, rsvg-convert, Ghostscript and ffmpeg preloaders in front of Imagick.

Package info

github.com/Padam87/ImageFilterBundle

Type:symfony-bundle

pkg:composer/padam87/image-filter-bundle

Statistics

Installs: 9

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v0.2.0 2026-11-28 23:53 UTC

This package is auto-updated.

Last update: 2026-11-29 00:04:30 UTC


README

Image presets for Symfony: thumbnails, previews and pages of PDFs, generated on upload through Messenger and served by the web server as static files.

  • Pages: every page of a PDF (or XPS, EPUB, multi-page TIFF) is an image of its own
  • Queued: generated by workers on upload; a file not generated yet is generated on its first request
  • Preloaders: mutool, rsvg-convert, Ghostscript and ffmpeg render what Imagick reads poorly (or not at all) to a PNG first. Enable the ones you have installed: without them, Imagick reads the file itself
  • Hints: filters tell the preloaders what they need; a preset's output size makes a vector page drawn at that size, and a large JPEG decoded at a reduced scale
  • Versioned presets: a new version of a preset gets new urls, no cache to clear

Installation

composer require padam87/image-filter-bundle
// config/bundles.php
Padam87\ImageFilterBundle\Padam87ImageFilterBundle::class => ['all' => true],

The route of the files not generated yet:

# config/routes/padam87_image_filter.yaml
padam87_image_filter:
    resource: .
    type: padam87_image_filter

The queued messages go to a transport:

# config/packages/messenger.yaml
framework:
    messenger:
        routing:
            'Padam87\ImageFilterBundle\Message\FilterImageMessageAsync': async

The web server serves the generated files directly, and passes the missing ones to Symfony (the default Apache .htaccess and nginx try_files $uri /index.php$is_args$args both do).

Configuration

padam87_image_filter:
    media_dir: media                 # under public_dir; also the url path
    placeholder: '%kernel.project_dir%/public/images/placeholder.png' # when a file can't be generated (null: 404)

    presets:
        original:
            format: webp
            quality: 85
            passthrough: true        # images are linked as they are; PDFs, videos... are rendered
            auto: true               # generated on upload (ImageFilter::queue())
        thumbnail:
            format: webp
            quality: 80
            version: 2               # new urls for a changed preset
            auto: true
            filters:
                thumbnail: { width: 400, height: 400, upscale: false }
                pad: { width: 400, height: 400 }
        content:                     # urls saved in content: no version, they would break
            format: webp
            filters:
                downscale: { width: 1480, height: 1110 }

All the options, with their defaults:

padam87_image_filter:
    project_dir: '%kernel.project_dir%'   # the files are named by their path relative to it
    public_dir: '%kernel.project_dir%/public'
    media_dir: media
    secret: '%kernel.secret%'             # signs the resolve keys
    placeholder: null
    fallback:
        base_url: ''                      # dev on a copy of the prod database: missing sources point there
        secret: ''                        # that host's secret, so it generates what it has not yet
    preloaders:
        # resolution, width: without a size hint; max_resolution, max_size: the most a size hint gets
        # all disabled: enable the ones installed (an enabled one without its tool throws)
        mutool:      { enabled: false, binary: mutool,       on_failure: fail,     timeout: 60, resolution: 144, max_resolution: 600 }
        rsvg:        { enabled: false, binary: rsvg-convert, on_failure: fallback, timeout: 30, width: 2000, max_size: 8000 }
        ghostscript: { enabled: false, binary: gs,           on_failure: fallback, timeout: 30, resolution: 144, max_resolution: 600 }
        ffmpeg:      { enabled: false, binary: ffmpeg, ffprobe_binary: ffprobe, on_failure: fail, timeout: 30, seek: 1.0 }
    presets:
        <name>:
            format: webp
            quality: 80
            background: transparent       # padding, transparent areas; jpg is flattened onto it (white if transparent)
            version: null
            passthrough: false
            auto: false
            strip: true                   # remove the metadata
            resolution: 144               # DPI of the vector sources without a size hint (no filter resizes)
            filters: {}                   # name: options, in this order

Usage

// on upload (e.g. a Vich POST_UPLOAD listener): every page of the auto presets, queued
$imageFilter->queue($file);
$probe = $imageFilter->probe($file);      // pageCount, width, height (PDFs: points)

// urls: the static file, with a resolve key until it is generated
$imageFilter->getPublicUrl($file, page: 1, preset: 'thumbnail');

// right now (the file is needed in this request)
$imageFilter->apply($file, 1, 'thumbnail');
$imageFilter->getFilteredFile($file, 1, 'thumbnail');

// on delete or replace: every page, every preset
$imageFilter->clearCache($file);
<img src="{{ image_filter(product.image.file, 'thumbnail') }}">
<img src="{{ image_filter(design.file, 'thumbnail', 2) }}">

apply() throws an ImageFilterException for a source it can't turn into the preset (a broken file, no such page); the queued messages of those are not retried.

Paths

public/media/<preset>[/v<version>]/<source path relative to the project, without its extension>_<page>.<format>

public/uploads/logo.pdf, page 2 of the thumbnail preset (version 2): public/media/thumbnail/v2/public/uploads/logo_2.webp.

Preloaders

A preloader renders a page to a PNG before Imagick reads it. The first one that supports the file and succeeds gives the PNG; without one, Imagick reads the file itself.

They are all disabled by default: enable the ones whose tools you have installed.

padam87_image_filter:
    preloaders:
        mutool: true
        ghostscript: { enabled: true, binary: /usr/local/bin/gs }
Preloader Reads Tool Disabled, Imagick...
mutool PDF, XPS, EPUB mutool reads them with Ghostscript
rsvg SVG rsvg-convert renders with its own, weaker renderer
ghostscript EPS, PostScript gs crude colours, a page around an EPS
ffmpeg video (a frame at seek) ffmpeg can't read them: no image

An enabled preloader whose tool is not installed throws a PreloaderUnavailableException for every file it reads: a configuration error, never skipped silently, and not a placeholder. When the tool runs and fails on a file, on_failure decides:

  • fail: no image. mutool also fails on a broken PDF it repaired and drew with errors, and Ghostscript would draw a wrong image of it: no image is better than a wrong one
  • fallback: the next preloader, or Imagick, reads it (rsvg timing out on a huge SVG: Imagick's image is still fine)

Your own: implement PreloaderInterface, and tag it with #[AsPreloader(priority: 50)] (mutool 40, rsvg 30, ghostscript 20, ffmpeg 10).

Hints

A filter tells what it needs of the source by yielding hints from hints(). A preset collects every hint of its filters, in order (ImageFilter::hints($preset), a Hints), and the preloaders read what they can use.

Size hints

The size hint is the only hint for now: the size a filter scales the image to. The preset's size is its latest size hint, the one closest to its output, so a source is read at about the size of the output instead of a fixed one: an A0 poster's 400 pixel thumbnail is not drawn at 9000 pixels first, and a large preset of a small PDF is not upscaled from a 144 DPI rendering.

thumbnail, resize and downscale yield one: fit (the image inside the box) for bestfit, cover (filling the box) for exact sizes. trim and pad yield nothing. A preset without a filter that resizes has no size hint: its vector sources are read at its resolution. To cap such a preset and give it a size, add a downscale.

Trim after resizing, not before: the source is rendered at the size of the resize, so trimming first would leave less than that and scale it up. Resized first, the result is smaller than the box by the trimmed margins, and sharp.

Read by With a size hint
mutool drawn at the resolution that gives the size (from the page's box), at most max_resolution
rsvg rendered at the size (cover: from the root element's size), at most max_size
ghostscript an EPS drawn at the resolution that gives the size (from its bounding box)
ffmpeg the frame scaled down to the size, never up
Imagick a JPEG decoded at 1/2, 1/4 or 1/8 (no less than twice the size); a PDF, EPS or SVG it reads itself at the resolution that gives the size

The preloaders get the hints as preload($path, $page, Hints $hints); $hints->size() is the size to render at (never smaller), $hints->latest(MyHint::class) any other hint, $hints->all() every one, in order.

Filters

Filter Options
thumbnail width, height, bestfit (true), fill (false), upscale (true) Imagick's thumbnail; fill pads to the box
resize width, height, bestfit (true), fill (false) Lanczos, up or down
downscale width, height fits it in the box, a smaller one is left as it is
trim fuzz (0, a share: 0.05) cuts the border of the corners' colour; not a plain image
pad width, height on a canvas of the box, in the middle, on the background

Every image is turned upright by its EXIF orientation and converted to sRGB first.

Your own:

#[AsImageFilter('mono')]
final class MonoFilter implements FilterInterface
{
    public function apply(\Imagick $image, array $options, Preset $preset): void
    {
        // ...
    }

    public function hints(array $options): iterable
    {
        // a colour filter: nothing to tell (a filter that scales yields a SizeHint)
        yield from [];
    }
}

Commands

  • image-filter:warm public/uploads [--preset=thumbnail] [--sync]: queues (or generates) the files of every source under the paths: after adding a preset or a new version of one
  • image-filter:prune [--dry-run] [--keep-orphans]: deletes the old versions' files, and the files of deleted sources. Only the configured presets' directories are touched

Security

A resolve key signs the file's path under the media directory (the preset, its version, the source and the page): an HMAC-SHA256 keyed by secret, its first 16 bytes, 22 characters. The MediaController finds the source from the path, so a key can't be moved to another file, preset or page, and shows nothing the url doesn't. Sources outside the project have no such path: their urls have no key, generate them with apply().