diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c5afe0c --- /dev/null +++ b/composer.json @@ -0,0 +1,7 @@ +{ + "name": "kirchbergernorr/optimize-images", + "require": { + "ps/image-optimizer": "^1.0" + }, + "bin": ["optimize-images.php"] +} \ No newline at end of file diff --git a/optimize-images.php b/optimize-images.php new file mode 100755 index 0000000..fa95310 --- /dev/null +++ b/optimize-images.php @@ -0,0 +1,51 @@ +#!/usr/bin/php +get(); + $optimizer->optimize($filepath); +} + +function recurseImages($dir, &$initialBytes, &$bytesSaved) +{ + $images = $initialBytes = $bytesSaved = 0; + $stack[] = $dir; + + while ($stack) { + sort($stack); + $thisdir = array_pop($stack); + if ($dircont = scandir($thisdir)) { + for ($i = 0; isset($dircont[$i]); $i++) { + if ($dircont[$i]{0} !== '.') { + $current_file = $thisdir .'/'. $dircont[$i]; + if (!is_link($current_file)) { + if (is_dir($current_file)) { + $stack[] = $current_file; + } else { + $initialBytes += filesize($current_file); + optimizeImage($current_file); + $bytesSaved += filesize($current_file); + $images++; + } + } + } + } + } + } + return $images; +} + +if (is_dir($argv[1])) { + $time = time(); + $initialBytes = $bytesSaved = 0; + $path = realpath($argv[1]); + + $images = recurseImages($path, $initialBytes, $bytesSaved); + + $time = time() - $time; + echo "Time: {$time}s\tImages: $images\tInitial bytes: $initialBytes\tBytes saved: $bytesSaved (". round($bytesSaved / $initialBytes * 100, 1) ."%)\n"; +}