18

Is there a way to compress SVG files?

I've been told that SVGz is the best way although I haven't found a converter to convert my SVG file.

Or is there an app where I can import an SVG file and export to SVGz?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
user1658756
  • 1,014
  • 5
  • 13
  • 27
  • Consider searching for "minify" options to compress the SVG's vector data before compressing its character encoding. `apt install minify` on a Debian/Ubuntu machine gives you [tdewolff's minify tool](https://github.com/tdewolff/minify), a nice command-line tool for that. – Adam Katz May 01 '18 at 20:06

2 Answers2

16

It uses GZip:

http://en.wikipedia.org/wiki/Scalable_Vector_Graphics#Compression

If you host SVG files on a web server, and have a properly configured web server, your web server will already compress files sent to the client, so compression of the SVG to SVGz is unnecessary (in fact undesirable).

To test whether your web server is configured correctly, refer to: How can I tell if my server is serving GZipped content?

Community
  • 1
  • 1
Andrew Alcock
  • 19,401
  • 4
  • 42
  • 60
  • With the system I'm using, I can't use GZip – user1658756 Sep 17 '12 at 08:15
  • And what is that system? or limitation that prevents you using GZip? – Andrew Alcock Sep 17 '12 at 08:17
  • I use [Squarespace](http://squarespace.com). I'm not sure if they use GZip, I'm going to check. If they do use GZip, I won't have to worry about SVGz will I? – user1658756 Sep 17 '12 at 08:22
  • If you place static SVG files on the server, and the web server is properly configured, it will already be sending the files using Gzip (as part of HTTP specification), so you don't need to do anything. Take a look at http://stackoverflow.com/questions/9140178/how-can-i-tell-if-my-server-is-serving-gzipped-content – Andrew Alcock Sep 17 '12 at 08:25
  • Yes, Squarespace does use GZip. Awesome, thanks! – user1658756 Sep 17 '12 at 09:04
  • 7
    Would you be able to clarify why this is undesirable? It seems to me that pre-compressing to SVGz has two advantage: reduced disk space usage, and reduced processing overhead for the server. – Slipp D. Thompson Jan 16 '13 at 14:37
  • @SlippD.Thompson: I view pre-compression as [premature optimization](http://programmers.stackexchange.com/a/80092): 1. If the file is manually compressed, then it has to be decompressed to edit then recompressed afterward. Inconvenient 2. Compression by the web server is extremely cheap - even a cheap server saturates a 1Gb ethernet before it runs out of CPU. And the web server probably caches this anyway 3. Generally disk is large compared to SVGs If however you have 1TB of SVGs, or the server is CPU bound on the compression, then by all means pre-compress. If not leave it to the transport – Andrew Alcock Jan 17 '13 at 00:16
  • 1
    To each their own then. I've long viewed classification as premature optimization only validated if the benefits are not definite, or if the cost to implement may not outweigh any benefit. In this case, I see the cost as nearly-nil (if a server can gzip compress quickly, my desktop can do it more thoroughly and faster), and the benefit as certainly always positive, even if small. – Slipp D. Thompson Jan 18 '13 at 23:26
  • 1
    I guess a clearer use-case I could see myself in is SVG/SVGz files in a local git repo, with many modifications made over time. A 50%+ storage savings over 100 revs adds up quickly. – Slipp D. Thompson Jan 18 '13 at 23:29
  • 2
    Actually, Git compresses data in its store already. If you precompress then Git won't be able to produce readable deltas and blame between versions. This might not be important if you store lots of SVGs that never change or if they are regenerated each time by a program. If they are manually edited, I'd suggest storing uncompressed and let Git optimize storage. See http://stackoverflow.com/questions/2869213/how-does-git-save-space-and-is-fast-at-the-same-time – Andrew Alcock Jan 19 '13 at 00:39
  • My shared host has a tiny amount of storage space and I have lots of SVG with embedded images. – jozxyqk Nov 22 '15 at 12:36
  • "your web server will already compress files sent to the client" - Andrew, is this true/relevant when the SVG is encoded in Base 64 within an HTML file? – f1lt3r Nov 25 '15 at 14:23
  • @AlistairMacDonald: Absolutely - HTTP allows for the client and server to compress all HTTP traffic at the protocol layer. Base64 compresses really well, so the on-the-wire data size will be very good. The in-memory will, of course, be less good ;) – Andrew Alcock Nov 25 '15 at 23:28
  • Thanks Andrew! Do you know if the GZIP compression is cached in Nginx? IE: the second time I request index.html, does Nginx check if the file has changed since it last sent the response and re-encode only if the file has changed? – f1lt3r Nov 30 '15 at 02:53
  • @AlistairMacDonald. I am not experienced on nginx, but a quick google turned up: http://killtheradio.net/technology/using-gzip_static-in-nginx-to-cache-gzip-files/ . This *implies* that caching is not implemented, but YMMV. In any case, GZip is not turned on by default - here's a tutorial: https://mattstauffer.co/blog/enabling-gzip-on-nginx-servers-including-laravel-forge – Andrew Alcock Dec 01 '15 at 02:52
  • Thanks @AndrewAlcock! GZIP is working on Nginx here. Shame it does not cache. – f1lt3r Dec 01 '15 at 14:20
  • 1
    Yeah I disagree with the "undesirable" comment quite a bit, while recognizing that there will be use cases where it really is. If editing of an SVG is completed (and there is the rub perhaps), it should be GZipped and extension changed to svgz. More and more we are moving to cloud delivery and paying for CPU cycles. It makes no financial sense to do that job with every GET when you can do it once in your Gulp/Grunt build script and be done with it. Per the spec, SVG clients must also be able to handle SVGZ. Modern browsers comply. – Geek Stocks Oct 17 '16 at 08:45
5

I thought it may be helpful to actually list out apps that can save in svgz format as per your request. Adobe Illustrator's Save As will allow you to save out to svgz format, as well as Inkscape, 7Zip, and if you are a fan of the command line and use windows this is a fairly simple solution GZip for Windows.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Zanderi
  • 907
  • 9
  • 15