arnapou / pixelsvg
Library - Simple library to convert pixelated images to svg.
v2.3.0
2024-11-25 09:18 UTC
Requires
- php: ~8.3.0 || ~8.4.0
- arnapou/ensure: ^2.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
README
This library is a simple tool to convert pixelated images to SVG.
It optimises the svg file size by merging pixels in a single path.
Installation
composer require arnapou/pixelsvg
packagist 👉️ arnapou/pixelsvg
Renderer
The logic of the path rendering is in the Renderer
you give to PixelSvg
Renderers | Example 1 | Example 2 | Example 3 | Description |
---|---|---|---|---|
Original | 0.7 KB | 0.9 KB | 0.3 KB | |
ByPixelRenderer | 18.4 KB | 7.6 KB | 25.5 KB | One svg path by pixel. |
ByColorRenderer | 2.0 KB | 1.8 KB | 4.9 KB | One svg path by color. |
ByColorLayeredRenderer | 1.6 KB | 1.5 KB | 4.5 KB | One svg path by color + guessing overlapping layers to reduce the number of path points. |
SplitAnimationRenderer | 1.6 KB | 1.5 KB | 4.0 KB | Split the static pixels from the dynamic before calling the decorated renderer. |
Color transforms injected in the renderer | Example 1 | Example 2 | Example 3 |
---|---|---|---|
ByColorRenderer + GrayColorTransform | 2.0 KB | 1.8 KB | 4.9 KB |
ByColorRenderer + UniqueColorTransform | 0.3 KB | 0.3 KB | 0.5 KB |
From code
$filename = '/some/file/to/convert.gif';
$renderer = new \Arnapou\PixelSvg\Renderer\ByColorRenderer();
$pixelSvg = new \Arnapou\PixelSvg\PixelSvg($renderer);
$reader = new \Arnapou\PixelSvg\ImageReader\ImagickReader::fromFilename($filename);
$svgContent = $pixelSvg->render($reader);
⚠️ Prefer the use of Imagick for animated GIFs, because GD does not support it.
Php versions
Date | Ref | 8.4 | 8.3 | 8.2 |
---|---|---|---|---|
25/11/2024 | 2.3.x, main | × | × | |
26/11/2023 | 2.0 - 2.2 | × | ||
05/08/2023 | 1.x | × |