|
<?php |
|
use Spatie\ImageOptimizer\OptimizerChainFactory; |
|
use Monolog\Logger; |
|
use Monolog\Handler\StreamHandler; |
|
|
|
// sanitize root directory separator |
|
$root = rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR); |
|
$root = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $root) . DIRECTORY_SEPARATOR; |
|
define('ROOT', $root); |
|
|
|
define('COMPOSER_AUTOLOAD', ROOT . 'vendor/autoload.php'); |
|
|
|
function listFolders($dir) |
|
{ |
|
$output_directories = array(); |
|
$ffs = scandir($dir); |
|
|
|
unset($ffs[array_search('.', $ffs, true)]); |
|
unset($ffs[array_search('..', $ffs, true)]); |
|
|
|
// prevent empty ordered elements |
|
if (count($ffs) < 1) { |
|
return; |
|
} |
|
|
|
foreach ($ffs as $ff) { |
|
if (is_dir($dir.'/'.$ff)) { |
|
$output_directories[] = $dir.'/'.$ff; |
|
$sub = listFolders($dir.'/'.$ff); |
|
if (!empty($sub)) { |
|
$output_directories = array_merge($output_directories, $sub); |
|
} |
|
} |
|
} |
|
|
|
return $output_directories; |
|
} |
|
|
|
function listFiles($dir) |
|
{ |
|
$output_files = array(); |
|
$ffs = scandir($dir . '/'); |
|
|
|
unset($ffs[array_search('.', $ffs, true)]); |
|
unset($ffs[array_search('..', $ffs, true)]); |
|
|
|
// prevent empty ordered elements |
|
if (count($ffs) < 1) { |
|
return; |
|
} |
|
|
|
foreach ($ffs as $ff) { |
|
if (!is_dir($dir.'/'.$ff)) { |
|
$output_files[] = $dir.'/'.$ff; |
|
} |
|
} |
|
|
|
return $output_files; |
|
} |
|
|
|
function optimizeImages($file) |
|
{ |
|
$output = array( |
|
'html' => '', |
|
'saved_kb' => 0 |
|
); |
|
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); |
|
$detectedType = exif_imagetype($file); |
|
if (in_array($detectedType, $allowedTypes)) { |
|
$origin_filesize = filesize($file)/1000; |
|
$output['html'] = '<small class="d-inline-block w-75 pr-2">' . str_replace(ROOT, '', $file) . ': ' . $origin_filesize . 'kb => '; |
|
include_once COMPOSER_AUTOLOAD; |
|
|
|
$optimizerChain = OptimizerChainFactory::create(); |
|
// $logger = new Logger('my_logger'); |
|
// $logger->pushHandler(new StreamHandler(ROOT . 'log/image-optimizer.log', Logger::DEBUG)); |
|
$optimizerChain |
|
// ->useLogger($logger) |
|
->optimize($file); |
|
|
|
$dest_filesize = filesize($file)/1000; |
|
$saved_kb = number_format($origin_filesize - $dest_filesize, 2); |
|
$output['html'] .= $dest_filesize . 'kb</small>'; |
|
if ($saved_kb > 0) { |
|
$output['html'] .= '<small class="d-inline-block w-25 text-success">- ' . $saved_kb . 'kb</small>'; |
|
$output['saved_kb'] = $saved_kb; |
|
} else { |
|
$output['html'] .= '<small class="d-inline-block w-25 text-muted">[already optimized]</small>'; |
|
} |
|
$output['html'] .= '<br>'; |
|
|
|
echo json_encode($output); |
|
} |
|
} |
|
|
|
|
|
if (isset($_POST['main_dir'])) { |
|
/*================================================= |
|
= 1st STEP: GET DIRECTORIES = |
|
=================================================*/ |
|
|
|
$dir = $_POST['main_dir']; |
|
$output = listFolders($dir); |
|
|
|
echo json_encode($output); |
|
} elseif (isset($_POST['img_dir'])) { |
|
/*=========================================== |
|
= 2nd STEP: GET FILES = |
|
===========================================*/ |
|
|
|
$dir = $_POST['img_dir']; |
|
$output = listFiles($dir); |
|
|
|
echo json_encode($output); |
|
} elseif (isset($_POST['file'])) { |
|
/*=========================================== |
|
= 3rd STEP: OPTIMIZE IMAGES = |
|
===========================================*/ |
|
|
|
$file = $_POST['file']; |
|
$output = optimizeImages($file); |
|
|
|
echo $output; |
|
} |