<?php
require_once 'Image/Transform.php';
define('WG_IMAGE_JPG_QUALITY', 90);

/**
 * Fits the image in the specified box size, simply. 
 *
 * If the image is bigger than the box specified by $width and $height,
 * it will be scaled down to fit inside of it.
 * If the image is smaller, nothing is done.
 *
 * We might use this for fixed width thumbnails.
 * @return string|PEAR_Error File name or PEAR_Error object on error
 */
function wg_resizeImage($from_name, $to_name, $max_width, $max_height, $to_type = 'jpg', $quality = WG_IMAGE_JPG_QUALITY) {
  $image =& Image_Transform::factory('');
  $image->load($from_name);
  $x = $image->img_x / $max_width;
  $y = $image->img_y / $max_height;
  if ($x <= 1 && $y <= 1) {
    /* no need to change the dimensions */
    return $from_name;
  }
  $image->fit($max_width, $max_height);
  $result =& $image->save($to_name, $to_type, $quality);
  if (PEAR::isError($result)) {
    return $result;
  }
  return $to_name;
}

/**
 * Fits the image in the specified box size, but with beautiful proportions. 
 *
 * If the image is bigger than the box specified by $width and $height,
 * it will be scaled down to fit inside of it.
 * If the image is smaller, nothing is done.
 * The image is going to be resized to either 1/2, 1/3, 1/4, 1/5, 1/6 or 1/7, etc.
 *
 * @return string|PEAR_Error the file name or PEAR_Error object on error
 */
function wg_resizeImageBeautifulFactor($from_name, $to_name, $max_width, $max_height, $to_type = 'jpg', $quality = WG_IMAGE_JPG_QUALITY) {
  $image =& Image_Transform::factory('');
  $image->load($from_name);
  if ($max_width <= 0 || $max_height <= 0) {
    $error =& PEAR::raiseError("Invalid arguments.", IMAGE_TRANSFORM_ERROR_ARGUMENT);
    return $error;
  }
  $x = $image->img_x / $max_width;
  $y = $image->img_y / $max_height;
  if ($x <= 1 && $y <= 1) {
    /* no need to change the dimensions */
    return $from_name;
  } elseif ($x > $y) {
    $image->scaleByFactor(wg_findBestResizeFactor($image->img_x, $max_width));
  } else {
    $image->scaleByFactor(wg_findBestResizeFactor($image->img_y, $max_height));
  }
  /* inspired from Image_Tranform::fit() */
  $result =& $image->save($to_name, $to_type, $quality);
  if (PEAR::isError($result)) {
    return $result;
  }
  return $to_name;
}
/**
 * @return float Image ratio that will be ok.
 * Will return a fraction where the denominator is either 1/2, 1/3, 1/4, 1/5, 1/6 or 1/7, etc.
 */
function wg_findBestResizeFactor($original_side_size, $max_side_size) {
  //echo " rescaling $original_side_size to $max_side_size\n<br>";
  $result_size = $original_side_size;
  $denominator = 0;

  while ($result_size > $max_side_size) {
    $result_size = $original_side_size / ++$denominator;
    //echo "1/$denominator<br>";
  }
  //  echo "will be $result_size<br>";
  return 1 / $denominator;
}

// test
/*
$result = wg_resizeImage('test.png', 'result.jpg', 200, 200);
if ($result) { echo "success 1\n<br>"; }

$result = wg_resizeImageBeautifulFactor('test.png', 'result_beau.jpg', 320, 240);
if ($result) { echo "success 2<br>"; }
*/
?>