Problem/Motivation

When SVG files are uploaded to an image field and the field is configured to use a Responsive Image formatter, the rendered output breaks.

Image styles do not generate derivatives for SVG files.

As a result, when a Responsive Image style is applied, Drupal attempts to generate responsive image source URLs for SVG files, which leads to broken derivative URLs in the rendered markup.

This issue commonly occurs when using the svg_image contributed module (or similar modules that allow SVG uploads in image fields), but the underlying problem is that the Responsive Image formatter does not defensively handle unsupported image types.

Currently:

SVG files can be uploaded to image fields.

Responsive Image styles can be applied to those fields.

The rendered output includes invalid derivative URLs for SVG images.

The SVG image therefore appears broken on the frontend.

Additionally, since SVG files often contain an internal <title> element for accessibility, there is an opportunity to improve UX by optionally allowing the system to use that title and bypass the required alt text field when appropriate.

Steps to reproduce

Install Drupal core (tested on 10.x/11.x).

Install and enable the svg_image contributed module (or any module that allows SVG uploads in image fields).

Create or edit a content type and add an image field.

Configure the field display to use a Responsive Image formatter.

Configure a Responsive Image style for the field.

Upload an SVG file to the image field.

View the rendered entity.

Result:

The responsive image <source> elements reference derivative URLs that do not exist.

The SVG image renders incorrectly or appears broken.

Expected result:

SVG images should either:

Be rendered without responsive image derivatives, or

Fall back to the original file URL, or

Use a defined fallback image style.

Proposed resolution

Add defensive handling in the Responsive Image formatter to detect unsupported image types (such as SVG) and avoid generating responsive image derivative URLs for them.

Possible approaches:

  • Detect SVG file extensions or MIME types and bypass responsive derivative generation.
  • Render the original file URL directly when image styles do not support the file type.
  • Allow configuration of a fallback image style for unsupported formats.

Optional enhancement:

Detect a valid <title> element within the SVG file and optionally use it as the alt text, allowing sites to relax the required alt text constraint for SVG images where appropriate.

Note:

When using the svg_image_responsive submodule (provided by svg_image), this issue does not occur because it replaces the Responsive Image formatter. However, adding defensive checks in core would improve robustness and prevent broken output even when contrib workarounds are not installed.

Remaining tasks

  • Determine whether the fix belongs in core Responsive Image formatter or should remain in contrib.
  • Implement defensive detection for unsupported image types.
  • Add automated test coverage for SVG handling in responsive image formatting.
  • Confirm no regressions for supported raster image formats (JPG, PNG, WebP, etc.).
  • Review and testing.

User interface changes

None required for the core defensive fix.

If the optional SVG title-to-alt feature is implemented, it may introduce minor UI adjustments to allow configuration of that behavior.

Introduced terminology

None.

API changes

No new public APIs are required.

The change would introduce additional internal validation/handling in the Responsive Image formatter to prevent generation of invalid derivatives for unsupported image types.

The change should be backward compatible and only affect cases where unsupported image formats (such as SVG) are used with Responsive Image styles.

Issue fork drupal-2957924

Command icon 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

crow created an issue. See original summary.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mgifford’s picture

Extra bonus would be if it looked for a good title within the SVG and allowed you to skip the required alt text.

jhedstrom’s picture

Extra bonus would be if it looked for a good title within the SVG and allowed you to skip the required alt text.

That would be pretty slick, but also probably a feature request against the image field (since it would benefit non-responsive images too.)

djsagar’s picture

Status: Active » Needs review

Hi,

Try this module hope it's working for you https://www.drupal.org/project/responsive_svg

Thanks!

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

azinck’s picture

Here's a patch. It displays the original file if the image style doesn't support SVG. This is different from the approach used in #2652138: Image styles do not play nicely with SVGs on of hiding the image by default, but I don't fully understand why that decision was made. Also, doing something similar here would require more substantial refactoring of the responsive_image module's API because of the way it delegates grabbing the image styles to _responsive_image_image_style_url().

Anyhow, this is a first pass. Feedback welcome.

smustgrave’s picture

Version: 9.4.x-dev » 10.1.x-dev
Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 200

Thank you for the patch.

This issue will need an issue summary update following the standard template I believe.

Also could use a test case.

Personally I wonder if such a feature should be configurable.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mahmoud barhouma’s picture

Thank you for the patch. #11

djdevin’s picture

Category: Feature request » Bug report
Status: Needs work » Needs review
StatusFileSize
new941 bytes

I feel this is a bug and not a feature request?

_responsive_image_image_style_url is deprecated in 11.3, code moved to ResponsiveImageBuilder

smustgrave’s picture

Status: Needs review » Needs work

Sure, possible to get test coverage though?

Also will need an issue summary for the standard template

alorenc made their first commit to this issue’s fork.

alorenc’s picture

Version: 11.x-dev » main

alorenc changed the visibility of the branch 2957924-have-responsive-image to hidden.

saidatom made their first commit to this issue’s fork.

saidatom’s picture

Assigned: Unassigned » saidatom
saidatom’s picture

Assigned: saidatom » Unassigned
Status: Needs work » Needs review

herved made their first commit to this issue’s fork.

saidatom’s picture

Steps to reproduce:

  1. Install svg_image contrib (or any module that allows SVG in image fields)
  2. Configure a responsive image style on an image field
  3. Upload an SVG file
  4. View the rendered entity — without the patch, the responsive image sources will point to broken derivative URLs

@herve001 also noticed that ImageUrlFormatter::viewElements() has the same unguarded buildUrl() call

herved’s picture

Regarding svg_image, I believe this patch is not necessary when using svg_image_responsive (submodule of svg_image). It can be enabled and replaces the responsive_image formatter class.

However, the proposed changes introduce a defensive check against unsupported image types, which seems like a good idea nonetheless.

claudiu.cristea’s picture

Issue tags: -Needs tests
smustgrave’s picture

Status: Needs review » Needs work

Have no reviewed but summary is incomplete, needs to use the issue template fully.

sivaji_ganesh_jojodae’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update
saidatom’s picture

Status: Needs work » Needs review
smustgrave’s picture

Think this should get sub-maintainer sign off.

smustgrave’s picture

Bumping to framework now.

anybody’s picture

I agree with #28 - and shouldn't we try implementing a solution in core for SVG in general, not only for responsive images but for image styles in general? Replacing svg_image module.

Is there an issue for that yet, which I didn't find? Then we should link it here.

I'm thinking of something like a skip for image styles (via code like here or as setting or as a "Skip" image effect that skips the following effects - all if first - if the image is an SVG or GIF)

anybody’s picture

Sorry the issue title tricked me. Looking into the code this also covers image styles already, not like I've written in #35 but maybe good enough for core?

anybody’s picture

Title: Have responsive image module ignore svg » Apply (responsive) image styles only on supported types (leave e.g. svg, gif) untouched
natts’s picture

Title: Apply (responsive) image styles only on supported types (leave e.g. svg, gif) untouched » Apply (responsive) image styles only on supported types (i.e. leave SVG untouched)