<?

$thumb_file = "-thumb.jpg";

$image = _resize_image(
         'testing_crop.jpg', 
         $thumb_file, 
         125, 
         'auto'
      );
var_export($image);



// methods can be 'fit', 'stretch', or 'auto'.
function _resize_image($source_file, $dest_file, $width, $height, 
   $quality = 90, $method = "lkjlkjlk", $upscale = "yes")
{
   $ret = array();
   $image = false;
   switch(exif_imagetype($source_file)) {
      case IMAGETYPE_GIF:
         $image = imagecreatefromgif ($source_file);
         break;
      case IMAGETYPE_JPEG:
         $image = imagecreatefromjpeg($source_file);
         break;
      case IMAGETYPE_PNG:
         $image = imagecreatefrompng($source_file);
         break;
      case IMAGETYPE_BMP:
         // don't think PHP can create an image from a BMP
         // will need to find a third-party function for this if anyone cares about BMPs anymore
         break;
   }
   
   // file not a supported image type
   if ($image === false) {
      return false;
   }
   
   $imagesize = getimagesize($source_file);

   if ($imagesize === false) {
      return false;
   }

   $max_width    = PHP_INT_MAX;
   $max_height   = PHP_INT_MAX;
   $image_width  = $imagesize[0];
   $image_height = $imagesize[1];
   
   // invalid dimensions specified, don't bother attempting to resize
   // note: possible that only width or height is specified, don't need both
   if ($width <= 0 && $height <= 0) {
      copy($source_file, $dest_file);
      $ret['width']  = round($image_width);
      $ret['height'] = round($image_height);
      return $ret;
   }
   
   if ($width > 0) {
      $max_width = $width;
   }
   if ($height > 0) {
      $max_height = $height;
   }
   
   $dim = _get_resize_dimensions($image_width, $image_height, $max_width, $max_height, $method, $upscale);
   
   if ($dim['copy']) {
      copy($source_file, $dest_file);
      $ret['width']  = round($image_width);
      $ret['height'] = round($image_height);
   } else {
      $resized_image = imagecreatetruecolor($dim['dw'], $dim['dh']);
      $alpha_image = false;
      
      switch($imagesize[2]) {
         case IMAGETYPE_GIF:
            _set_transparency($resized_image, $image);
            break;
         case IMAGETYPE_PNG:
            imagealphablending($resized_image, false);
            imagesavealpha($resized_image, true);
            
            $alpha_image = imagecreatetruecolor($image_width, $image_height);
            imagealphablending($alpha_image, false);
            imagesavealpha($alpha_image, true);
            
            for ($x = 0; $x < $image_width; $x++) {
               for ($y = 0; $y < $image_height; $y++) {
                  $alpha = (imagecolorat($image, $x, $y) >> 24) & 0xFF;
                  $color = imagecolorallocatealpha($alpha_image, 0, 0, 0, $alpha);
                  imagesetpixel($alpha_image, $x, $y, $color);
               }
            }
            
            break;
      }
      
      imagegammacorrect($image, 2.2, 1.0);
      imagecopyresampled($resized_image, $image, 0, 0, $dim['sx'], $dim['sy'], $dim['dw'], $dim['dh'], $dim['sw'], $dim['sh']);
      imagegammacorrect($resized_image, 1.0, 2.2);
      
      imagedestroy($image);
      
      switch ($imagesize[2]) {
         case IMAGETYPE_GIF:
            imagegif ($resized_image, $dest_file);
            break;
         case IMAGETYPE_JPEG:
            imagejpeg($resized_image, $dest_file, $quality);
            break;
         case IMAGETYPE_PNG:
            $alpha_resized_image = imagecreatetruecolor($dim['dw'], $dim['dh']);
            imagealphablending($alpha_resized_image, false);
            imagesavealpha($alpha_resized_image, true);
            imagecopyresampled($alpha_resized_image, $alpha_image, 0, 0, $dim['sx'], $dim['sy'], $dim['dw'], $dim['dh'], $dim['sw'], $dim['sh']);
            
            for($x = 0; $x < $dim['dw']; $x++) {
               for($y = 0; $y < $dim['dh']; $y++) {
                  $alpha = (imagecolorat($alpha_resized_image, $x, $y) >> 24) & 0xFF;
                  $rgb = imagecolorat($resized_image, $x, $y);
                  $r = ($rgb >> 16) & 0xFF;
                  $g = ($rgb >> 8) & 0xFF;
                  $b = $rgb & 0xFF;
                  $color = imagecolorallocatealpha($resized_image, $r, $g, $b, $alpha);
                  imagesetpixel($resized_image, $x, $y, $color);
               }
            }
            
            imagedestroy($alpha_image);
            imagedestroy($alpha_resized_image);
            
            imagepng($resized_image, $dest_file);
            break;
      }
      
      imagedestroy($resized_image);
      
      $ret['width']  = $dim['dw'];
      $ret['height'] = $dim['dh'];
   }

   return $ret;
}

function _get_resize_dimensions($iw, $ih, $mw, $mh, $method = "fit", $upscale = "no") {
   $r = array();
   $r['copy'] = false;
   
   if ($method == "fill" && $mw < PHP_INT_MAX && $mh < PHP_INT_MAX) {
      if ($upscale == "yes" || ($iw > $mw && $ih > $mh)) {
         $ratio_width  = $iw / $mw;
         $ratio_height = $ih / $mh;

         $scale_by = $ratio_width <= $ratio_height ? $ratio_width : $ratio_height;

         $w = $iw  / $scale_by;
         $h = $ih / $scale_by;
         
         $r['sx'] = floor(($w - $mw) / 2 * $scale_by);
         $r['sy'] = floor(($h - $mh) / 2 * $scale_by);
         
         $r['dw'] = $mw;
         $r['dh'] = $mh;
         
         $r['sw'] = floor($mw * $scale_by);
         $r['sh'] = floor($mh * $scale_by);
         
         return $r;
      } else {
         $r['copy'] = true;
         return $r;
      }
   } else if ($method == "stretch" && $mw < PHP_INT_MAX && $mh < PHP_INT_MAX) {
      if ($upscale == "yes" || $iw > $mw || $ih > mh) {
         $r['sx'] = 0;
         $r['sy'] = 0;

         $r['dw'] = $mw;
         $r['dh'] = $mh;

         $r['sw'] = $iw;
         $r['sh'] = $ih;

         return $r;  
      } else {
         $r['copy'] = true;
         return $r;
      }
   } else {
      if ($upscale == "yes" || $iw > $mw || $ih > $mh) {
         $ratio_width  = $iw / $mw;
         $ratio_height = $ih / $mh;
         
         $scale_by = $ratio_width >= $ratio_height ? $ratio_width : $ratio_height;
         
         $r['sx'] = 0;
         $r['sy'] = 0;
         
         $r['dw'] = floor($iw / $scale_by);
         $r['dh'] = floor($ih / $scale_by);
         
         $r['sw'] = $iw;
         $r['sh'] = $ih;
         
         return $r;
      } else {
         $r['copy'] = true;
         return $r;
      }
   }
}

function _set_transparency($new_image, $image_source) 
{ 
   $transparencyIndex = imagecolortransparent($image_source);
   $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255); 

   if ($transparencyIndex >= 0) { 
      $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);
   }

   $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); 
   imagefill($new_image, 0, 0, $transparencyIndex); 
   imagecolortransparent($new_image, $transparencyIndex);
}