Problem/Motivation

When I apply an image style in a field of type media image, I got an empty field if the image is an svg.

Steps to reproduce

  1. Install latest version of Drupal 11.x using the standard profile.
  2. Add an image media type field to the article content type.
  3. Apply an image style to the image field.
  4. Create a node of type article and add an svg image to the image field and save the node.

Proposed resolution

Apply the image theme to the svg images and set the height and width of the image style as attributes.

Remaining tasks

  • Apply the patch to the MR
  • Add test coverage
  • Make the pipeline run green and ensure that the test-only test fails.
  • Code Review

User interface changes

N/A

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Issue fork drupal-3091740

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

a.boutakaoua created an issue. See original summary.

a.boutakaoua’s picture

Status: Active » Needs review
a.boutakaoua’s picture

Assigned: Unassigned » a.boutakaoua

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.

nelslynn’s picture

Using Drupal 8.8.2, this patch produces this error:

ParseError: syntax error, unexpected 'protected' (T_PROTECTED) in Composer\Autoload\includeFile() (line 167 of /var/www/html/web/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php)
#0 /var/www/html/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile('/var/www/html/w...')
#1 [internal function]: Composer\Autoload\ClassLoader->loadClass('Drupal\\media\\Pl...')
#2 /var/www/html/web/core/lib/Drupal/Core/Field/FormatterPluginManager.php(113): spl_autoload_call('Drupal\\media\\Pl...')
#3 /var/www/html/web/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php(205): Drupal\Core\Field\FormatterPluginManager->getInstance(Array)
#4 /var/www/html/web/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php(236): Drupal\Core\Entity\Entity\EntityViewDisplay->getRenderer('field_logo_imag...')
#5 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(351): Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array)
#6 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(293): Drupal\Core\Entity\EntityViewBuilder->buildComponents(Array, Array, Array, 'default')
#7 /var/www/html/web/modules/contrib/paragraphs/src/ParagraphViewBuilder.php(18): Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array)
#8 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(250): Drupal\paragraphs\ParagraphViewBuilder->buildMultiple(Array)
#9 [internal function]: Drupal\Core\Entity\EntityViewBuilder->build(Array)
#10 /var/www/html/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(100): call_user_func_array(Array, Array)
#11 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(781): Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_ren...', 'silenced_deprec...', 'Drupal\\Core\\Ren...')
#12 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(372): Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array)
#13 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#14 /var/www/html/web/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#15 /var/www/html/vendor/twig/twig/src/Environment.php(497) : eval()'d code(84): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#16 /var/www/html/vendor/twig/twig/src/Template.php(455): __TwigTemplate_be25d8aaa05e74eb416049f0b746c55ba082874d958ba718c7a3b8bff56d76b7->doDisplay(Array, Array)
#17 /var/www/html/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#18 /var/www/html/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#19 /var/www/html/web/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#20 /var/www/html/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/u...', Array)
#21 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('field', Array)
#22 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(444): Drupal\Core\Render\Renderer->doRender(Array)
#23 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#24 /var/www/html/web/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#25 /var/www/html/vendor/twig/twig/src/Environment.php(497) : eval()'d code(81): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#26 /var/www/html/vendor/twig/twig/src/Template.php(455): __TwigTemplate_12ab449b35bb4c78ff755c43b5db8d6c88b212f4b3d72ee57ef559ddeedf91d3->doDisplay(Array, Array)
#27 /var/www/html/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#28 /var/www/html/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#29 /var/www/html/web/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#30 /var/www/html/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/u...', Array)
#31 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('node', Array)
#32 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#33 /var/www/html/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(226): Drupal\Core\Render\Renderer->render(Array, false)
#34 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(573): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
#35 /var/www/html/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(227): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#36 /var/www/html/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(117): Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#37 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#38 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#39 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#40 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(156): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent))
#41 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#42 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#43 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#44 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#45 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#46 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#47 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#48 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#49 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(694): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#50 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#51 {main}

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.

phenaproxima’s picture

Component: media system » image.module

I think this is out of the media system's scope; core's image handling doesn't really support SVGs, so I'm moving this into that component.

akalam’s picture

The patch assumes the media entity has a field called "field_media_image" and can not be the case.

feuerwagen’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative

As the available patch only handles a very specific case with a media entity and not the root problem in the image module, setting to needs work.

feuerwagen’s picture

#2652138 apparently only fixed the image module trying to apply an image style to SVGs (and sometimes failing in nasty ways) but didn't make sure that SVGs are still displayed.

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
vipin.mittal18’s picture

This patch won't work as it is specifically for field_media_image field.

I still not figured out any alternative for this but there is a way from manage display, how we wan't SVG to be render without applying image style.

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.

Thangaraj Moorthi’s picture

My drupal version is 9.4.8
The given patch (https://www.drupal.org/files/issues/2019-11-01/media_library_not_render_...) is not working for me.
Is there any fix to doing image style for the svg format ?
This is the error I'm facing,
Image scale and crop failed using the gd toolkit on public://2022-11/banner_01.svg (image/svg+xml, 2500x1178)

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

kolesnikoff’s picture

Reroll the patch for Drupal 9.5.9.

avpaderno’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests, +Needs Review Queue Initiative

The issue summary could use some updating.

Also as a bug will need a test case.

Have you checked out https://www.drupal.org/project/svg_image

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

simeonkesmev’s picture

StatusFileSize
new0 bytes
new3.27 KB

Change to the patch avoiding hardcoded file names and fixing deprecated functiontion call

_utsavsharma’s picture

StatusFileSize
new654 bytes
new3.26 KB

Fixed failures in #21.

avpaderno’s picture

Tests have not been added.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

fwust’s picture

StatusFileSize
new3.28 KB
new812 bytes
guicom’s picture

StatusFileSize
new3.15 KB

Update patch for Drupal 10.3.x

jennypanighetti’s picture

StatusFileSize
new53.35 KB

I'm still having issues with this in 10.4, but I'm not getting any errors and the patch applies, so I don't know where to go from here.

PNGs I use display fine, but SVGs are showing as 0x0, even though they have the image width and height set in the HTML. I have to specify a width in the CSS to get it to show; max-width and width:auto are not enough.

avpaderno’s picture

Assigned: a.boutakaoua » Unassigned
dewancodes’s picture

Image styles are not generating images for svg types even on Drupal 11.2

It says "Error generating image.".

/sites/default/files/styles/left_right_content_image_tags_650x550/public/2025-10/16-oct-StandardizeMembership-With-a-Universal-Workflow--svgg.svg?itok=NyDM9Yij

ironnuts’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

Applied IS template to the IS, re-structured the IS with minimal deletions and re-writing for clarity.

mayurgajar’s picture

Assigned: Unassigned » mayurgajar

mayurgajar’s picture

Assigned: mayurgajar » Unassigned

MR: https://git.drupalcode.org/project/drupal/-/merge_requests/14003
It still need to fix phpstan issues
------ -----------------------------------------------------------------------
Line core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailForm
atter.php
------ -----------------------------------------------------------------------
223 Method
Drupal\media\Plugin\Field\FieldFormatter\MediaThumbnailFormatter::ren
derSvgTheme() has no return type specified.
🪪 missingType.return
------ -----------------------------------------------------------------------

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.

sophie.sk made their first commit to this issue’s fork.

sophie.sk’s picture

Status: Needs work » Needs review

Rerolled the patch against Drupal 11.4.4: https://git.drupalcode.org/project/drupal/-/merge_requests/16388

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.25 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.