PHP code example of mathiasreker / php-svg-optimizer
1. Go to this page and download the library: Download mathiasreker/php-svg-optimizer library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mathiasreker / php-svg-optimizer example snippets
declare(strict_types=1);
r\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,
sortAttributes: true,
convertEmptyTagsToSelfClosing: true,
removeUnnecessaryWhitespace: true,
)
->optimize()
->saveToFile('path/to/output.svg');
} catch (\Exception $exception) {
echo $exception->getMessage();
}
declare(strict_types=1);
r\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();
}
declare(strict_types=1);
r\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();
}
declare(strict_types=1);
r\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();
}
$svgOptimizer = SvgOptimizerService::fromFile('path/to/source.svg');
$svgOptimizer = SvgOptimizerService::fromString('<svg>...</svg>');
$svgOptimizer->withRules(removeTitleAndDesc: true);
$svgOptimizer->withRules(removeComments: true);
$svgOptimizer->withRules(sortAttributes: true);
$svgOptimizer->withRules(removeDefaultAttributes: true);
$svgOptimizer->withRules(removeDeprecatedAttributes: true);
$svgOptimizer->withRules(removeMetadata: true);
$svgOptimizer->withRules(removeInvisibleCharacters: true);
$svgOptimizer->withRules(flattenGroups: true);
$svgOptimizer->withRules(convertColorsToHex: true);
$svgOptimizer->withRules(minifySvgCoordinates: true);
$svgOptimizer->withRules(minifyTransformations: true);
$svgOptimizer->withRules(removeDoctype: true);
$svgOptimizer->withRules(removeEnableBackgroundAttribute: true);
$svgOptimizer->withRules(removeEmptyAttributes: true);
$svgOptimizer->withRules(convertEmptyTagsToSelfClosing: true);
$svgOptimizer->withRules(removeUnnecessaryWhitespace: true);
$svgOptimizer->withRules(
convertColorsToHex: true,
flattenGroups: true,
minifySvgCoordinates: true,
minifyTransformations: true,
removeComments: true,
removeDefaultAttributes: true,
removeDeprecatedAttributes: true,
removeDoctype: true,
removeEmptyAttributes: true,
removeMetadata: true,
removeInvisibleCharacters: true,
removeTitleAndDesc: true,
sortAttributes: true,
convertEmptyTagsToSelfClosing: true,
removeUnnecessaryWhitespace: true,
);
$svgOptimizer->optimize();
$svgOptimizer->saveToFile('path/to/output.svg');
$svgOptimizer->getContent();
$svgOptimizer->getMetaData()->getOptimizedSize();
$svgOptimizer->getMetaData()->getOriginalSize();
$svgOptimizer->getMetaData()->getSavedBytes();
$svgOptimizer->getMetaData()->getSavedPercentage();
bash
composer
bash
docker exec -it php-svg-optimizer bash