<?php
	/**
 * @author Gasper Kozak
 * @copyright 2007, 2008, 2009

    This file is part of WideImage.
		
    WideImage is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.
		
    WideImage is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
		
    You should have received a copy of the GNU Lesser General Public License
    along with WideImage; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    * @package Internal/Operations
  **/
	
	/**
	 * An Exception for when an invalid fit method is passed
	 * 
	 * @package Internal/Operations
	 */
	class WideImage_Operation_InvalidFitMethodException extends WideImage_Exception {}
	/**
	 * An Exception for when an invalid resize dimensions are passed
	 * 
	 * @package Internal/Operations
	 */
	class WideImage_Operation_InvalidResizeDimensionException extends WideImage_Exception {}
	
	/**
	 * Resize operation class
	 * 
	 * @package Internal/Operations
	 */
	class WideImage_Operation_Resize
	{
		/**
		 * Prepares and corrects smart coordinates
		 *
		 * @param WideImage_Image $img
		 * @param smart_coordinate $width
		 * @param smart_coordinate $height
		 * @param string $fit
		 * @return array
		 */
		protected function prepareDimensions($img, $width, $height, $fit)
		{
			list($width, $height) = WideImage_Coordinate::fixForResize($img, $width, $height);
			if ($width === 0 || $height === 0)
				return array('width' => 0, 'height' => 0);
			
			if ($fit == null)
				$fit = 'inside';
			
			$dim = array();
			if ($fit == 'fill')
			{
				$dim['width'] = $width;
				$dim['height'] = $height;
			}
			elseif ($fit == 'inside' || $fit == 'outside')
			{
				$rx = $img->getWidth() / $width;
				$ry = $img->getHeight() / $height;
				
				if ($fit == 'inside')
					$ratio = ($rx > $ry) ? $rx : $ry;
				else
					$ratio = ($rx < $ry) ? $rx : $ry;
				
				$dim['width'] = round($img->getWidth() / $ratio);
				$dim['height'] = round($img->getHeight() / $ratio);
			}
			else
				throw new WideImage_Operation_InvalidFitMethodException("{$fit} is not a valid resize-fit method.");
			
			return $dim;
		}
		
		/**
		 * Returns a resized image
		 *
		 * @param WideImage_Image $img
		 * @param smart_coordinate $width
		 * @param smart_coordinate $height
		 * @param string $fit
		 * @param string $scale
		 * @return WideImage_Image
		 */
		function execute($img, $width, $height, $fit, $scale = 'any')
		{
			$dim = $this->prepareDimensions($img, $width, $height, $fit);
			if (($scale === 'down' && ($dim['width'] >= $img->getWidth() && $dim['height'] >= $img->getHeight())) ||
				($scale === 'up' && ($dim['width'] <= $img->getWidth() && $dim['height'] <= $img->getHeight())))
				$dim = array('width' => $img->getWidth(), 'height' => $img->getHeight());
			
			if ($dim['width'] <= 0 || $dim['height'] <= 0)
				throw new WideImage_Operation_InvalidResizeDimensionException("Both dimensions must be larger than 0.");
			
			$new = WideImage_TrueColorImage::create($dim['width'], $dim['height']);
			
			if ($img->isTransparent())
			{
				$new->copyTransparencyFrom($img);
				imagecopyresized(
						$new->getHandle(), 
						$img->getHandle(), 
						0, 0, 0, 0, 
						$new->getWidth(), 
						$new->getHeight(), 
						$img->getWidth(), 
						$img->getHeight()
					);
			}
			else
			{
				$new->alphaBlending(false);
				$new->saveAlpha(true);
				imagecopyresampled(
						$new->getHandle(), 
						$img->getHandle(), 
						0, 0, 0, 0, 
						$new->getWidth(), 
						$new->getHeight(), 
						$img->getWidth(), 
						$img->getHeight()
					);
				$new->alphaBlending(true);
			}
			return $new;
		}
	}
?>