<?php
define('IMAGEPATH', 'uploads/items_photos');

if (is_dir(IMAGEPATH)){
    $handle = opendir(IMAGEPATH);
}
else{
    echo 'No image directory';
}

$directoryfiles = array();
while (($file = readdir($handle)) !== false) {
    $newfile = str_replace(' ', '_', $file);
    rename(IMAGEPATH . $file, IMAGEPATH . $newfile);
    $directoryfiles[] = $newfile;
}

foreach($directoryfiles as $directoryfile){
    if(strlen($directoryfile) > 3){
        compressedImage("uploads/items_photos/$directoryfile", "uploads/items_photos/$directoryfile", "uploads/items_photos2/$directoryfile", 25);
         echo "<BR>Compressed $directoryfile";
    }
}

closedir($handle);




    // Compress image
function compressedImage($source, $path,$path2, $quality) {
    echo $source;
    $info = getimagesize($source);
    var_dump($info);
    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);
    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);
    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);

        echo "<BR>" . $width = $info[0]/5;
        echo "<BR>" . $height = $info[1]/5;
    $new_image = imagecreatetruecolor($width,$height );
    imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
    echo "<BR>ok"; 
    echo "<BR>";
    imagejpeg($new_image, $path2, $quality);
 
}

?>

 