From b73eb5d6747fc181de86b2de50fb158dff463618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Mon, 9 Jan 2017 23:58:20 +0000 Subject: [PATCH] Recursive directory processing example --- examples/optimize_directory.php | 164 ++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100755 examples/optimize_directory.php diff --git a/examples/optimize_directory.php b/examples/optimize_directory.php new file mode 100755 index 0000000..354c1eb --- /dev/null +++ b/examples/optimize_directory.php @@ -0,0 +1,164 @@ +#!/usr/bin/env php + /www/example.com/optimized/hello-640.png\n\n"; + echo "If you have questions, ask support@imageoptim.com\n"; +} + +if (count($_SERVER['argv']) < 4) { // the arg 0 is the command name + usage(); + exit(1); +} + +$argn = 1; +$apiUsername = $_SERVER['argv'][$argn++]; +if (!$apiUsername || ctype_digit($apiUsername) || file_exists($apiUsername)) { + echo "The first argument (". escapeshellarg($apiUsername) . ") must be an ImageOptim API username.\n"; + echo "Get your username from https://imageoptim.com/api/register\n"; + exit(1); +} + +$width = null; +if (count($_SERVER['argv']) > 4 && ctype_digit($_SERVER['argv'][$argn])) { + $width = $_SERVER['argv'][$argn++]; +} + +$sourceDir = $_SERVER['argv'][$argn++]; +if (!is_dir($sourceDir)) { + echo "ERROR: ", $sourceDir, " does not exist or is not a directory.\n\n"; + usage(); + exit(1); +} + +$destDir = $_SERVER['argv'][$argn++]; +if (!is_dir($destDir)) { + if (is_dir(dirname($destDir))) { + if (!mkdir($destDir)) { + echo "ERROR: can't create ", $destDir, ". Please create this directory first.\n"; + exit(1); + } + } else { + echo "ERROR: ", $destDir, " does not exist or is not a directory.\n\n"; + usage(); + exit(1); + } +} + +// Clears symlinks from paths, makes them absolute and comparable +$sourceDir = realpath($sourceDir); +$destDir = realpath($destDir); + +try { + $api = new ImageOptim\API($apiUsername); + + // This is a fancy way of getting a list of all files in a directory + $items = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($sourceDir, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::SELF_FIRST, + RecursiveIteratorIterator::CATCH_GET_CHILD); + + $nonImage = []; + $skipped = 0; + $done = 0; + + foreach ($items as $item) { + if ($item->isDir()) continue; + $filename = $item->getFilename(); + if (!preg_match('/\.(png|jpe?g|gif|svgz?|bmp|tiff?)/i', $filename)) { + $nonImage[] = $filename; + continue; + } + + $sourcePath = $item->getPathname(); + $destRelPath = substr($sourcePath, strlen($sourceDir)); + $destPath = $destDir . $destRelPath; + + // Append .min extension if source and destination are the same + if ($destPath === $sourcePath && false === strpos($destRelPath, '.min.')) { + $destRelPath = preg_replace('/\.[^.]+$/', '.min$0', $destRelPath); + $destPath = $destDir . $destRelPath; + } + + echo substr($destRelPath,1),"... "; + + if (file_exists($destPath) && filemtime($destPath) > filemtime($sourcePath)) { + echo " already exists (skipped)\n"; + $skipped++; + continue; + } + + // The process preserves directory structure, so it needs to create dirs + $destSubdir = dirname($destPath); + if (!is_dir($destSubdir)) { + if (!mkdir($destSubdir, 0777, true)) { + echo "error: unable to create", $destSubdir,"\n"; + continue; + } + } + + $apiRequest = $api->imageFromPath($sourcePath); + if ($width) { + // You could add more options here + $apiRequest->resize($width); + } + $data = $apiRequest->getBytes(); + if (!file_put_contents($destPath, $data)) { + echo "ERROR: unable to save file $destPath\n"; + break; + } + + $inSize = filesize($sourcePath); + $outSize = strlen($data); + echo "ok (", ($inSize > $outSize ? "$inSize -> $outSize bytes" : "already optimized"), ")\n"; + $done++; + } + + if (count($nonImage)) { + echo "Skipped ", count($nonImage), " non-image file(s) ", implode(', ', array_slice($nonImage, 0, 50)), "\n"; + $nonImage = []; + } + + if ($skipped) { + echo "\nSkipped $skipped alredy-existing file(s) in $destDir"; + } + echo "\nImageOptim API processed $done file(s)\n"; + +} catch(\ImageOptim\AccessDeniedException $e) { + echo "ERROR\n\n"; + echo "Please got to https://imageoptim.com/api/register\n"; + echo "get your API username, and replace '$apiUsername' with\n"; + echo "your new registered API username.\n\n"; + echo $e; + exit(1); +} catch(\Exception $e) { + echo "ERROR\n\n"; + echo $e; + exit(1); +}