5
5
use Composer \InstalledVersions ;
6
6
use SVGTinyPS \SVGTinyPS ;
7
7
8
- if (!class_exists ('\Composer\InstalledVersions ' )) {
8
+ if (! class_exists ('\Composer\InstalledVersions ' )) {
9
9
require __DIR__ .'/../vendor/autoload.php ' ;
10
10
}
11
11
@@ -57,26 +57,26 @@ function showHelp(): void
57
57
echo 'Commands: ' .PHP_EOL ;
58
58
echo ' convert [input] [output] - Convert SVG file ' .PHP_EOL ;
59
59
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 ;
61
61
echo ' help - Show this help information ' .PHP_EOL ;
62
62
echo PHP_EOL ;
63
63
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 : '' ;
65
65
echo ' PHP version: ' .phpversion ().PHP_EOL ;
66
66
// echo 'PHP sapi name: '.php_sapi_name().PHP_EOL;
67
67
echo ' Based on https://github.com/srwiez/php-svg-ps-converter ( ' .getComposerVersion ('srwiez/php-svg-ps-converter ' ).') ' .PHP_EOL ;
68
68
echo ' Built with https://github.com/box-project/box ' .PHP_EOL ;
69
69
echo php_sapi_name () == 'micro ' ? ' Compiled with https://github.com/crazywhalecc/static-php-cli ' .PHP_EOL : '' ;
70
70
}
71
71
72
- if (!$ command || $ command === 'help ' ) {
72
+ if (! $ command || $ command === 'help ' ) {
73
73
showHelp ();
74
74
exit ;
75
75
}
76
76
77
77
function checkInputFile ($ inputFile ): void
78
78
{
79
- if (!$ inputFile || !file_exists ($ inputFile )) {
79
+ if (! $ inputFile || ! file_exists ($ inputFile )) {
80
80
echo "Error: Input file not provided or doesn't exist. " .PHP_EOL ;
81
81
exit (1 );
82
82
}
@@ -90,7 +90,7 @@ function checkOutputFile($outputFile): void
90
90
exit (1 );
91
91
}
92
92
93
- if ($ outputDir && (!is_dir ($ outputDir ) || !is_writable ($ outputDir ))) {
93
+ if ($ outputDir && (! is_dir ($ outputDir ) || ! is_writable ($ outputDir ))) {
94
94
echo "Error: The output directory either does not exist or is not writeable. \n" ;
95
95
exit (1 );
96
96
}
@@ -117,10 +117,17 @@ function checkOutputFile($outputFile): void
117
117
verboseLog ('Checking for SVG issues ' , $ options ['is_verbose ' ]);
118
118
checkIssues ($ inputFile , $ options ['is_verbose ' ]);
119
119
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 ;
124
131
default :
125
132
echo 'Invalid command! ' .PHP_EOL ;
126
133
echo PHP_EOL ;
@@ -162,8 +169,15 @@ function checkIssues($input, $isVerbose): void
162
169
}
163
170
}
164
171
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