
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文件測試該工具,並在生產環境中使用之前確保與特定用例的兼容性。