Add fallback format support to responsive images

Created on 11 May 2021, over 3 years ago
Updated 30 January 2023, almost 2 years ago

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.

Feature request
Status

Needs work

Version

10.1

Component
Responsive image 

Last updated about 16 hours ago

Created by

🇺🇸United States mherchel Gainesville, FL, US

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The 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.

  • 🇳🇱Netherlands Martijn de Wit 🇳🇱 The Netherlands
  • 🇯🇴Jordan Rajab Natshah Jordan

    Support having this awesome feature

    Modules likeImage 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.

  • First commit to issue fork.
  • 🇫🇷France GaëlG Lille, France
  • 🇫🇷France andypost

    It needs test and code comments - why 3 is enough!?

  • 🇮🇳India shiks

    Anyone 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.

  • 🇲🇽Mexico Alan Delval

    MR !2690 don't work as expected.

    Tested in Drupal 11.

    1. Leaving one single item with - none shows error.
    2. Selecting two different image formats will merge both at the same srcset and the type attribute gets removed. This makes fallbacks useless.

    Workaround for second issue:
    Make individual <source> items for every image format option.

    Hacky workaround for image fallbacks using AVIF patch:
    As Drupal 11 uses WebP by default, using 3202016 Let GDToolkit support AVIF image format Needs work 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.

    material_base.inline_images.lg:
      label: 'Large'
      mediaQuery: 'all and (min-width: 1024px)'
      weight: 6
      mutipliers:
        - 1x
      group: material_base.inline_images
    material_base.inline_images.lg_alt:
      label: 'Large (alt)'
      mediaQuery: 'all and (min-width: 1024px)'
      weight: 5
      mutipliers:
        - 1x
      group: material_base.inline_images
    
    material_base.inline_images.md:
      label: 'Medium'
      mediaQuery: 'all and (min-width: 768px)'
      weight: 4
      mutipliers:
        - 1x
      group: material_base.inline_images
    material_base.inline_images.md_alt:
      label: 'Medium (alt)'
      mediaQuery: 'all and (min-width: 768px)'
      weight: 3
      mutipliers:
        - 1x
      group: material_base.inline_images
    ...
    
Production build 0.71.5 2024