Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Razbakov committed Feb 1, 2017
0 parents commit 2b03c88
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "kirchbergernorr/optimize-images",
"require": {
"ps/image-optimizer": "^1.0"
},
"bin": ["optimize-images.php"]
}
51 changes: 51 additions & 0 deletions optimize-images.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/php
<?php

require 'vendor/autoload.php';

function optimizeImage($filepath)
{
$factory = new \ImageOptimizer\OptimizerFactory();
$optimizer = $factory->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";
}

0 comments on commit 2b03c88

Please sign in to comment.