public static function optimize($path, $svgo = 0, $image_optim = 0, $picopt = 0, $imageOptim = 0)
{
// make sure the path is real
if (!file_exists($path)) {
return false;
}
$is_dir = is_dir($path);
if (!$is_dir) {
$dir = escapeshellarg(substr($path, 0, strrpos($path, '/')));
$file = escapeshellarg(substr($path, strrpos($path, '/') + 1));
}
$path = escapeshellarg($path);
// make sure we got some ints up in here
$svgo = (int) $svgo;
$image_optim = (int) $image_optim;
$picopt = (int) $picopt;
$imageOptim = (int) $imageOptim;
// create some vars to store output
$output = array();
$return_var = 0;
// if we’re using image_optim, we need to create the YAML config file
if ($image_optim > 0) {
$yml = tempnam('/tmp', 'yml');
file_put_contents($yml, "verbose: true\njpegtran:\n progressive: false\noptipng:\n level: 7\n interlace: false\npngcrush:\n fix: true\n brute: true\npngquant:\n speed: 11\n");
}
// do the svgo optimizations
for ($i = 0; $i < $svgo; $i++) {
if ($is_dir) {
$command = escapeshellcmd('svgo -f ' . $path . ' --disable removeUnknownsAndDefaults');
} else {
$command = escapeshellcmd('svgo -i ' . $path . ' --disable removeUnknownsAndDefaults');
}
exec($command, $output, $return_var);
if ($return_var != 0) {
return false;
}
}
// do the image_optim optimizations
for ($i = 0; $i < $image_optim; $i++) {
$command = escapeshellcmd('image_optim -r ' . $path . ' --config-paths ' . $yml);
exec($command, $output, $return_var);
if ($return_var != 0) {
return false;
}
}
// do the picopt optimizations
for ($i = 0; $i < $picopt; $i++) {
$command = escapeshellcmd('picopt -r ' . $path);
exec($command, $output, $return_var);
if ($return_var != 0) {
return false;
}
}
// do the ImageOptim optimizations
// ImageOptim can’t handle the path with single quotes, so we have to strip them
// ImageOptim-CLI has an issue where it only works with a directory, not a single file
for ($i = 0; $i < $imageOptim; $i++) {
if ($is_dir) {
$command = escapeshellcmd('imageoptim -d ' . $path . ' -q');
} else {
$command = escapeshellcmd('find ' . $dir . ' -name ' . $file) . ' | imageoptim';
}
exec($command, $output, $return_var);
if ($return_var != 0) {
return false;
}
}
return $output;
}