I am using media library, media types and paragraphs to create content. There is a problem with rendering a .svg images.

How to reproduce:
To upload .svg image, use Svg Image module.
I had an image media type (Entity reference), attached to content type. I am using media library widget to add media. When I try to upload new image with .svg extension, it doesn't have thumbnail.

Order of reproducing:

1
2
3
4
5

Expected behavior:
Image should have style which render the .svg file.

Comments

NikolaAt created an issue. See original summary.

cilefen’s picture

reinchek’s picture

Assigned: Unassigned » reinchek
Status: Active » Needs review
StatusFileSize
new1.26 KB

I've reproduced the issue and can confirm what you noticed. The problem is that when media_library module try to build the svg preview creates the render array '#theme' => 'image_style', '#style_name' => 'media_library'.
The image style hook will try to build preview with given style's settings. But there isn't a solution in core to apply a style to an SVG file. Attached you can find a simple patch that i've applied to temporarily resolve the issue.

reinchek’s picture

nikolaat’s picture

It looks like that the patch works very well. Thank you!

nikolaat’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Thanks for filing this bug report and for fixing it. Bug fixing is very valuable. However in order to commit a bug fix we need an automated to test to prove that we've fixed the bug and ensure that we don't break it again in the future. For more information about writing tests in Drupal 8 see the following links:

  1. https://www.drupal.org/docs/8/testing
  2. https://api.drupal.org/api/drupal/core%21core.api.php/group/testing/8.7.x
+++ b/core/modules/media_library/src/Form/AddFormBase.php
@@ -314,11 +314,19 @@ protected function buildEntityFormElement(MediaInterface $media, array $form, Fo
+      $extension = pathinfo($thumbnail_uri, PATHINFO_EXTENSION);
+      if (strtolower($extension) === 'svg') {

Here we can check if the image style supports the URI using the \Drupal\image\Entity\ImageStyle::supportsUri() method rather than hardcoding svg. There's a good question about what we should do if we can't use the thumbnail formatter. I think displaying the full size image as the patch currently does might not be the right way to go. Not sure what is though.

There's a lot of discussion about SVG image style support in #2652138: Image styles do not play nicely with SVGs

reinchek’s picture

@alexpott you right when you say

displaying the full size image as the patch currently does might not be the right way to go

I agree with you. Maybe, as they are saying in the linked post, the right ways are at least 2 or 3:
- install and use ImageMagick instead of GD Toolkit which already supports the svg
- embed in core a dedicated image style which converts the vectorial image to raster one.
- extends the GDToolkit class, using external composer svg library, to also works with svg images.

reinchek’s picture

a.boutakaoua’s picture

I had the some error can you check the related issue

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

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

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.

bkosborne’s picture

Note this also affects the media library. Media entities use an image field type for the thumbnail, so that means it will try and output SVG media thumbnails using image field formatters, which typically run the source image through an image style. And, as we've already found, image styles don't work with SVGs out of the box unless your image processing library says it can handle them. When it cannot handle them, it hides the image output entirely but still allows contrib to override that behavior. This is what Lightning Media and SVG Image module do. They detect if an SVG image is trying to be output through an image style, and if so, allow it.

phenaproxima’s picture

Category: Bug report » Feature request
Issue tags: -svg image, -media library, -widget +svg

I'm going to mark this as a feature request for now, because core doesn't really support SVGs properly and Media Library stays within the boundaries of that assumption.

phenaproxima’s picture

carolpettirossi’s picture

The patch doesn't seem to work with the Library. When I upload an SVG image it generates the thumbnail, however, when it goes back to the library the image is empty.

I had to use the combination of this patch + https://www.drupal.org/project/media_thumbnails_svg

liam morland’s picture

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

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

luksak’s picture

The patch works for me.

Regarding the comment in #16: This only fixes svgs in in create media form in the media library widget. It doesn't fix the display of the thumbnails for existing media in the widget. For that you'll need media_thumbnails_svg as you already figured out.

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.

johnpitcairn’s picture

Note if you use a dedicated media type for SVGs (ie"logo" in my case), you can set the image formatter to output "original image" for all view modes, and that will display just fine in the media library and widget.

szato’s picture

@Lukas von Blarer

you are right.

Using the patch from #3 solved the issue on Drupal core 10.2.7

prashant.c’s picture

StatusFileSize
new26.49 KB
new34.02 KB
new20.17 KB
new41.42 KB

This is a major issue while using SVG images, I faced this while working with SVG images in one of the projects which is using Drupal 10.3.2, but this exists in later versions also. SVG images do not at all appear on the following:

  1. Media library Media library
  2. Media field Media field
  3. Media entity listing page Media entity listing
  4. Media render (this in on node page) Media render
prashant.c’s picture

The path submitted in #3 resolves the issue only for the Point 1 (Media library) mentioned in #25

bronismateusz’s picture

It helped me to install this module: svg_image
Now the thumbnails display correctly in the admin panel.

prashant.c’s picture

I came across this comment https://www.drupal.org/project/drupal/issues/3028868#comment-13812371, which highlights that the Drupal image module does not support SVGs for a variety of reasons. Given this, it is unclear whether the current issue is still relevant.

Consequently, it appears that utilizing the SVG Image module might be a more suitable alternative, as it offers a widget and formatter for handling SVG fields.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.