96

I am unable to upload SVG image files to Stack Overflow. So I would like to know why it is blocked while other image formats are allowed. SVG images are useful for diagrams such as tree diagrams for algorithm discussions.

10
  • 6
    So just convert them to PNG. Problem solved. May 25, 2011 at 5:08
  • @TheEstablishment true, found online converter which works just fine from quick test I just did. Dec 3, 2012 at 10:02
  • 55
    Converting to a raster format defeats the purpose behind SVG which is scalability.
    – bgoodr
    Dec 7, 2012 at 5:58
  • 12
    In addition to @bgoodr - scalability and edit friendly format. Dec 1, 2014 at 14:06
  • 12
    @CodyGray and time wasted + information lost. Aug 15, 2015 at 18:49
  • You can convert the SVG to PNG with Internet Explorer or Microsoft Edge, as I explained here. But I agree that doing so isn't always good for the image quality and that SVG should be supported by Imgur. Mar 23, 2017 at 12:56
  • Also I've experienced that the site sometimes doesn't accept some image types, even if they are accepted (jpg, png, etc). They I have to download, re-compress them (f.e. convert sux.png sux-fixed.png or so) and then I can upload them. It is possible, that the real problem of the system that he doesn't like if I request him to download something from a third location, and not the actual file format.
    – peterh
    May 2, 2017 at 11:53
  • 3
    In my opinion, the site quality would be significantly improved with the ability to insert SVG pictures, and it doesn't really count if they are coming from the imgur or elsewhere.
    – peterh
    May 2, 2017 at 12:06
  • 1
    "Stack Exchange is not interested in building an image hosting service." Tell them how your really feel! LOL!
    – user148298
    May 20, 2020 at 3:44
  • just converting SVG to PNG is only a workaround. It is not a good solution ! Why ? Because an SVG can be changed without drawing. Because a SVG can be easily changed or corrected by another user. Because a SVG can be scripted to add dynamic colors. I'm not stupid and I know that SVG script can be malicious ! For this case, it is perhaps interesting to define a new Review queue to review SVG that contains script !
    – schlebe
    Sep 2, 2022 at 5:08

6 Answers 6

26

Stack Exchange supports image uploading by using an external service (imgur) which does not support vector images.

You'll need to convert your SVG into a raster format (PNG is probably the best choice for most vector images, but JPG, GIF, and even BMP are supported) before uploading them to the provided service.

Stack Exchange is not interested in building an image hosting service (which is why they use imgur) so it's unlikely that they will either create one, or encourage imgur to support vector image formats.

However, if you find a free vector image hosting service that can handle the load Stack Overflow puts on such a service, and provides an API that is as simple and easy to include as imgur's, then you might be able to convince Stack Exchange, Inc, to include support for your request.

Alternately you should consider going to imgur and requesting that they support vector image formats. It would be ideal if they did the hard work, because, using browser detection methods, they could also support browsers that don't have SVG support by rendering the image prior to display, or they could even render it on upload and only store the rendered image (thus not affecting their normal workflow since it's simply a raster image after upload and conversion).

Unless you make it trivial for Stack Exchange to include in their code, you are unlikely to get much support for your feature request.

5
  • That is asking end users of a product or service to supply an implementation, and constraining it further with vaguely defined words such as "easy". Not implementing it due to lack of browser support seems like a more reasonable rationale for saying "will not implement".
    – bgoodr
    May 26, 2011 at 20:28
  • Marking as answer as it was actually helpful. Thanks Adam.
    – bgoodr
    May 26, 2011 at 20:34
  • 20
    Stack Exchange has a special deal with imgur for custom image hosting. That makes them a paying customer, and apparently a pretty large one at that. If SE asked imgur for SVG support, they'd be a lot more likely to listen than if the request came from a random third party. Oct 9, 2014 at 16:31
  • 2
    @bgoodr I made an userscript that makes the image uploading easier and automatically converts SVG to PNG (by using CanvasDrawContext2D.drawImage()). Dec 1, 2014 at 14:08
  • 2
    Feature request thread for native imgur support: community.imgur.com/t/native-svg-support-on-imgur/11369 May 15, 2015 at 19:28
37
+50

