Skip to content

Commit

Permalink
Minify
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Oct 19, 2024
1 parent 1bd8fd4 commit b733ea4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Identify issues in an SVG file
svgtinyps issues input.svg
```

Convert an SVG file to SVG (P/S)
Convert an SVG file to SVG P/S (Portabler and Secure) format
```bash
svgtinyps convert input.svg output.svg
```
Expand All @@ -61,6 +61,11 @@ If in the identified issues, you missing th title tag, you can set its value wit
svgtinyps convert input.svg output.svg --title="My awesome company"
```

Minify an SVG
```bash
svgtinyps minify input.svg output.svg
```


## 🚦 Testing
This project use [Pest](https://pestphp.com/) for testing.
Expand Down
44 changes: 29 additions & 15 deletions src/svgtinyps.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Composer\InstalledVersions;
use SVGTinyPS\SVGTinyPS;

if (!class_exists('\Composer\InstalledVersions')) {
if (! class_exists('\Composer\InstalledVersions')) {
require __DIR__.'/../vendor/autoload.php';
}

Expand Down Expand Up @@ -57,26 +57,26 @@ function showHelp(): void
echo 'Commands:'.PHP_EOL;
echo ' convert [input] [output] - Convert SVG file'.PHP_EOL;
echo ' issues [input] - Check for issues in SVG file'.PHP_EOL;
// echo ' minify [input] - Minify SVG file'.PHP_EOL;
echo ' minify [input] [output] - Minify SVG file'.PHP_EOL;
echo ' help - Show this help information'.PHP_EOL;
echo PHP_EOL;
echo 'Informations:'.PHP_EOL;
echo !str_starts_with($version, '@git_tag') ? ' Version: '.$version.PHP_EOL : '';
echo ! str_starts_with($version, '@git_tag') ? ' Version: '.$version.PHP_EOL : '';
echo ' PHP version: '.phpversion().PHP_EOL;
// echo 'PHP sapi name: '.php_sapi_name().PHP_EOL;
echo ' Based on https://github.com/srwiez/php-svg-ps-converter ('.getComposerVersion('srwiez/php-svg-ps-converter').')'.PHP_EOL;
echo ' Built with https://github.com/box-project/box'.PHP_EOL;
echo php_sapi_name() == 'micro' ? ' Compiled with https://github.com/crazywhalecc/static-php-cli'.PHP_EOL : '';
}

if (!$command || $command === 'help') {
if (! $command || $command === 'help') {
showHelp();
exit;
}

function checkInputFile($inputFile): void
{
if (!$inputFile || !file_exists($inputFile)) {
if (! $inputFile || ! file_exists($inputFile)) {
echo "Error: Input file not provided or doesn't exist.".PHP_EOL;
exit(1);
}
Expand All @@ -90,7 +90,7 @@ function checkOutputFile($outputFile): void
exit(1);
}

if ($outputDir && (!is_dir($outputDir) || !is_writable($outputDir))) {
if ($outputDir && (! is_dir($outputDir) || ! is_writable($outputDir))) {
echo "Error: The output directory either does not exist or is not writeable.\n";
exit(1);
}
Expand All @@ -117,10 +117,17 @@ function checkOutputFile($outputFile): void
verboseLog('Checking for SVG issues', $options['is_verbose']);
checkIssues($inputFile, $options['is_verbose']);
break;
// case 'minify':
// verboseLog('Starting SVG minification', $isVerbose);
// minifySvg($inputFile, $isVerbose);
// break;
case 'minify':
$inputFile = $argv[1] ?? null;
$outputFile = $argv[2] ?? null;

checkInputFile($inputFile);
checkOutputFile($outputFile);

verboseLog('Starting SVG minification', $options['is_verbose']);

minifySvg($inputFile, $outputFile, $options['is_verbose']);
break;
default:
echo 'Invalid command!'.PHP_EOL;
echo PHP_EOL;
Expand Down Expand Up @@ -162,8 +169,15 @@ function checkIssues($input, $isVerbose): void
}
}

// function minifySvg($input, $isVerbose)
// {
// verboseLog("Minifying $input", $isVerbose);
// // Your implementation
// }
function minifySVG($input, $output, $isVerbose): void
{
verboseLog("Converting $input to $output", $isVerbose);

$new_svg = file_get_contents($input);
$new_svg = preg_replace('/\s+/', ' ', $new_svg);

//TODO: Maybe just use svggo ?
file_put_contents($output, $new_svg);
}

0 comments on commit b733ea4

Please sign in to comment.