//create high quality aspect true thumbnails for all the files in a given directory if ($handle = opendir('path/to/directory/')) { echo "Directory handle: $handle\n"; echo "Processing Entries:\n"; while (false !== ($entry = readdir($handle))) { $isThumb = strpos($entry, 'thumb'); if ($entry != "." && $entry != ".." && $isThumb === FALSE) { //echo "$entry\n";//debugging $filename = $entry; //for readability, now that we know its a valid filename. $width = 175; // maximum width $height = 175; // maximum height header('Content-Type: image/jpeg'); // set content type // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } // Resample image $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Output $newFileName = "thumb-" . $filename; //echo "thumb $newFileName created"; //debug imagejpeg($image_p, $newFileName, 100); imagedestroy($image_p); } } closedir($handle); }