Skip to content

Instantly share code, notes, and snippets.

@pranid
Last active November 20, 2017 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranid/25917edecdffb19695b2a2815dfee1b4 to your computer and use it in GitHub Desktop.
Save pranid/25917edecdffb19695b2a2815dfee1b4 to your computer and use it in GitHub Desktop.
Bulk Images Compressor for Servers
<?php
function getFolderFiles($dir){
$quality = 60;
$scan_dir = scandir($dir);
echo '<ol>';
foreach($scan_dir as $file_path){
$destination = $dir."\/".$file_path;
$source = $dir."\/".$file_path;
if($file_path != '.' && $file_path != '..'){
echo '<li>'.$file_path;
if(is_dir($dir.'/'.$file_path)) {
getFolderFiles($dir.'/'.$file_path);
} else {
compress($source, $destination, $quality);
}
echo '</li>';
}
}
echo '</ol>';
}
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
/*elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);*/
if(isset($image)) {
imagejpeg($image, $destination, $quality);
return $destination;
}
}
getFolderFiles('You media Folder Path Here');
?>
@shansana
Copy link

Works perfectly. Thank you. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment