<?php

use Vn\Lib\UserException;

class Image
{	
	/**
	 * Creates an image resource from a valid image file.
	 *
	 * @param string $srcFile The source file name
	 **/
	static function create ($srcFile)
	{
		$imageType = exif_imagetype ($srcFile);

		if ($imageType !== FALSE)
		switch ($imageType)
		{
			case IMAGETYPE_JPEG:
				$image = imagecreatefromjpeg ($srcFile);
				break;
			case IMAGETYPE_PNG:
				$image = imagecreatefrompng ($srcFile);
				break;
			case IMAGETYPE_GIF:
				$image = imagecreatefromgif ($srcFile);
				break;
			default:
				throw new UserException (s('Bad file format'));
		}
		else
			throw new UserException (s('Image open error'));
			
		return $image;
	}
	
	/**
	 * Resizes and saves an image resource.
	 *
	 * @param resource $image The image resource
	 * @param string $dstFile The destination file name
	 * @param integer $maxHeight The maximum height of resized image in pixels
	 * @param integer $maxWidth The maximum width of resized image in pixels
	 * @param boolean $crop Wether to crop the image
	 * @param boolean $symbolicSrc If it is not necessary to resize the image creates a symbolic link using the passed path as source
	 **/
	static function resizeSave ($image, $dstFile, $maxHeight, $maxWidth, $crop = FALSE, $symbolicSrc = NULL)
	{
		$width = imagesx ($image);
		$height = imagesy ($image);

		$dirname = dirname ($dstFile);

		if (!is_dir ($dirname))
			mkdir ($dirname, 0775, TRUE);
		
		if (file_exists ($dstFile))
			@unlink ($dstFile);
	
		// Check if it is necessary to resize the image
		
		if ($height > $maxHeight || $width > $maxWidth)
		{
			$srcX = 0;
			$srcY = 0;
			$srcWidth = $width;
			$srcHeight = $height;
			$dstWidth = $width;
			$dstHeight = $height;

			if (!$crop) // Resize
			{
				$ratio = NULL;

				if ($dstWidth > $maxWidth)
				{
					$ratio = $dstWidth / $maxWidth;
					$dstWidth = $maxWidth;
					$dstHeight = (int) ($dstHeight / $ratio);
				}

				if ($dstHeight > $maxHeight)
				{
					$ratio = $dstHeight / $maxHeight;
					$dstHeight = $maxHeight;
					$dstWidth = (int) ($dstWidth / $ratio);
				}
			}
			else // Cut & resize
			{
				if ($width > $maxWidth)
					$dstWidth = $maxWidth;
				if ($height > $maxWidth)
					$dstHeight = $maxHeight;

				if ($width <= $maxWidth)
				{
					if ($height > $srcHeight)
						$srcHeight = $maxHeight;
				}
				elseif ($height <= $maxHeight)
				{
					if ($width > $maxWidth)
						$srcWidth = $maxWidth;
				}
				else
				{
					$srcWidth = (int) ($maxWidth * ($height / $maxHeight));
					$srcHeight = (int) ($maxHeight * ($width / $maxWidth));
				
					if ($srcWidth <= $width)
						$srcHeight = $height;
					else
						$srcWidth = $width;
				}
			
				if ($width !== $srcWidth)
					$srcX = (int) (($width / 2) - ($srcWidth / 2));

				if ($height !== $srcHeight)
					$srcY = (int) (($height / 2) - ($srcHeight / 2));
			}
			
			$resizedImage = imagecreatetruecolor ($dstWidth, $dstHeight);
			imagealphablending ($resizedImage, FALSE);
			imagesavealpha ($resizedImage, TRUE);
			imagecopyresampled ($resizedImage, $image,
				0, 0, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
			$saved = imagepng ($resizedImage, $dstFile);
			imagedestroy ($resizedImage);
		}
		elseif (isset ($symbolicSrc))
		{
			$saved = symlink ($symbolicSrc, $dstFile);
		}
		else
		{
			imagesavealpha ($image, TRUE);
			$saved = imagepng ($image, $dstFile);
		}
		
		if (!$saved)
			throw new UserException (sprintf (s('File save error: %s'), $dstFile));
	}
}