Repository navigation
feat(svg): Rasterize SVG media into variations when the rasterize_svg setting is enabled - #188
Merged
Merged
Conversation
… 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
rvanlaak
force-pushed
the
feature/svg-input
branch
from
November 4, 2026 09:22
504657f to
79537a8
Compare
… 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.
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 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
How it works
Format::SVGis an input-only format. No processor outputs SVG, sogetAlternativeFormat()returns PNG, and a variation without aformatturns a SVG media into a PNG.ImageDimensionsGuesserreads the intrinsic size of a SVG from the unitless orpxwidthandheightof its root element, or else from itsviewBox. 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.rasterize_svgenabled, theimagineprocessor lists SVG as an input format.Converter,Resolver::isMediaProcessable(),Resolver::isPathProcessable()andjoli:media:debug:processorspick it up without changes.Transformation::getAsImagineCallback()takes an optional source scale, so that thecropcoordinates apply to the larger raster. Its behavior is unchanged at the default scale of 1.Opt-in, with no new hard requirement
rasterize_svgis disabled by default, so nothing changes for the applications that do not enable it. Once it is enabled, the newSvgRasterizationPassmakes sure that the driver isimagick, 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-convertImageMagick 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-extrainstalled). 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:
imagickrenderer stays the default and works as before, so nothing changes for the applications that do not setsvg_renderer.rsvg-convertrenders the SVG at the size the transformation needs (--width/--heightfrom the intrinsic size and the scale), so the crop and resize steps are the same as withimagick. The result is loaded into Imagine as a PNG, so this renderer works with any driver (gdincluded).rsvg-convert, the compiler pass skips its Imagick checks. The binary is listed under theimagineprocessor injoli:media:debug:processors, which checks it like the other binaries, since it may only be installed where the application runs.librsvg2-bin. The existing SVG tests run against both renderers, and a new gradient fixture checks thatrsvg-convertrenders the gradient. With ImageMagick's internal renderer, that gradient comes out black.