Drupal 9.2 supports the webP image format (see change record: https://www.drupal.org/node/3171135).
In order for browsers that do not support WebP (earlier versions of Safari & IE), the image needs to be wrapped in a <picture> tag like so:
<picture>
<source srcset="my-awesome-picture.webp" type="image/webp">
<img src="my-awesome-picture.jpg" alt="Awesome">
</picture>
Older browsers will look at the type attribute on the <source> element, and fallback to the <img> element.
This is next to impossible to do with Drupal core (although you can get it working in an extremely hacky way!)
The breakpoint module gives you the option to select media queries and multipliers, but does not give you an option to select the type.
I do not have an option to create multiple image styles on a single media query. I can hack this by creating multiple media queries such as 'all and (min-width: 1x)' and 'all and (min-width: 2x)'. But this is just plain ol' wrong.
Note this is solved in contrib through the https://www.drupal.org/project/webp or https://www.drupal.org/project/imageapi_optimize_webp modules. for Drupal 9.x There is no Drupal 10 support for those modules (yet).
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | 3213491-nr-bot.txt | 150 bytes | needs-review-queue-bot |
| #20 | responsive_image_form.png | 138.21 KB | finnsky |
Issue fork drupal-3213491
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mherchelComment #3
mherchelComment #4
andypostWhat is required to fix it?
Comment #5
anybodySeems to me that webP only "works" on responsive images, not "native" images? Is this solvable in core at all as it means the result of the image style wouldn't be an
<img>anymore but a<picture>once someone selects to convert it?@mhercel: Could you find out which browsers in this liste are affected? https://www.drupal.org/docs/system-requirements/browser-requirements
Update:
Affected browsers:
See https://caniuse.com/webp

Comment #6
anybodyIf this might be unsolvable for image formats (at least for older browsers) in the meantime core should perhaps adopt an option to responsive image styles instead?
The option for image formats could be kept, if everyone knows what he's doing... or add a warning note to the image effect?
Comment #7
anybodyComment #8
saschaeggi@Mike you could check the existence of WebP images and serve them if supported. In Apache this could look similar to this:
.htaccess:
Comment #10
catchJust found this issue and had a think about it / did a bit of research.
https://www.drupal.org/project/webp supports fallbacks for webp images for browsers such as IE and Safari from contrib.
Drupal 10 has dropped support for all versions of IE, so we wouldn't add any new features to support IE specifically.
The latest couple of versions of Safari support webp, but only if you're on Big Sur or later. Catalina support is going to end in approx November 2022.
https://caniuse.com/webp
This means if we add some kind of polyfill via picture module for webp to core, it would fall under our browser support matrix for the next six months, but then be eligible for removal again - potentially still for 10.0.x if we end up releasing in December. Given there's not a viable patch here, I would tend to suggest that people use the contrib module for full webp support, and core's webp support will be sufficient for all modern browsers/supported OSs in approximately 6-9 months anyway. If there's another browser we support that can't handle webp yet though that might tip the balance towards adding this to 10.0.x
There's a further question whether we want to add a generic feature of allowing a format aspect to responsive image styles to address similar kinds of issues in the future - that might mean converting this to a task. This seems to be the only viable implementation here, we can't do it via pure image formats.
Comment #11
catchA further issue, due to an Apple bug, it's not possible to tell the relative usage of Catalina vs. Big Sur, because Big Sur reports itself as Catalina.
See the note and link on top of:
https://gs.statcounter.com/macos-version-market-share/desktop/worldwide
Comment #12
catchGoing to go ahead and close this as won't fix.
Core currently makes it possible to configure a site such that it won't work on IE / and <= Catalina MacOS, but you have to intentionally configure it to transform images into webp.
Catalina support will end in approximately six months, that's the only configuration that Drupal 10 supports since support for all versions of IE was already dropped, and it probably won't be supported by the time Drupal 10 is released. If we had an RTBC patch here and it was a year ago, it might have been worth providing the workaround for webp support on Catalina, but at this point we'd be adding what is essentially a feature to responsive image module that will be obsolete in a few months. The contrib webp project is already there for sites that want to support IE/Catalina.
Comment #13
anybodyThanks for caring, @catch! Totally agree with the decision due to the reasons given.
Comment #14
catchRe-opening this in light of #3202016: Let GDToolkit support AVIF image format which would add support for the AVIF format. AVIF also has (currently) spotty browser support, so it'd be viable to set up something like avif with a fallback to webp and then to the original image format.
Comment #15
andypostas a feature it goes to 10.1
Comment #16
andypostOTOH it means that 10.0 will be released with partial WEBP support
Comment #17
catchThis is true but webp has been in for several releases now, so it's not a new situation.
Comment #20
finnsky commentedAdded rude mvp.
For every breakpoint we may create set of images.
Should be autocomplete form or draggable table here.
Tested with patch from https://www.drupal.org/project/drupal/issues/3202016 so AVIF was onboard aswell.
Created 2 images styles with convert to formats:
- AVIF
- WEBP
Output was:
In my chrome browser was loaded AVIF, in other WEBP will appear or even single breakpoint failback
Comment #21
finnsky commentedComment #22
andyposthttps://caniuse.com/avif needs Safari 16.0 TP to be displayed
Overall great job! added some code-review
Main questions
- why 3 elements in
srcsetis enough to configure?- how to extend/override it and how to configure defaults if webp, jpg I bet is used on mostly all sites and how to fit afiv when patch accepted
Comment #23
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #24
martijn de witComment #25
rajab natshahSupport having this awesome feature
Modules like Image Styles too with Responsive Image Style
and contrib modules like Dynamic Responsive Image (or drimage) will switch from using Webp to the Drupal core solution.
Comment #26
rajab natshahCan projects start using this fix? If it will be committed to Drupal Core
10.1.xor10.0.xComment #29
gaëlgComment #30
andypostIt needs test and code comments - why 3 is enough!?
Comment #31
shiks commentedAnyone who is looking for some module in context of Drupal 10, can probably have a look here :
https://www.drupal.org/project/wpf
This module is good work around in context of fallback image support.
Comment #32
alan delval commentedMR !2690 don't work as expected.
Tested in Drupal 11.
Quote from MDN.
Hacky workaround for image fallbacks using AVIF patch:
As Drupal 11 uses WebP by default, using 3202016 allow us to use AVIF images, then we create a copy of every image style but using AVIF (convert). In the current theme make an alternative breakpoint group with copies for every fallback, for example, X-Large (WebP) and X-Large (AVIF), Medium (WebP) and Medium (AVIF), it's important to set a weight to avoid headaches. This way, every fallback will get its own
<source>with type. We need to select a single style for every breakpoint to get this working.output sample:
Comment #33
heddnLooking at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture for inspiration, I think what we want to generate is markup that looks like. Note in the first 2
<source/>tags, we add a type. In the 3rd, we can't because we have 2 types of images. The fallback is yet another image all together.OOTB, right now, we can generate markup that looks like:
What we don't have with core right now is the ability to have multiple image styles on a per image type. For the
sizesoption, I don't think that is critical. You can make it do what you want. (Please someone confirm this assumption).What we need to address is
Select a single image style.. We want to change that into multiple images. What I wonder is if we really want to do that or not. We really want that image "style" to have multiple image conversions. (Please confirm this assumption).Comment #34
andypostAs AVIF been commited would be great to resurrect the issue, ref #3202016: Let GDToolkit support AVIF image format
Comment #35
ambient.impactIt would be awesome to have a well defined way to define variants of image styles in core so contrib no longer needs to hack around this problem as is often the case. I think @heddn has the right idea, in that a single image style could generate multiple formats (or variants? Naming is hard) as opposed to the current limitation of a single image.
Comment #36
catchAdding #2986669: Split ImageStyle into the config entity and a separate event-based image processing service as related - that would probably help with image style variations idea.
However I'm not sure it should block this issue.
For AVIF and webp browser support is very high, so if we just fell back to the original image it would not be that bad IMO, would be a tiny fraction of requests.
Comment #37
catchAlso because browser support is so high, this issue probably doesn't block using AVIF by default in core, the server-side requirements are more of an issue, but there is #3510582: AVIF conversion with WEBP fallback open for that.