While you can’t directly upload an SVG image, if you can host it somewhere else, you can still display it in your post using HTML like <img src="https://example.com/url/to/your/image.svg"/> (note - you need to use https:// for this to work).

For example, take this US map here from Wikimedia Commons

See this post for other examples of what you can do.

10
  • 7
    True, however the whole point of dedicated image hosting is preventing rot i.e. cases where the original host is down, then the image is lost forever. Dec 3, 2012 at 9:59
  • 7
    No I understand this, I just wanted to point out its still possible to show an svg. Would be nice if I could just paste the SVG code directly into a post (maybe you can but nothing I tried worked) but I could see the xml code taking up a lot of space on more complicated images
    – WebChemist
    Dec 3, 2012 at 10:01
  • 1
    Ideally, the StackExchange websites would provide the hosting services.
    – bgoodr
    Dec 7, 2012 at 5:59
  • 1
    Modern browsers will let you view inline svg xml right in the html, so its not like it would really need its own SO svg hosting service, they would just need to tweak the way posted svg code is shown... but easier said then done when you have to prevent XSS and that sort of stuff. Example of inline svg code in html: jsfiddle.net/webchemist/c6E7w
    – WebChemist
    Dec 7, 2012 at 6:23
  • 5
    Here, have a bounty for actually demonstrating a working method of using SVG on SE (even if it does require using a third-party image host). Dec 26, 2014 at 12:45
  • @WebChemist I accidentally edited this post when trying to correct a different one. Would you please roll-back my change? Thank-you Jan 19, 2016 at 10:00
  • 3
    I can't see the image at the moment—I just see a "broken image" icon. That's ironic, isn't it? Apr 28, 2016 at 2:22
  • @CraigMcQueen looks like wikimedia took down that map, fixed it with a different svg source
    – WebChemist
    Apr 29, 2016 at 0:15
  • 2
    Images only work with HTTPS on Stack Exchange, and I haven't found any HTTPS site to host images that supports SVG. Mar 23, 2017 at 12:53
  • @DonaldDuck gist.github.com supports SVG. The mobile view on Github doesn't display a preview, but the desktop view does. Eg, see here.
    – PM 2Ring
    Jan 13, 2020 at 15:21
10

These days you can paste the SVG markup into the HTML area in a Stack Snippet and it will render in the question or answer in most browsers.

<svg width="100%" height="150">
  <circle id = "middle" cx="50%" cy="50" r="25" fill="red">
  <animate id="a" begin="click" attributeName="fill" to="green" dur="2s"/>
  <set begin="a.end" dur="2s" attributeName="fill" to="green" dur="1s"/>
</circle>
</svg>
<div style="text-align:center">
<button onclick="document.getElementById('a').beginElement();">Click on the circle or click on me!</button>
</div>

If you upload the SVG as a file and reference it as an image (via the html <img> tag or as a CSS background-image etc) and the SVG contains javascript then the script won't run as SVG in an image context disables scripting.

Neither can you trigger animation via clicking e.g via begin="click" as I've done above as that SVG capability is also disabled when it's used as an image.

So despite the need to click Run to display the SVG you may need to do it this way instead.

4
  • 3
    Well, it works but requires the user to click a button. Far from being trivial or elegant. Better host it somewhere and use <img> markup as suggested by others. Jan 31, 2016 at 8:44
  • @ShadowWizard Good point, I've updated the answer to explain why you might want to do this, despite the other methods having such an advantage. Jan 31, 2016 at 8:52
  • Could you provide a working example? Apr 28, 2016 at 2:27
  • 4
    Stack Snippets aren't enabled on all sites, so this is even more limited than one might suppose. Apr 28, 2016 at 8:06
8

Images uploaded to Stack Overflow are hosted by imgur, and right now imgur doesn't support SVGs.

That said, it might be possible to just use them for binary storage and render the SVGs correctly here?

4

I think that there are currently 3 solutions to this question, but all of them are workarounds.

  1. Displaying an SVG image on browser and copy-pasting the displayed image into Stack Overflow post.

  2. Posting an SVG file on another site and adding an <img> HTML tag referencing this file as already explained on this question.

  3. Adding a stack snippet in adding SVG tags in HTML part.

All of them have their advandages and disavantages.

  1. With copy/paste the size of image is certainly bigger and the zooming display is certainly less efficient.

  2. The image is no longer displayed if the site that contains the SVG file is closed or has some problems.

  3. The image is only displayed after having clicked on the "Run code snippet" button.

I have read that implementing this new feature is not possible ... in 2011.

Now we are in 2020 and since 2011, StackOverflow has implemented automatic loading of image and code snippet.

Is it possible for the Stack Overflow developer team to add a new feature that allows adding of SVG images?

As developer/analyst, I see currently 3 solutions

  1. Adding a new feature to Stack Snippets so that their result can be displayed immediately without having to click on 'Run code snippet`.

  2. Allowing the use of SVG HTML tags directly on any post. I

  3. Allowing intelligent copy/paste of SVG HTML files, as already done with image files. The SVG file must be recorded on SO site and the process must be enough smart so that all javascript code present in SVG XML is automatically removed for security purpose.

There are certainly other possible solutions.

What can be done ... in 2020 ?

I think that this question is interesting for a lot of users. It is perhaps time for StackOverflow to implement this requested feature.

2

I'd imagine part of the reason is that Microsoft only support SVG since IE9, and most people use IE as their browser but have not upgraded to 9.

Examples of browser usage statitistics: http://www.w3counter.com/globalstats.php, http://gs.statcounter.com/#browser_version-ww-monthly-201004-201104-bar

Getting SVG support into IE8 would require a JS/flash plugin like SVG Web, which would cause significant bandwidth usage for an infrequent feature.

Either that or take the SVG markup and return a PNG. But that sounds like a lot of work for a Q&A website to undertake.

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .