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.5(2w ago)3410.5k↑12.8%62MITPHPPHP ^8.3CI passing

Since Aug 27Pushed 2w 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 2d ago

READMEChangelog (10)Dependencies (108)Versions (93)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)[![PHPStan Level](https://camo.githubusercontent.com/66ba0baab49056b29c513a4a7c6d672e08da7a5e456be4228ad8ca52a87a4385/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d4c6576656c253230392d626c7565)](#)[![Type Coverage](https://camo.githubusercontent.com/005570096f18ab54d8a0bc70d9eee4ef71db36392433c009eb0eb6bcc3da438a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74797065253230636f7665726167652d3130302532352d627269676874677265656e)](#)[![Code Coverage](https://github.com/MathiasReker/php-svg-optimizer/raw/develop/dev/artifacts/coverage.svg)](#)[![Tests](https://github.com/MathiasReker/php-svg-optimizer/raw/develop/dev/artifacts/tests.svg)](#)[![Assertions](https://github.com/MathiasReker/php-svg-optimizer/raw/develop/dev/artifacts/assertions.svg)](#)[![Filesize](https://github.com/MathiasReker/php-svg-optimizer/raw/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/c75563390dfab0395c4ecf9513df241abe3e252fe3230cde0b54e67e3f233c9e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e737667)](https://github.com/MathiasReker/php-svg-optimizer/network/members)[![Stargazers](https://camo.githubusercontent.com/50e91b3f62c3339c7fb1a3cc6b137cb7597f20eddc9f7bf1b3070492b4c8275d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f4d61746869617352656b65722f7068702d7376672d6f7074696d697a65722e737667)](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.5^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 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.

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

#### Examples:

[](#examples)

```
vendor/bin/svg-optimizer --dry-run --with--all-rules process /path/to/svgs
vendor/bin/svg-optimizer --config=config.json process /path/to/file.svg
vendor/bin/svg-optimizer --config='{"removeUnsafeElements": true}' --allow-risky 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 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@v6
            -   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)

```
