Skip to content

Commit b733ea4

Browse files
committed
Minify
1 parent 1bd8fd4 commit b733ea4

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Identify issues in an SVG file
5151
svgtinyps issues input.svg
5252
```
5353

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

64+
Minify an SVG
65+
```bash
66+
svgtinyps minify input.svg output.svg
67+
```
68+
6469

6570
## 🚦 Testing
6671
This project use [Pest](https://pestphp.com/) for testing.

src/svgtinyps.php

+29-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Composer\InstalledVersions;
66
use SVGTinyPS\SVGTinyPS;
77

8-
if (!class_exists('\Composer\InstalledVersions')) {
8+
if (! class_exists('\Composer\InstalledVersions')) {
99
require __DIR__.'/../vendor/autoload.php';
1010
}
1111

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

72-
if (!$command || $command === 'help') {
72+
if (! $command || $command === 'help') {
7373
showHelp();
7474
exit;
7575
}
7676

7777
function checkInputFile($inputFile): void
7878
{
79-
if (!$inputFile || !file_exists($inputFile)) {
79+
if (! $inputFile || ! file_exists($inputFile)) {
8080
echo "Error: Input file not provided or doesn't exist.".PHP_EOL;
8181
exit(1);
8282
}
@@ -90,7 +90,7 @@ function checkOutputFile($outputFile): void
9090
exit(1);
9191
}
9292

93-
if ($outputDir && (!is_dir($outputDir) || !is_writable($outputDir))) {
93+
if ($outputDir && (! is_dir($outputDir) || ! is_writable($outputDir))) {
9494
echo "Error: The output directory either does not exist or is not writeable.\n";
9595
exit(1);
9696
}
@@ -117,10 +117,17 @@ function checkOutputFile($outputFile): void
117117
verboseLog('Checking for SVG issues', $options['is_verbose']);
118118
checkIssues($inputFile, $options['is_verbose']);
119119
break;
120-
// case 'minify':
121-
// verboseLog('Starting SVG minification', $isVerbose);
122-
// minifySvg($inputFile, $isVerbose);
123-
// break;
120+
case 'minify':
121+
$inputFile = $argv[1] ?? null;
122+
$outputFile = $argv[2] ?? null;
123+
124+
checkInputFile($inputFile);
125+
checkOutputFile($outputFile);
126+
127+
verboseLog('Starting SVG minification', $options['is_verbose']);
128+
129+
minifySvg($inputFile, $outputFile, $options['is_verbose']);
130+
break;
124131
default:
125132
echo 'Invalid command!'.PHP_EOL;
126133
echo PHP_EOL;
@@ -162,8 +169,15 @@ function checkIssues($input, $isVerbose): void
162169
}
163170
}
164171

165-
// function minifySvg($input, $isVerbose)
166-
// {
167-
// verboseLog("Minifying $input", $isVerbose);
168-
// // Your implementation
169-
// }
172+
function minifySVG($input, $output, $isVerbose): void
173+
{
174+
verboseLog("Converting $input to $output", $isVerbose);
175+
176+
$new_svg = file_get_contents($input);
177+
$new_svg = preg_replace('/\s+/', ' ', $new_svg);
178+
179+
//TODO: Maybe just use svggo ?
180+
181+
file_put_contents($output, $new_svg);
182+
}
183+

0 commit comments

Comments
 (0)