This function calls up to four external programs, which must be installed and available in the $PATH:
* SVGO
* image_optim
* picopt
* ImageOptim
Note that these are executed using PHP’s exec command, so there may be security implications.
public static optimize ( string $path, integer $svgo, integer $image_optim, integer $picopt, integer $imageOptim ) | ||
$path | string | The path to the file or directory that should be optimized. |
$svgo | integer | The number of times to optimize using SVGO. |
$image_optim | integer | The number of times to optimize using image_optim. |
$picopt | integer | The number of times to optimize using picopt. |
$imageOptim | integer | The number of times to optimize using ImageOptim. |
private function optimize($imagePath) { chmod($imagePath, 0777); $image = new Respimg($imagePath); $width = getimagesize($imagePath); if ($width > 54) { $width = 54; } $image->smartResize($width, 0, true); $image->cropImage($width, $width, 0, 0); $image->writeImage($imagePath); Respimg::optimize($imagePath, 0, 1, 1, 1); }
$base = pathinfo($file, PATHINFO_BASENAME); $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if ($ext === 'svg') { foreach ($widths as $w) { echo 'Rasterizing ' . $file . ' to ' . $w . '…'; Respimg::rasterize($path_svg_i . '/' . $file, $path_svg_o . '/', $w, 0); echo "OK\n"; } } } } // copy SVGs if ($dir = opendir($path_svg_i)) { while (($file = readdir($dir)) !== false) { $base = pathinfo($file, PATHINFO_BASENAME); $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if ($ext === 'svg') { echo 'Copying ' . $file . '…'; copy($path_svg_i . '/' . $file, $path_svg_o . '/' . $file); echo "OK\n"; } } } // optimize outputs echo 'Optimizing…'; if (Respimg::optimize(__DIR__ . '/generated', 3, 1, 1, 1)) { echo "OK\n"; } else { echo "failed\n"; } echo "Done\n";