LinuxCommandLibrary

svgcleaner

Optimize and clean SVG files

TLDR

Optimize an SVG

$ svgcleaner [input.svg] [output.svg]
copy

Optimize an SVG multiple times
$ svgcleaner --multipass [input.svg] [output.svg]
copy

SYNOPSIS

svgcleaner [OPTIONS]
svgcleaner [-c config_file] [-p precision] [-s style_level] [...more_options...] input_file output_file

PARAMETERS

--help, -h
    Displays a help message and exits.

--version, -v
    Displays version information and exits.

--input , -i
    Specifies the input SVG file. Can also be provided as the first positional argument.

--output , -o
    Specifies the output SVG file. Can also be provided as the second positional argument.

--config , -c
    Specifies a JSON configuration file for detailed optimization settings, overriding command-line flags.

--force, -f
    Overwrites the output file if it already exists without prompting.

--pretty-print, -d
    Formats the output SVG with indentation for readability, rather than minifying it.

--precision , -p
    Sets the number of decimal places for numeric values (e.g., coordinates, dimensions) in the output.

--style-level , -s
    Controls how CSS styles are processed. Common levels include none, stylesheet (internal CSS), and inline (style attributes).

--apply-transforms
    Applies transformations (e.g., translations, rotations) directly to shape coordinates instead of keeping them as transform attributes.

--strip-comments
    Removes all XML comments from the SVG output.

--strip-metadata
    Removes metadata tags like from the SVG document.

--remove-unreferenced-ids
    Deletes IDs that are defined but not referenced elsewhere in the document (e.g., in ).

--remove-empty-containers
    Removes empty container elements like or that have no content.

--group-elements
    Attempts to group elements into tags where possible, which can sometimes aid in better compression or organization.

--ungroup-elements
    Attempts to ungroup elements from tags, potentially simplifying the DOM structure.

DESCRIPTION

svgcleaner is a powerful command-line tool designed to optimize Scalable Vector Graphics (SVG) files. Its primary purpose is to reduce the file size of SVG images by removing unnecessary data and applying various optimizations without compromising visual quality. This includes stripping out unused definitions, comments, metadata, and redundant attributes. It can also simplify paths, merge groups, optimize transforms, and remove default or empty elements.

By making SVG files more compact, svgcleaner helps improve website loading times and overall performance, making it an essential tool for web developers and designers working with vector graphics. It supports various levels of cleaning and can be configured via JSON files for fine-grained control over the optimization process.

CAVEATS

While svgcleaner is highly effective, users should always verify the output SVG, especially for complex or highly specific designs, to ensure no unintended visual changes occur. Extensive customization often requires understanding of its JSON configuration schema.

CONFIGURATION FILES

svgcleaner offers extensive customization through JSON configuration files. These files allow users to specify exactly which optimization passes to apply, set numerical precision for coordinates, control style processing, and fine-tune many other aspects of the cleaning process, providing granular control beyond command-line flags.

PERFORMANCE AND IMPLEMENTATION

Being written in Rust, svgcleaner is lauded for its speed and memory efficiency, making it well-suited for processing large batches of SVG files or integrating into automated build pipelines where performance is critical.

HISTORY

svgcleaner is an open-source project primarily written in Rust, a language known for its performance and safety. It was developed to provide a fast, efficient, and robust command-line solution for optimizing SVG files, catering to the growing need for optimized web assets and streamlined build processes. Its development aligns with the broader trend of static asset optimization in web development workflows.

SEE ALSO

svgo(1), inkscape(1), convert(1)

Copied to clipboard