Skip to content

Instantly share code, notes, and snippets.

@chalasr
Last active August 29, 2015 14:19
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 chalasr/f182d2daad03db03be60 to your computer and use it in GitHub Desktop.
Save chalasr/f182d2daad03db03be60 to your computer and use it in GitHub Desktop.
ImageResizing : light and simple script for jpg/png images resizing

imageResize

Light and simple script made for resize all images presents in current folder and recursively. Work on JPEG and PNG images (mixed or not).

#Use

  • Copy the code and paste it in imgResize.php file or download it.
  • Place the file into your image(s) folder (You can have many image(s) folders into).
  • Run it:
    php imgResize.php [new size]

#Example

    php imgResize.php 5 

Result: New width = old width x O.5 New height = height x 0.5 Image ÷ 2

<?php
error_reporting(0);
$dh1 = opendir('.');
while (false !== ($directory = readdir($dh1))) {
if(in_array($directory, ['..', '.', 'imgResize.php', '.DS_STORE']))
continue;
if(is_dir($directory)){
$dh = opendir($directory);
while (false !== ($filename = readdir($dh))) {
if(!is_dir($filename)){
$filename = $directory.DIRECTORY_SEPARATOR.$filename;
$pathinfo = pathinfo($filename, PATHINFO_EXTENSION);
if(in_array($pathinfo, ['jpg', 'jpeg', 'png'])){
$percent = '0.'.$argv[1];
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
if($pathinfo == 'jpeg' || $pathinfo == 'jpg'){
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $filename, 100);
}elseif($pathinfo == 'png'){
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p, $filename);
}
}
}
}
}else{
$filename = $directory;
$pathinfo = pathinfo($filename, PATHINFO_EXTENSION);
$percent = '0.'.$argv[1];
if(in_array($pathinfo, ['jpg', 'jpeg', 'png'])){
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
if($pathinfo == 'jpeg' || $pathinfo == 'jpg'){
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $filename, 100);
}elseif($pathinfo == 'png'){
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p, $filename);
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment