I found that it's possible to upload SVG images to Drupal 7 (7.38) and they can even be rendered as the original image, but any image styles don't work (obviously since gd doesn't support SVG). Of course there is the imagemagick module that one could try to use and possibly patch, but it didn't seem to work out of box either. I thought that it would be great to add basic SVG support to Drupal core since many browsers support them and they are great for todays world with different screen sizes etc. By basic support I mean that the SVG images would work with styles, not applying the ones that can't be done without gd or similar image manipulation tool, but simply bypassing the styles and applying for example width and height to the img tag directly when theming the images.

Issue fork drupal-2539478

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

aleksijohansson’s picture

Status: Active » Needs review
StatusFileSize
new3.64 KB

The attached patch simply allows SVG images to function within the site without causing errors. It also allows Scale effect (image styles) to be used on SVG images and allows administrators to define the default height and width of SVG images uploaded to the site. Information about these is also added to the admin UI.

aleksijohansson’s picture

StatusFileSize
new3.87 KB

The previous patch was missing one function checking bypass for SVG images. New patch attached.

cilefen’s picture

Please see if this is an issue in Drupal 8. If it is, then move this to version 8.0.x where it can be fixed, then backported.

j-lee’s picture

Patch #3 is working with Drupal 7.39 (and Media 2.0-beta1).

There is an issue for Drupal 8:
#2541480: Allow SVG files in the image widget

By GD (our default image toolkit) does not support SVG. So you cannot generate image styles of them. I think we should close this in favor of #1014816: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only).

So this issue should a feature request for Drupal 8 with an backport to Drupal 7 or SVG files should defined as files and not as images (Working in Drupal 8 beta 15 with patched #1014816 ).

ravibarnwal’s picture

#2 patch is not working with core image field of content type. It is showing error at the time of upload .svg type image. So I am adding new patch for above issue.

heddn’s picture

nsshrinivasan’s picture

The patch mentioned in #6 does not work with Drupal 7.44. Any pointers on a more recent working patch?

I get the following error when I try to patch in Mac OSX....

$ patch < drupal-add_basic_svg_support-2539478-3-D7.patch
can't find file to patch at input line 5
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/modules/image/image.admin.inc b/modules/image/image.admin.inc
|index cebe894..11125df 100644
|--- a/modules/image/image.admin.inc
|+++ b/modules/image/image.admin.inc
--------------------------
File to patch: /modules/image/image.admin.inc
/modules/image/image.admin.inc: No such file or directory
Skip this patch? [y] n
File to patch: modules/image/image.admin.inc
patching file modules/image/image.admin.inc
can't find file to patch at input line 22
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/modules/image/image.field.inc b/modules/image/image.field.inc
|index 6d1867c..822ad2e 100644
|--- a/modules/image/image.field.inc
|+++ b/modules/image/image.field.inc
--------------------------

cilefen’s picture

You must be in Drupal's root directory when you apply a core patch.

DrPal43’s picture

I got SVGs working by going to configurations->text formats->configure full html and disabling "Convert line breaks into HTML (i.e.
and

)". I then pasted the entire

CODE GOES HERE

the body field as Source and it worked. The only issue is that you have to disable the Convert everything you push code up to the server.

DrPal43’s picture

DrPal43’s picture

rreiss’s picture

#6 works great.
It does look like a workaround, and not a core valid solution, but I have no complaints! :)

I had to change it a little bit to support custom SVG image effects (we are using it to change the SVG fill color for example), the change to the patch is only on the image.module file:

  $is_svg = ($image->info['extension'] == 'svg');
  foreach ($style['effects'] as $effect) {
    if ($is_svg && empty($effect['svg_support'])) {
      continue;
    }

    image_effect_apply($image, $effect);
  }

And the custom image effect looks like this (note the svg_support attribute I created):

/**
 * Implements hook_image_effect_info().
 */
function svg_effects_image_effect_info() {
  $effects = array(
    'svg_effects_fill' => array(
      'label' => t('SVG fill'),
      'help' => t('Change the SVG fill color.'),
      'effect callback' => 'svg_effects_fill_effect',
      'form callback' => 'svg_effects_fill_form',
//      'summary theme' => 'image_scale_summary',
      'svg_support' => TRUE
    )
  );

  return $effects;
}

Thanks :)

q0rban’s picture

q0rban’s picture

Title: Add basic SVG support » Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only)
nwom’s picture

It would be great if the .ico extension also worked

Kuàilè de xīn’s picture

Hi,

Can somebody commit this to drupal core since #6 is working as expected!
Because everytime we update the core, this patch is lost and have to manually apply it all again.
It will be great if somebody can commit this to drupal core

Thanks in advance.

Kuàilè de xīn’s picture

Issue tags: +Drupal 7.65 target
joseph.olstad’s picture

Issue tags: -Drupal 7.65 target +Drupal 7.66 target
Kuàilè de xīn’s picture

Status: Active » Fixed
Issue tags: -Drupal 7.66 target +Drupal 7.67 target
feuerwagen’s picture

Status: Fixed » Needs review

Setting proper issue status. It's certainly not fixed, but if anybody wants to RTBC this … (haven't reviewed the patch myself).

damienmckenna’s picture

Bumping to 7.68.

shivachevva’s picture

Updated the patch. This patch working in latest Drupal core.

shivachevva’s picture

Removed space in the patch file name.

shivachevva’s picture

Updated the patch. This patch working in latest Drupal 7.67 core.

mustanggb’s picture

Issue tags: -Drupal 7.68 target +Drupal 7.69 target
mustanggb’s picture

Issue tags: -Drupal 7.69 target +Drupal 7.70 target
chrisgross’s picture

I don't know if this patch is still needed, but the latest no longer applies, so here's a re-roll for anyone still on 7.x.

izmeez’s picture

This may be nit-picking, not only is the title of this issue rather long but it also doesn't seem to be entirely accurate or provide clarity on what the issue and patch do.

"Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only)"

Looking at the patch it appears that the allowed extensions are still hard coded and the only difference is the addition of svg formats.

Would the following title be more suitable? "Add svg to image fields to match all extensions image toolkit supports"

aleksijohansson’s picture

You are very correct @izmeez that the patch does not match the title. It was just a quick workaround at the time. I would still argue that the title of the issue is relevant for what should be achieved here.

izmeez’s picture

Added this issue to #3216978: [meta] Priorities for 2021-12-01 release of Drupal 7 in anticipation of that issue being rolled over into a new issue of priorities for the next release of D7 core.

poker10’s picture

I also agree with @izmeez that the title of this issue is a bit misleading. If we want to allow any extension, then we need to implement the backport of the #1014816: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only). This will ensure that all image toolkits will be able to define supported extensions and also handle them in particular functions.

Hardcoding the SVG into many places does not seems a good idea to me, mainly because GD does not support SVG, so it rather needs to be added to another image toolkit (see D8/9 status of the SVG support).

If we go this way, image toolkits will be able to allow different extensions (as in D8) and then the followup issues can extend support for these extensions in the tookits (for example like this: #3179554: [D7] Add webp support for core image module.

I have attached a patch which should backport #1014816: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only), but still preserves the default extensions if there is another image toolkit in use, which does not currently implements the new get_supported_extensions() function. Let's see what testbot says.

mgifford’s picture

Version: 7.x-dev » 10.3.x-dev
Issue tags: +sustainability, +avif, +webp

I don't see this getting into D7, can we get it in d10 or 11?

AVIF & WebP look ready to roll though:
https://caniuse.com/?search=avif
https://caniuse.com/?search=webp

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

poker10’s picture

Title: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only) » [D7] Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only)
Version: 10.3.x-dev » 7.x-dev
Status: Needs work » Needs review

@mgifford this is a D7 backport issue. The original issue is here: #1014816: Allow image fields to use any extensions the current image toolkit supports (instead of hard-coding jpg, png and gif only) and was fixed 6 years ago, so it should be in D10+ already.

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

avpaderno’s picture

Since the last patch no longer applies to the 7.x branch, I created a MR.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.