/**
 * RezImgCrop
 * Copyright 2011 by Artdevue.com  <info@artdevue.com>
 * Arguings at a forum http://community.modx-cms.ru/
 * @package rezimgcrop
 */
/**
 * RezImgCrop
 * A dynamic form processing Snippet for MODx Revolution 2.0.
 *
 * @package rezimgcrop
 */
$percent = false;
list($w_i, $h_i, $type) = getimagesize($file_input);
  if (!$w_i || !$h_i) {
    return $file_input;
    }
    $types = array('','gif','jpeg','png');
    $ext = $types[$type];
    if ($ext) {
      $func = 'imagecreatefrom'.$ext;
      $img = $func($file_input);
    } else {
    return $file_input;
    }
  if ($percent) {
    $w_o *= $w_i / 100;
    $h_o *= $h_i / 100;
  }
  if (!$h_o) $h_o = $w_o/($w_i/$h_i);
  if (!$w_o) $w_o = $h_o/($h_i/$w_i);
  $img_o = imagecreatetruecolor($w_o, $h_o);
  if ($type == 3) {
  imagesavealpha($img_o, true);
  $trans_colour = imagecolorallocatealpha($img_o, 255, 255, 255, 127);
  imagefill($img_o, 0, 0, $trans_colour);
  }
  imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o, $h_o, $w_i, $h_i);
  if ($type == 2) {
    imagejpeg($img_o,$file_output,100);
    imagedestroy($img_o);
  } else {
    $func = 'image'.$ext;
    return $func($img_o,$file_output);
  }