Create new file and put below code in this file.
// PHP Configuration
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 30000);
//Initial settings, Just specify Source and Destination Image folder.
$file_dir = 'E:\demo image';
$save_dir_name = 'New Folder';
$fixedWidth = 650;
$fixedHeight = 650;
$maxWidth = 650;
$maxHeight = 650;
check_is_dir_or_file($file_dir);
function check_is_dir_or_file($file_dir){
$files = array();
global $save_dir_name;
$folder = opendir($file_dir);
while ( $file = readdir($folder) ) {
if ( $file[0] != '.' && $file[0] != '..' ){
$dir = $file_dir.'\\'.$file;
if(is_dir( $dir ) && $file != $save_dir_name){
check_is_dir_or_file($dir);
}else{
$files[$file] = $file;
}
}
}
if($files){
image_resize($files,$file_dir);
}
}
function image_resize($files,$dir){
global $save_dir_name, $maxWidth, $maxHeight ,$fixedWidth, $fixedHeight;
$oldDir = $dir;
$newDir = $oldDir.'\\'.$save_dir_name;
$quality = 100;
if ( !file_exists($newDir) )
mkdir($newDir);
echo "
";
echo $oldDir;
foreach ( $files as $key => $value ) {
$ext = strtolower(pathinfo($key, PATHINFO_EXTENSION));
if ( $ext == 'jpg' or $ext == 'jpeg' or $ext == 'png' ) {
if ( resizer($key, $maxWidth, $maxHeight, $fixedWidth, $fixedHeight, $oldDir, $newDir, $quality) )
echo '*Resized image: ', $key, '*';
else
echo '** Failed to resize image: ', $key, ' **';
} else {
if($key != $save_dir_name){
echo '** ', $key, ' is not a jpeg or png **';
}
}
}
}
function resizer($fileName, $maxWidth, $maxHeight, $fixedWidth, $fixedHeight, $oldDir, $newDir, $quality) {
$file = $oldDir.'/'.$fileName;
//print_r($file);exit;
$fileDest = $newDir.'/'.$fileName;
list($width, $height) = getimagesize($file);
if ( $fixedWidth ) {
$newWidth = $fixedWidth;
$newHeight = ($newWidth / $width) * $height;
} elseif ( $fixedHeight ) {
$newHeight = $fixedHeight;
$newWidth = ($newHeight / $height) * $width;
} elseif ( $width < $height ) { // image is portrait
$newHeight = $maxHeight;
$newWidth = ($newHeight / $height) * $width;
} elseif ( $width > $height ) { // image is landscape
$newWidth = $maxWidth;
$newHeight = ($newWidth / $width) * $height;
} else { // image is square
$newWidth = $maxHeight;
$newHeight = $maxHeight;
}
$extn = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$imageDest = imagecreatetruecolor($newWidth, $newHeight);
// jpeg
if ( $extn == 'jpg' or $extn == 'jpeg' ) {
$imageSrc = imagecreatefromjpeg($file);
if ( imagecopyresampled($imageDest, $imageSrc, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) ) {
imagejpeg($imageDest, $fileDest, $quality);
imagedestroy($imageSrc);
imagedestroy($imageDest);
return true;
}
return false;
}
// png
if ( $extn == 'png' ) {
imagealphablending($imageDest, false);
imagesavealpha($imageDest, true);
$imageSrc = imagecreatefrompng($file);
if ( imagecopyresampled($imageDest, $imageSrc, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) ) {
imagepng($imageDest, $fileDest, ($quality / 10) - 1);
imagedestroy($imageSrc);
imagedestroy($imageDest);
return true;
}
return false;
}
}