Skip to content

feat(svg): Rasterize SVG media into variations when the rasterize_svg setting is enabled - #188

Merged
xavierlacot merged 2 commits into
jolicode:mainfrom
rvanlaak:feature/svg-input
Nov 16, 2026
Merged

xavierlacot merged 2 commits into
jolicode:mainfrom
rvanlaak:feature/svg-input

Conversation

@rvanlaak

@rvanlaak rvanlaak commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes #187

SVG media can now be rasterized into PNG, WebP, AVIF, JPEG or GIF variations, for contexts that cannot display SVG, such as email clients:

joli_media:
    processors:
        imagine:
            rasterize_svg: true
    libraries:
        default:
            variations:
                email_logo:
                    format: png
                    transformers:
                        heighten:
                            height: 168

How it works

  • Format::SVG is an input-only format. No processor outputs SVG, so getAlternativeFormat() returns PNG, and a variation without a format turns a SVG media into a PNG.
  • ImageDimensionsGuesser reads the intrinsic size of a SVG from the unitless or px width and height of its root element, or else from its viewBox. It uses plain PHP, with no network access and no entity substitution, so the transformers, the dimension predictor and the Twig components work with SVG media.
  • With rasterize_svg enabled, the imagine processor lists SVG as an input format. Converter, Resolver::isMediaProcessable(), Resolver::isPathProcessable() and joli:media:debug:processors pick it up without changes.
  • The SVG is rendered at a density matched to the target size, so that an enlarged variation stays sharp, and on a transparent background. Transformation::getAsImagineCallback() takes an optional source scale, so that the crop coordinates apply to the larger raster. Its behavior is unchanged at the default scale of 1.

Opt-in, with no new hard requirement

rasterize_svg is disabled by default, so nothing changes for the applications that do not enable it. Once it is enabled, the new SvgRasterizationPass makes sure that the driver is imagick, that the extension is loaded, and that ImageMagick supports SVG. A missing dependency then fails when the container is compiled, with an error that tells what to install, rather than when a media is converted.

The documentation states that sanitizing SVG uploads is the job of the application, and points to ImageMagick's security policy.

Optional renderer: librsvg's rsvg-convert

ImageMagick renders SVG with its internal renderer (MSVG) unless it is built with librsvg, and Debian's packages are not (trixie, ImageMagick 7.1.1-43, even with libmagickcore-7.q16-10-extra installed). That renderer draws flat shapes well, but paints a gradient black and does not support masks, filters or CSS. Imagick::queryFormats('SVG') still lists SVG, so the compiler pass cannot tell.

A second, opt-in renderer uses librsvg instead:

joli_media:
    processors:
        imagine:
            rasterize_svg: true
            svg_renderer: rsvg-convert          # default: imagick
            # rsvg_convert_binary: /usr/bin/rsvg-convert (or JOLI_MEDIA_RSVG_CONVERT_BINARY)
            # process_timeout: 10              # overrides the global one
  • The imagick renderer stays the default and works as before, so nothing changes for the applications that do not set svg_renderer.
  • rsvg-convert renders the SVG at the size the transformation needs (--width/--height from the intrinsic size and the scale), so the crop and resize steps are the same as with imagick. The result is loaded into Imagine as a PNG, so this renderer works with any driver (gd included).
  • The SVG is passed on stdin. Without a base URI, librsvg does not load the files it references.
  • With rsvg-convert, the compiler pass skips its Imagick checks. The binary is listed under the imagine processor in joli:media:debug:processors, which checks it like the other binaries, since it may only be installed where the application runs.
  • The test image installs librsvg2-bin. The existing SVG tests run against both renderers, and a new gradient fixture checks that rsvg-convert renders the gradient. With ImageMagick's internal renderer, that gradient comes out black.

@rvanlaak rvanlaak changed the title Rasterize SVG media into variations when the rasterize_svg setting is enabled RFC: Rasterize SVG media into variations when the rasterize_svg setting is enabled Nov 4, 2026
@rvanlaak rvanlaak changed the title RFC: Rasterize SVG media into variations when the rasterize_svg setting is enabled feat(svg): Rasterize SVG media into variations when the rasterize_svg setting is enabled Nov 4, 2026
… enabled

SVG becomes an input-only format, falling back to PNG. The imagine
processor renders it at the density of the target size, so that enlarged
variations stay sharp, and the pixel dimensions are read from the width,
height or viewBox of its root element.

The setting is disabled by default. When enabled, a compiler pass makes
sure that the imagick driver and the SVG support of ImageMagick are
available, so that a missing dependency fails when the container is
compiled rather than when a media is converted.

Closes jolicode#187
… for it

ImageMagick renders SVG with its internal renderer unless it is built with
librsvg, which Debian's packages are not. That renderer paints a gradient
black, and supports no masks, filters or CSS, while Imagick::queryFormats()
still lists SVG.

The new svg_renderer setting of the imagine processor keeps imagick as its
default, and can be set to rsvg-convert, which renders the SVG with librsvg
at the size the transformation needs. The PNG it outputs goes through
Imagine as before, with any driver. The SVG is given on stdin, so librsvg
loads none of the files it references. The binary is set by
rsvg_convert_binary or JOLI_MEDIA_RSVG_CONVERT_BINARY, and checked by
joli:media:debug:processors rather than by the compiler pass.
@xavierlacot

Copy link
Copy Markdown
Member

Hi @rvanlaak,

Thanks a lot for this contribution, being able to rasterize SVG images is really helpful. I am merging this PR now.

I have also prepared #191 as a follow-up. It moves the rasterize_svg setting to the libraries and variations, so that it can be enabled per library and overridden per variation, like the other variation settings. This PR also makes minor changes (it rejects format: svg on a variation, flattens the JPEG variations of SVG media on a white background).

Could you please have a look at this PR and comment on it? I plan to merge it by the end of the week if there are no objections.

@xavierlacot
xavierlacot merged commit c910ce8 into jolicode:main Nov 16, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support SVG as an input format (to allow rasterize to PNG/WebP variations)

2 participants