0.02
Low commit activity in last 3 years
No release in over a year
SVG optimization based on Node's SVGO
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

SvgOptimizer

Tests Gem Gem

Some small optimizations for SVG files.

Installation

gem install svg_optimizer

Or add the following line to your project's Gemfile:

gem "svg_optimizer"

Usage

# Optimize an existing String.
xml = File.read("file.svg")
optimized = SvgOptimizer.optimize(xml)

# Optimize a file - it will override the original file.
SvgOptimizer.optimize_file("file.svg")

# Optimize a file - it saves a copy to "optimized/file.svg".
SvgOptimizer.optimize_file("file.svg", "optimized/file.svg")

You may have a need to optimize a trusted SVG document with entities that need to be expanded. Only if you are sure the file is trusted, you may enable this additional entity processing by passing the trusted: keyword argument:

# Optimize an existing trusted String which requires entity expansion.
xml = File.read("file.svg")
optimized = SvgOptimizer.optimize(xml, trusted: true)

# Optimize a trusted file which requires entity expansion - it will override the original file.
SvgOptimizer.optimize_file("file.svg", trusted: true)

You can specify the plugins you want to enable. The method signature is:

SvgOptimizer.optimize(xml, plugins)
SvgOptimizer.optimize_file(input, output, plugins)

where plugins is an array of classes that implement the following contract:

class MyPlugin < SvgOptimizer::Plugins::Base
  def process
    xml.xpath("//comment()").remove
  end
end

The default list of plugins is stored at SvgOptimizer::DEFAULT_PLUGINS. To use your new plugin, just do something like this:

SvgOptimizer.optimize(xml, SvgOptimizer::DEFAULT_PLUGINS + [MyPlugin])

Maintainer

Contributors

Contributing

For more details about how to contribute, please read https://github.com/fnando/svg_optimizer/blob/main/CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/svg_optimizer/blob/main/LICENSE.md.

Code of Conduct

Everyone interacting in the svg_optimizer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.