PHPackages                             mathiasreker/php-svg-optimizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. mathiasreker/php-svg-optimizer

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

mathiasreker/php-svg-optimizer
==============================

php-svg-optimizer is a PHP library designed to optimize SVG files by applying various transformations and cleanup operations.

8.5.9(1mo ago)3411.1k↑21.9%62MITPHPPHP ^8.3CI failing

Since Aug 27Pushed 1w ago1 watchersCompare

[ Source](https://github.com/MathiasReker/php-svg-optimizer)[ Packagist](https://packagist.org/packages/mathiasreker/php-svg-optimizer)[ Docs](https://github.com/mathiasreker/php-svg-optimizer)[ RSS](/packages/mathiasreker-php-svg-optimizer/feed)WikiDiscussions develop Synced 2w ago

READMEChangelog (10)Dependencies (135)Versions (97)Used By (2)

PHP SVG Optimizer
=================

[](#php-svg-optimizer)

[![Packagist Version](https://camo.githubusercontent.com/94757a2d94f727fcd028e3311ac6d55f4d0c18302f1bcf0a7b170f090dd0e5e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e737667)](https://packagist.org/packages/MathiasReker/php-svg-optimizer)[![Packagist Downloads](https://camo.githubusercontent.com/06bc80a02e46ba6aded0b15fb59061c8ff2a2da5beeb49e4f3247c56850e4a73/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e7376673f636f6c6f723d253233666630303766)](https://packagist.org/packages/MathiasReker/php-svg-optimizer)[![CI status](https://github.com/MathiasReker/php-svg-optimizer/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/MathiasReker/php-svg-optimizer/actions/workflows/ci.yml)[![Codacy Security Scan](https://github.com/MathiasReker/php-svg-optimizer/actions/workflows/codacy.yml/badge.svg)](https://github.com/MathiasReker/php-svg-optimizer/actions/workflows/codacy.yml)[![Aikido Security Score](https://camo.githubusercontent.com/1fde4ae9a8a4ba0b6a91576e667346182e3e9449a3e71e2c278f2da553a7230d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f41696b69646f5f53656375726974792d3130302532352d627269676874677265656e)](https://intel.aikido.dev/packages/packagist/mathiasreker/php-svg-optimizer)[![PHPStan Level](https://camo.githubusercontent.com/0d535d730dbb19a2d58b83b72f94f489ac21fa551969481445f2d8fea441eeaf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d4c6576656c2532306d61782d626c7565)](#)[![Type Coverage](https://raw.githubusercontent.com/MathiasReker/php-svg-optimizer/develop/dev/artifacts/type-coverage.svg)](#)[![Code Coverage](https://raw.githubusercontent.com/MathiasReker/php-svg-optimizer/develop/dev/artifacts/coverage.svg)](#)[![Mutation Score](https://raw.githubusercontent.com/MathiasReker/php-svg-optimizer/develop/dev/artifacts/mutation-score.svg)](#)[![Mutation Coverage](https://raw.githubusercontent.com/MathiasReker/php-svg-optimizer/develop/dev/artifacts/mutation-coverage.svg)](#)[![Tests](https://raw.githubusercontent.com/MathiasReker/php-svg-optimizer/develop/dev/artifacts/tests.svg)](#)[![Assertions](https://raw.githubusercontent.com/MathiasReker/php-svg-optimizer/develop/dev/artifacts/assertions.svg)](#)[![Filesize](https://raw.githubusercontent.com/MathiasReker/php-svg-optimizer/develop/dev/artifacts/filesize.svg)](#)[![Contributors](https://camo.githubusercontent.com/48e0d0c7323099784f888e726f8d6f052384d396c250ed80729f77a87c50c60d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e737667)](https://github.com/MathiasReker/php-svg-optimizer/graphs/contributors)[![Forks](https://camo.githubusercontent.com/8ddcf3e849b912384c2775422e0016f8766f5a3dcd5af00bba61e410378abffc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e7376673f7374796c653d666c6174)](https://github.com/MathiasReker/php-svg-optimizer/network/members)[![Stargazers](https://camo.githubusercontent.com/c5c5be0250453f4c46e048ca67f1e906af9fdb1f51c4ea28254c5a6b034822c5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e7376673f7374796c653d666c6174)](https://github.com/MathiasReker/php-svg-optimizer/stargazers)[![Issues](https://camo.githubusercontent.com/4a6bc450ce803ae8bcaa97439cd2a2005700350015d9c2334ad4ea669fbcd26a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e737667)](https://github.com/MathiasReker/php-svg-optimizer/issues)[![MIT License](https://camo.githubusercontent.com/288961bfec25310f87ee1380a5d5bbdccac3997169a063fdb7af383da0c198ae/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e737667)](https://github.com/MathiasReker/php-svg-optimizer/blob/develop/LICENSE)[![PHP](https://camo.githubusercontent.com/647fce7bbbf1cd70310c75731e04fc106b7dc5e24fef9644adc9ad4630c847ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d3737374242343f7374796c653d666c6174266c6f676f3d706870266c6f676f436f6c6f723d7768697465)](#)

`php-svg-optimizer` is a lightweight PHP library designed to **optimize, minify, and sanitize** SVG files. It applies various transformations and cleanup operations while ensuring compliance with SVG 2.0 specifications. The resulting SVGs remain visually identical to the original but are smaller, cleaner, and safer.

### Versions &amp; Dependencies

[](#versions--dependencies)

VersionPHPDocumentation^8.7^8.3[current](https://github.com/MathiasReker/php-svg-optimizer)### Requirements

[](#requirements)

- `ext-dom`: Required PHP extension for XML handling.
- `ext-libxml`: Required PHP extension for XML error handling.
- `ext-mbstring`: Required PHP extension for multibyte string handling.

### Installation

[](#installation)

To install the library, run:

```
composer require mathiasreker/php-svg-optimizer
```

[![Demo GIF](dev/artifacts/demo.gif)](dev/artifacts/demo.gif)

### Using the Library

[](#using-the-library)

You can use this library in two main ways:

1. **Command-Line Interface (CLI):** Run the optimizer directly from your terminal to process SVG files quickly. This is ideal for batch processing or integrating into build scripts.
2. **Standalone Package:** Use it as a PHP package in your project to optimize SVGs programmatically. This allows you to integrate SVG optimization directly into your application workflow or custom scripts.

CLI tool
--------

[](#cli-tool)

```
vendor/bin/svg-optimizer [options] process   ...
```

```
Options:
-h , --help               Display help for the command.
-c , --config             Path to a JSON file, or a raw JSON string, with custom optimization rules. If not provided, all default optimizations will be applied.
-d , --dry-run            Only calculate potential savings without modifying the files.
-r , --allow-risky        Explicitly enables risky rules, allowing them to be applied.
-a , --with-all-rules     Enable all non-risky rules. Use --allow-risky to include risky rules as well.
-q , --quiet              Suppress all output except errors.
-v , --version            Display the version of the library.
    --ansi                Force ANSI color output, even when output is not an interactive terminal.
    --no-ansi             Disable ANSI color output.

Commands:
Process                   Provide a list of directories or files to process.
```

#### Examples:

[](#examples)

```
vendor/bin/svg-optimizer --with-all-rules --dry-run process /path/to/svgs
vendor/bin/svg-optimizer --config=config.json process /path/to/file.svg
vendor/bin/svg-optimizer --config='{"removeInkscapeFootprints": true}' process /path/to/file.svg
vendor/bin/svg-optimizer --quiet --with-all-rules process /path/to/file.svg
vendor/bin/svg-optimizer --with-all-rules --allow-risky process /path/to/file.svg
vendor/bin/svg-optimizer --with-all-rules process /path/to/file.svg
```

#### Config file example:

[](#config-file-example)

```
{
    "convertColorsToHex": true,
    "convertCssClassesToAttributes": true,
    "convertEmptyTagsToSelfClosing": true,
    "convertInlineStylesToAttributes": true,
    "fixAttributeNames": false,
    "flattenGroups": true,
    "minifySvgCoordinates": true,
    "minifyTransformations": true,
    "removeAriaAndRole": true,
    "removeComments": true,
    "removeDataAttributes": false,
    "removeDefaultAttributes": true,
    "removeDeprecatedAttributes": true,
    "removeDoctype": true,
    "removeDuplicateElements": true,
    "removeEmptyAttributes": true,
    "removeEmptyGroups": true,
    "removeEmptyTextElements": true,
    "removeEnableBackgroundAttribute": false,
    "removeInkscapeFootprints": true,
    "removeInvisibleCharacters": true,
    "removeMetadata": true,
    "removeNonStandardAttributes": false,
    "removeNonStandardTags": false,
    "removeTitleAndDesc": true,
    "removeUnnecessaryWhitespace": true,
    "removeUnsafeElements": false,
    "removeUnusedMasks": true,
    "removeUnusedNamespaces": true,
    "removeWidthHeightAttributes": false,
    "scopeSvgStyles": false,
    "sortAttributes": true
}
```

### Example Workflow for GitHub Actions

[](#example-workflow-for-github-actions)

```
name: Optimize SVGs

on: [ push, pull_request ]

jobs:
    run-optimizer:
        runs-on: ubuntu-latest

        steps:
            -   uses: actions/checkout@v7
            -   uses: shivammathur/setup-php@v2
                with:
                    php-version: '8.5'
            -   run: composer install --no-dev --optimize-autoloader --no-interaction --no-progress
            -   run: php vendor/bin/svg-optimizer -a -q process /path/to/svgs
```

Package
-------

[](#package)

> It is recommended to catch exceptions when using this library. Doing so ensures that your application can handle unexpected input gracefully and avoid unintended crashes.

### Example specifying rules

[](#example-specifying-rules)

```
