Created
September 23, 2020 18:56
-
-
Save vicdecode/adaa55c2fb209a5a8c79c8741aeab458 to your computer and use it in GitHub Desktop.
Script to optimize images using ImageMagick line command and ImageOptim (only OSX) cli...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Optimize Images | |
* | |
* Script to optimize images using ImageOptim and ImageMagic | |
* Usage: optimize-images.php /path/to/image/or/directory [options] | |
* | |
* Options: | |
* --max-width | |
* --quality | |
* --image-optim | |
* --jpeg-mini | |
* --image-alpha | |
* -- only-optim | |
* | |
* @author Marcelo Rodrigues <marcelo.mx@gmail.com> | |
*/ | |
$scriptArgs = parseArgs($argv, [ | |
'max-width' => '1920', | |
'quality' => '80', | |
'image-optim' => 0, | |
'jpeg-mini' => 0, | |
'image-alpha' => 0, | |
'only-optim' => 0 | |
]); | |
/** @var array $args **/ | |
/** @var array $options **/ | |
extract($scriptArgs); | |
if (!isset($args[0])) { | |
throw new \Exception('You must provide a valid directory or image path'); | |
} | |
optimizeImage($args[0], $options, true); | |
exit(); | |
/** | |
* @param array $args | |
* @param array $defaults | |
* @return array | |
*/ | |
function parseArgs($inputArgs, $defaultOptions = []) | |
{ | |
$args = []; | |
$options = $defaultOptions; | |
// Remove first args | |
unset($inputArgs[0]); | |
foreach($inputArgs as $key => $arg) { | |
if (preg_match('/--([\w\-]+)(=(.*))?/', $arg, $match)) { | |
$value = isset($match[3]) ? $match[3] : 1; | |
$options[$match[1]] = $value; | |
} else { | |
$args[] = $arg; | |
} | |
} | |
return ['args' => $args, 'options' => $options]; | |
} | |
/** | |
* @param string $imagePath | |
* @param int $maxWidth | |
* @param int $quality | |
* @param int $imageOptim | |
*/ | |
function optimizeImage($imagePath, $options = array(), $output = true) | |
{ | |
if (is_array($imagePath)) { | |
foreach ($imagePath as $path) { | |
try { | |
optimizeImage($path, $options, $output); | |
} catch (\Exception $e) { | |
if (!$output) throw $e; | |
echo '> Error: ' . $e->getMessage() . PHP_EOL; | |
} | |
} | |
return; | |
} | |
if (!file_exists($imagePath)) { | |
$error = 'Invalid image or directory path: ' . $imagePath; | |
if (!$output) { | |
throw new \RuntimeException($error); | |
} | |
echo '> Error: ' . $error . PHP_EOL; | |
return; | |
} | |
if (is_dir($imagePath)) { | |
$imageFiles = getImageFiles($imagePath); | |
optimizeImage($imageFiles, $options, $output); | |
if ($options['only-optim'] || $options['image-optim']) { | |
//if ($options['jpeg-mini']) $cmd .= ' --jpeg-mini'; | |
//if ($options['image-alpha']) $cmd .= ' --image-alpha'; | |
//$cmd .= ' --verbose'; | |
$cmd = ' imageoptim '; | |
// $cmd .= ' --image-alpha'; | |
// $cmd .= ' --jpeg-mini'; | |
$cmd .= ' --quit'; | |
$cmd .= ' --no-color'; | |
$cmd .= ' --directory ' . escapeshellarg($imagePath); | |
if ($output) { | |
echo $cmd . PHP_EOL; | |
} | |
passthru($cmd); | |
} | |
return; | |
} | |
if ($options['only-optim']) { | |
return; | |
} | |
$cmd = 'mogrify '; | |
if ($options['max-width']) $cmd .= sprintf(' -resize "%s>"', $options['max-width']); | |
if ($options['quality']) $cmd .= sprintf(' -quality %s', escapeshellarg($options['quality'])); | |
$cmd .= ' ' . escapeshellarg($imagePath); | |
if ($output) { | |
echo $cmd . PHP_EOL; | |
} | |
passthru($cmd); | |
} | |
/** | |
* @param string | |
* @return array | |
*/ | |
function getImageFiles($path, $extensions = ['jpg', 'jpeg', 'png', 'gif']) | |
{ | |
$imageFiles = array(); | |
foreach ($extensions as $extension) { | |
foreach ([$extension, strtoupper($extension)] as $ext) { | |
$imageFiles = array_merge($imageFiles, getFiles($path . '/*.' . $ext)); | |
} | |
} | |
return $imageFiles; | |
} | |
/** | |
* @param string $pattern | |
* @param int $flags | |
* @return array | |
*/ | |
// Does not support flag GLOB_BRACE | |
function getFiles($pattern, $flags = 0) | |
{ | |
$files = glob($pattern, $flags); | |
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { | |
$files = array_merge($files, getFiles($dir.'/'.basename($pattern), $flags)); | |
} | |
return $files; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment