arnapou / pixelsvg
库 - 将像素化图像转换为 SVG 的简单库。
v2.2
2024-04-14 15:55 UTC
Requires
- php: ~8.3.0
- arnapou/ensure: ^2.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
README
此库是一个将像素化图像转换为 SVG 的简单工具。
它通过合并单一路径中的像素来优化 SVG 文件大小。
安装
composer require arnapou/pixelsvg
packagist 👉️ arnapou/pixelsvg
渲染器
路径渲染的逻辑在您提供给 PixelSvg
的 Renderer
中。
渲染器 | 示例 1 | 示例 2 | 示例 3 | 描述 |
---|---|---|---|---|
原始 | ![]() | ![]() | ![]() | |
ByPixelRenderer | 每个像素一个 SVG 路径。 | |||
ByColorRenderer | 每个颜色一个 SVG 路径。 | |||
ByColorLayeredRenderer | 一个 SVG 路径通过颜色 + 猜测重叠层以减少路径点数。 | |||
SplitAnimationRenderer | 在调用装饰渲染器之前,将静态像素与动态像素分开。 |
在渲染器中注入颜色转换 | 示例 1 | 示例 2 | 示例 3 |
---|---|---|---|
ByColorRenderer + GrayColorTransform | |||
ByColorRenderer + UniqueColorTransform |
从代码中
$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);
⚠️ 对于动画 GIF,首选使用 Imagick,因为 GD 不支持它。
PHP 版本
日期 | 参考 | 8.3 | 8.2 |
---|---|---|---|
26/11/2023 | 2.x, main | × | |
05/08/2023 | 1.x | × |