php | thumbnailer

PHP Thumnail Maker

Using Imagick

function topimgthumb($folder,$output_w,$output_h){
  foreach (scandir($folder) as $f){
    $ext = pathinfo($f, PATHINFO_EXTENSION);
    if ( $ext!='jpg' &&  $ext!='jpeg' ){continue;}
    $new_filename = $folder.'/zzz_'.$output_w.'x'.$output_h.'_'.$f;
    if ( is_file($new_filename) && is_writable($new_filename) ){continue;}   // No over-writes.
    $image = new Imagick($folder.'/'.$f);
    $dim = $image->getImageGeometry();
    $orig_w = $dim['width'];
    $orig_h = $dim['height']; 
    if ( $output_w*$orig_h > $output_h*$orig_w) {$scale = $output_h/$orig_h;} 
    else { $scale = $output_w/$orig_w;}
    $new_w =  $orig_w * $scale;
    $new_h =  $orig_h * $scale;
    $image->resizeImage($new_w,$new_h,Imagick::FILTER_CATROM,1);    
    $image->writeImage($new_filename);
    $image->clear();   
  }
}