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\SvgOptimizer;

$result = (new SvgOptimizer())
    ->withDefaultRules()
    ->optimizeFile('logo.svg');

$result->saveToFile('optimized/logo.svg');

echo $result->getContent(), \PHP_EOL;



declare(strict_types=1);

r\SvgOptimizer;

$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect x="2" y="2" width="20" height="20" fill="#ff0000"/></svg>';

$result = (new SvgOptimizer())
    ->withDefaultRules()
    ->optimizeString($svg);

$metrics = $result->getMetrics();

echo $result->getContent(), \PHP_EOL;
echo 'Original size: ', $metrics->getOriginalSizeInBytes(), ' bytes', \PHP_EOL;
echo 'Optimized size: ', $metrics->getOptimizedSizeInBytes(), ' bytes', \PHP_EOL;
echo 'Saved: ', $metrics->getSavedSizeInBytes(), ' bytes', \PHP_EOL;
echo 'Saved: ', round($metrics->getSavedPercentage(), 2), ' %', \PHP_EOL;
echo 'Has savings: ', $metrics->hasSavings() ? 'yes' : 'no', \PHP_EOL;
echo 'Duration: ', round($metrics->getDurationInSeconds(), 4), ' seconds', \PHP_EOL;



declare(strict_types=1);

r\Rule;
use MathiasReker\PhpSvgOptimizer\SvgOptimizer;

$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><!-- icon --><rect width="20" height="20" fill="#ff0000"/><title>Square</title></svg>';

$result = (new SvgOptimizer())
    ->withRules(Rule::RemoveComments, Rule::MinifyColors)
    ->optimizeString($svg);

echo $result->getContent(), \PHP_EOL;



declare(strict_types=1);

r\Rule;
use MathiasReker\PhpSvgOptimizer\RuleSet;
use MathiasReker\PhpSvgOptimizer\SvgOptimizer;

$result = (new SvgOptimizer())
    ->withRuleSets(RuleSet::Security, RuleSet::Minification)
    ->withRules(Rule::RemoveTitles)
    ->withoutRules(Rule::SortAttributes)
    ->optimizeFile('logo.svg');

echo $result->getContent(), \PHP_EOL;



declare(strict_types=1);

r\Rule;
use MathiasReker\PhpSvgOptimizer\RuleSet;
use MathiasReker\PhpSvgOptimizer\SvgOptimizer;

$result = (new SvgOptimizer())
    ->withDefaultRules()
    ->withRuleSets(RuleSet::Security)
    ->withoutRules(Rule::RemoveComments)
    ->optimizeFile('logo.svg');

echo $result->getContent(), \PHP_EOL;



declare(strict_types=1);

r\Exception\RiskyRulesNotAllowedException;
use MathiasReker\PhpSvgOptimizer\Rule;
use MathiasReker\PhpSvgOptimizer\SvgOptimizer;

$optimizer = (new SvgOptimizer())->withDefaultRules();

echo $optimizer->optimizeFile('logo.svg')->getContent(), \PHP_EOL;

echo $optimizer->withRiskyRulesAllowed()->optimizeFile('logo.svg')->getContent(), \PHP_EOL;

try {
    (new SvgOptimizer())->withRules(Rule::RemoveDataAttributes)->optimizeFile('logo.svg');
} catch (RiskyRulesNotAllowedException $exception) {
    echo $exception->getMessage(), \PHP_EOL;
}



declare(strict_types=1);

r\Exception\ExceptionInterface;
use MathiasReker\PhpSvgOptimizer\Exception\FileNotFoundException;
use MathiasReker\PhpSvgOptimizer\Exception\InvalidSvgException;
use MathiasReker\PhpSvgOptimizer\Exception\IOException;
use MathiasReker\PhpSvgOptimizer\SvgOptimizer;

$optimizer = (new SvgOptimizer())->withDefaultRules();

try {
    $optimizer->optimizeFile('logo.svg')->saveToFile('notes.txt/logo.svg');
} catch (IOException $exception) {
    echo $exception::class, ': ', $exception->getMessage(), \PHP_EOL;
}

foreach (['missing.svg', 'broken.svg'] as $path) {
    try {
        $optimizer->optimizeFile($path);
    } catch (FileNotFoundException $exception) {
        echo $exception::class, ': ', $exception->getMessage(), \PHP_EOL;
    } catch (InvalidSvgException $exception) {
        echo $exception::class, ': ', $exception->getMessage(), \PHP_EOL;
        echo 'Reason: ', $exception->getPrevious()?->getMessage(), \PHP_EOL;
    } catch (ExceptionInterface $exception) {
        echo 'Any other exception of the library: ', $exception->getMessage(), \PHP_EOL;
    }
}

$rule = Rule::RemoveUnusedIds;

echo $rule->value, ' (risky: ', var_export($rule->isRisky(), true), ', set: ', $rule->getRuleSet()?->value, ')', \PHP_EOL;
echo $rule->getDescription(), \PHP_EOL;



declare(strict_types=1);

r\RuleSet;
use MathiasReker\PhpSvgOptimizer\SvgOptimizer;

$result = (new SvgOptimizer())
    ->withRuleSets(RuleSet::Security)
    ->optimizeFile('untrusted.svg');

echo $result->getContent(), \PHP_EOL;
bash
composer