If you use the twig filter image_style on a path that link to an svg image, it will create an error.

Since the core provide a method supportsUri to check if image styles are supported or not, we should use it before applying the style. The core issue was this one: Image styles do not play nicely with SVGs

I created a patch that allow to use the filter and will not crash if someone put an svg.

Comments

phjou created an issue. See original summary.

phjou’s picture

Title: image_style don't support SVG » image_style doesn't support SVG
StatusFileSize
new641 bytes
phjou’s picture

StatusFileSize
new617 bytes
new577 bytes

Sorry I put the first broken patch. The good now is there now.

phjou’s picture

Status: Active » Needs review
chi’s picture

Status: Needs review » Needs work

With this patch image_style will return the path for SVG image as is, which is inconsistent with core behavior.

From the change record:

If the style does not support the image format, it will simply set the image's #access property to FALSE, which by default will produce no markup at all.

I think we should follow the same approach. Simply return NULL if image format is not supported.

phjou’s picture

@Chi Ok I got your point. But should we change the API then? This module is supposed to make developers life easier, but basically you can't use that filter each time you allow an image format that is not supported. And since svgs are more and more used, I feel that filter is kind of useless.

For me, it is more confusing to have nothing displayed than the original image. And the behaviour you describe doesn't seem to happen, an error is thrown when the format is not supported. So I think that it is not handled properly, but maybe I'm wrong.

chi’s picture

basically you can't use that filter each time you allow an image format that is not supported

Isn't that true for Drupal core as well? If you upload SVG it won't be displayed through image formatter unless it is supported by image toolkit.

Also it is not a big deal to apply the filter conditionally.
{% if uri|split('.')|last != 'svg' %} {{ uri|image_style('large') }} {% endif %}

phjou’s picture

Yeah, you're right.
Maybe it's going to change in the future but for now it looks to be the case.

I'm a little bit confused because there is an issue that seems to have allowed svg but in fact it's not working when you try.
Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only)

So I am lost but you're right the core doesn't add support for svgs yet so we probably should leave it.

chi’s picture

Then we need just add$image_style->supportsUri($path) condition to avoid errors.

  • Chi committed 5372b82 on 8.x-2.x
    Issue #3053556 by phjou: image_style doesn't support SVG
    
chi’s picture

Status: Needs work » Fixed

I've added custom error for this case. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.