
php-svg-optimizer是一个PHP库,旨在通过应用各种转换和清理操作来优化SVG文件。库确保优化的SVG文件符合SVG 2.0规格。
该工具努力在不丢失任何可能扭曲图像质量的数据的情况下进行优化,从而确保所得的SVG在视觉上与原始相同,同时在尺寸和性能方面更有效。
| 版本 | php | 文档 |
|---|---|---|
| ^5.1 | ^8.3 | 当前的 |
ext-dom :XML处理所需的PHP扩展。ext-libxml :XML错误处理所需的PHP扩展名。要安装库,请运行:
composer require mathiasreker/php-svg-optimizer您可以将此库用作命令行工具(CLI)或独立包装。
vendor/bin/svg-optimizer [options] process < path 1> < path 2> ...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.
-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.vendor/bin/svg-optimizer --dry-run process /path/to/svgs
vendor/bin/svg-optimizer --config=config.json process /path/to/file.svg
vendor/bin/svg-optimizer --quiet process /path/to/file.svg{
"convertColorsToHex" : true ,
"flattenGroups" : true ,
"minifySvgCoordinates" : true ,
"minifyTransformations" : true ,
"removeComments" : true ,
"removeDefaultAttributes" : true ,
"removeDeprecatedAttributes" : true ,
"removeDoctype" : true ,
"removeEnableBackgroundAttribute" : true ,
"removeEmptyAttributes" : true ,
"removeMetadata" : true ,
"removeTitleAndDesc" : true ,
"removeUnnecessaryWhitespace" : true ,
"sortAttributes" : true
}为了确保使用库时的鲁棒性,要处理异常至关重要,因为无效或畸形的SVG文件可能导致运行时错误。捕获这些例外将使您能够优雅地管理潜在问题并防止应用程序崩溃。
<?php
declare (strict_types= 1 );
require_once __DIR__ . ' /vendor/autoload.php ' ;
use MathiasReker PhpSvgOptimizer Services SvgOptimizerService ;
try {
$ svgOptimizer = SvgOptimizerService:: fromFile ( ' path/to/source.svg ' )
-> withRules (
convertColorsToHex: true ,
flattenGroups: true ,
minifySvgCoordinates: true ,
minifyTransformations: true ,
removeComments: true ,
removeDefaultAttributes: true ,
removeDeprecatedAttributes: true ,
removeDoctype: true ,
removeEnableBackgroundAttribute: true ,
removeEmptyAttributes: true ,
removeMetadata: true ,
removeTitleAndDesc: false ,
removeUnnecessaryWhitespace: true ,
sortAttributes: true ,
)
-> optimize ()
-> saveToFile ( ' path/to/output.svg ' );
} catch ( Exception $ exception ) {
echo $ exception -> getMessage ();
} <?php
declare (strict_types= 1 );
require_once __DIR__ . ' /vendor/autoload.php ' ;
use MathiasReker PhpSvgOptimizer Services SvgOptimizerService ;
try {
$ svgOptimizer = SvgOptimizerService:: fromFile ( ' path/to/source.svg ' )
-> optimize ()
-> saveToFile ( ' path/to/output.svg ' );
$ metaData = $ svgOptimizer -> getMetaData ();
echo sprintf ( ' Optimized size: %d bytes%s ' , $ metaData -> getOptimizedSize (), PHP_EOL );
echo sprintf ( ' Original size: %d bytes%s ' , $ metaData -> getOriginalSize (), PHP_EOL );
echo sprintf ( ' Size reduction: %d bytes%s ' , $ metaData -> getSavedBytes (), PHP_EOL );
echo sprintf ( ' Reduction percentage: %s %%%s ' , $ metaData -> getSavedPercentage (), PHP_EOL );
} catch ( Exception $ exception ) {
echo $ exception -> getMessage ();
} <?php
declare (strict_types= 1 );
require_once __DIR__ . ' /vendor/autoload.php ' ;
use MathiasReker PhpSvgOptimizer Services SvgOptimizerService ;
try {
$ svgOptimizer = SvgOptimizerService:: fromFile ( ' path/to/source.svg ' )
-> optimize ();
echo sprintf ( ' Get content: ' , $ svgOptimizer -> getContent (), PHP_EOL );
$ metaData = $ svgOptimizer -> getMetaData ();
echo sprintf ( ' Optimized size: %d bytes%s ' , $ metaData -> getOptimizedSize (), PHP_EOL );
echo sprintf ( ' Original size: %d bytes%s ' , $ metaData -> getOriginalSize (), PHP_EOL );
echo sprintf ( ' Size reduction: %d bytes%s ' , $ metaData -> getSavedBytes (), PHP_EOL );
echo sprintf ( ' Reduction percentage: %s %%%s ' , $ metaData -> getSavedPercentage (), PHP_EOL );
} catch ( Exception $ exception ) {
echo $ exception -> getMessage ();
} <?php
declare (strict_types= 1 );
require_once __DIR__ . ' /vendor/autoload.php ' ;
use MathiasReker PhpSvgOptimizer Services SvgOptimizerService ;
try {
$ svgOptimizer = SvgOptimizerService:: fromString ( ' <svg>...</svg> ' )
-> optimize ();
echo sprintf ( ' Content: ' , $ svgOptimizer -> getContent (), PHP_EOL );
$ metaData = $ svgOptimizer -> getMetaData ();
echo sprintf ( ' Optimized size: %d bytes%s ' , $ metaData -> getOptimizedSize (), PHP_EOL );
echo sprintf ( ' Original size: %d bytes%s ' , $ metaData -> getOriginalSize (), PHP_EOL );
echo sprintf ( ' Size reduction: %d bytes%s ' , $ metaData -> getSavedBytes (), PHP_EOL );
echo sprintf ( ' Reduction percentage: %s %%%s ' , $ metaData -> getSavedPercentage (), PHP_EOL );
} catch ( Exception $ exception ) {
echo $ exception -> getMessage ();
}静态出厂方法从文件路径创建SvgOptimizerService 。
$ svgOptimizer = SvgOptimizerService:: fromFile ( ' path/to/source.svg ' );静态出厂方法从字符串创建SvgOptimizerService 。
$ svgOptimizer = SvgOptimizerService:: fromString ( ' <svg>...</svg> ' );withRules方法配置要应用的SVG优化规则。该方法接受布尔参数,以确定是否应启用或禁用特定规则。
从svg中删除<title>和<desc>标签:
$ svgOptimizer -> withRules (removeTitleAndDesc: true );从SVG中删除所有评论:
$ svgOptimizer -> withRules (removeComments: true );清理SVG中不必要的空格:
$ svgOptimizer -> withRules (removeUnnecessaryWhitespace: true );每个元素中的属性:
$ svgOptimizer -> withRules (sortAttributes: true );删除匹配常见默认值的默认属性值:
$ svgOptimizer -> withRules (removeDefaultAttributes: true );从SVG中删除弃用的属性:
$ svgOptimizer -> withRules (removeDeprecatedAttributes: true );从SVG中删除<metadata>标签:
$ svgOptimizer -> withRules (removeMetadata: true ); <g>的元素嵌套,将其子元素移至父节点:
$ svgOptimizer -> withRules (flattenGroups: true );将rgb()颜色值转换为十六进制格式:
$ svgOptimizer -> withRules (convertColorsToHex: true );通过删除不必要的精度来减小坐标值:
$ svgOptimizer -> withRules (minifySvgCoordinates: true );通过删除冗余值来减小转换属性:
$ svgOptimizer -> withRules (minifyTransformations: true );删除SVG Doctype声明:
$ svgOptimizer -> withRules (removeDoctype: true );从svg中删除enable-background属性:
$ svgOptimizer -> withRules (removeEnableBackgroundAttribute: true );从SVG中删除空属性:
$ svgOptimizer -> withRules (removeEmptyAttributes: true );默认情况下,所有选项均设置为true。您可以通过将所需值传递给它来单独配置它们:
$ svgOptimizer -> withRules (
convertColorsToHex: true ,
flattenGroups: true ,
minifySvgCoordinates: true ,
minifyTransformations: true ,
removeComments: true ,
removeDefaultAttributes: true ,
removeDeprecatedAttributes: true ,
removeDoctype: true ,
removeEmptyAttributes: true ,
removeMetadata: true ,
removeTitleAndDesc: true ,
removeUnnecessaryWhitespace: true ,
sortAttributes: true ,
);optimize方法最终确定优化过程并生成优化的SVG文件。
$ svgOptimizer -> optimize ();saveToFile方法将优化的SVG文件保存到指定的路径。
$ svgOptimizer -> saveToFile ( ' path/to/output.svg ' );getContent方法返回优化的SVG内容。
$ svgOptimizer -> getContent ();getOptimizedSize方法返回优化的SVG文件的大小。
$ svgOptimizer -> getMetaData ()-> getOptimizedSize ();getOriginalSize方法返回原始SVG文件的大小。
$ svgOptimizer -> getMetaData ()-> getOriginalSize ();getSavedBytes方法返回优化过程保存的字节数。
$ svgOptimizer -> getMetaData ()-> getSavedBytes ();getSavedPercentage方法返回优化过程保存的字节百分比。
$ svgOptimizer -> getMetaData ()-> getSavedPercentage ();有关建议的功能和已知问题的完整列表,请参阅“开放问题”。
我们欢迎所有贡献!如果您有改进的想法,请随意分配存储库并提交拉动请求。您也可以打开一个问题。如果您发现这个项目有帮助,请不要忘记给它一个明星!
图书馆实现了策略模式,其中策略被封装为/src/Services/Rules目录中的“规则”。
创建规则:
通过在/src/Services/Rules目录中添加新类,以实现SvgOptimizerRuleInterface 。
写测试:
在/tests/Services/Rules目录中为您的规则开发全面的测试案例,以确保其行为预期。
整合规则:
/src/Services/SvgOptimizerService.php的构建器中。/src/Commands/SvgOptimizerCommand.php更新CLI工具。更新文档:
在README.md中记录规则的功能和目的。
遵循以下步骤确保您的规则无缝整合到项目中。
要与Docker一起使用该项目,您可以使用:
docker-compose up -d然后,访问容器:
docker exec -it php-svg-optimizer bash运行phpstan:
composer phpstan运行测试:
composer test格式代码:
composer format该项目已根据MIT许可获得许可。有关更多信息,请参见许可证文件。
尽管该工具已经经过了彻底的测试,并以避免风险变化的方式构建,但其使用自负。我们不能保证它将与所有SVG文件或工作流完全兼容。强烈建议使用示例SVG文件测试该工具,并在生产环境中使用之前确保与特定用例的兼容性。