
<?php
/**
 * Image manipulation class. Create thumbnails or add watermarks to an image
 * Created for easy usage
 *
 * @author	Sean Koole
 * @access	public
 */
class Image
{
	/**
	 * Create a thumbnail
	 *
	 * @param	string $imgPath			The path of the image
	 * @param	string $newPath			The path to where the image should be copied to
	 * @param	bool $overwrite			Do you wish to overwrite the file if it already exists
	 * @param	int $imgMaxWidth		The maximum width of the thumbnail
	 * @param	int $imgMaxHeight		The maximum height of the thumbnail
	 * @param	int $quality			The quality of the image (100 is max and best)
	 * @return	bool
	 * @access	public
	 */
	function CreateThumb ($imgPath, $newPath, $overwrite = false, $imgMaxWidth = 100, $imgMaxHeight = 100, $quality = 100)
	{
		/**
		 * Basic checks
		 * - Check if the maxwidth and maxheight are integers
		 * - Check if the image exists to be thumbnailed
		 * - Check if we need to overwrite or not, if necessary
		 * - Check if we have gd enabled
		 */
		if (!ctype_digit ($imgMaxWidth) || !ctype_digit ($imgMaxHeight))
			return false;
		if (!file_exists ($imgPath))
			return false;
		if (file_exists ($newPath) && ($overwrite == false || $overwrite == ''))
			return false;
		if (!extension_loaded ('gd'))
			return false;

		/**
		 * Store the information in an array for easy use
		 */
		$thumb						= array ();
		$thumb['imgOldPath']		= $imgPath;
		$thumb['imgNewPath']		= $newPath;
		$thumb['imgMaxWidth']		= $imgMaxWidth;
		$thumb['imgMaxHeight']		= $imgMaxHeight;
		$thumb['imgQuality']		= $quality;
		$thumb['imgExtension']		= strtolower (end(explode ('.', $thumb['imgOldPath'])));

		list ($thumb['imgWidth'], $thumb['imgHeight']) = getimagesize ($thumb['imgOldPath']);

		/**
		 * Create the new height and width for the thumbnail image
		 */
		$ratio	=  ($thumb['imgWidth'] > $thumb['imgMaxWidth']) ? (real)($thumb['imgMaxWidth'] / $thumb['imgWidth']) : 1;
		$width	= ((int)($thumb['imgWidth'] * $ratio));
		$height	= ((int)($thumb['imgHeight'] * $ratio));

    	$ratio	=  ($height > $height) ? (real)($thumb['imgMaxHeight'] / $height) : 1;
    	$width	= ((int)($width * $ratio));
    	$height	= ((int)($height * $ratio));

		/**
		 * Choose the functions to use
		 */
		if ($thumb['imgExtension'] == 'jpg' || $thumb['imgExtension'] == 'jpeg' || $thumb['imgExtension'] == 'jpe' || $thumb['imgExtension'] == 'jfif')
		{
			$thumb['imgExtension'] = 'jpg';
			$func1 = 'imagecreatefromjpeg';
			$func2 = 'imagejpeg';
		}
		else if ($thumb['imgExtension'] == 'gif')
		{
			$thumb['imgExtension'] = 'gif';
			$func1 = 'imagecreatefromgif';
			$func2 = 'imagegif';
		}
		else if ($thumb['imgExtension'] == 'png')
		{
			$thumb['imgExtension'] = 'png';
			$func1 = 'imagecreatefrompng';
			$func2 = 'imagepng';
		}
		else
			return false;

		/**
		 * Check if we can manipulate this image
		 */
		$check = defined ('IMG_' . strtoupper ($thumb['imgExtension'])) ? constant ('IMG_' . strtoupper ($thumb['imgExtension'])) : 0;
		if ($check == 0)
			return false;

		/**
		 * Create the thumbnail and stuff
		 */
		$img_new	= imagecreatetruecolor ($width, $height);
		$img_old	= $func1 ($thumb['imgOldPath']);
		imagecopyresampled ($img_new, $img_old, 0, 0, 0, 0, $width, $height, $thumb['imgWidth'], $thumb['imgHeight']);
		$func2 ($img_new, $thumb['imgNewPath'], $thumb['imgQuality']);
		imagedestroy ($img_new);
		imagedestroy ($img_old);

		/**
		 * Return true
		 */
		return true;
	}


	/**
	 * Add a watermark to an image
	 *
	 * @param	string $image			The path of the image
	 * @param	string $watermark		The path of the watermark image
	 * @param	bool $transparency		The transparancy level of the image
	 * @param	int $wposition			The amount of pixels the watermark should be placed from the left of the image
	 * @param	int $hposition			The amount of pixels the watermark should be placed from the top of the image
	 * @return	bool
	 * @access	public
	 */
	function AddWatermark ($image, $watermark, $transparency = 16, $wposition = 0, $hposition = 0)
	{
		/**
		 * Check if the image and the watermark image exist
		 */
		if (!file_exists ($image))
			return trigger_error ('Could not add watermark to image ' . $_IMG['IMGName'] . ', image file does not exist');
		if (!file_exists ($watermark))
			return trigger_error ('Could not add watermark to image ' . $_IMG['IMGName'] . ', watermark file does not exist');
	
		/**
		 * Store the information in an array for easy use
		 */
		$_IMG	= array ();
		$_IMG['transparency']	= $transparency;
		$_IMG['image']			= $image;
		$_IMG['watermark']		= $watermark;
		$_IMG['wposition']		= $wposition;
		$_IMG['hposition']		= $hposition;

		$_EXT	= array ();
		$_EXT['image']			= end (explode ('.', strtolower ($_IMG['image'])));
		$_EXT['watermark']		= end (explode ('.', strtolower ($_IMG['watermark'])));

		/**
		 * Make sure we use the correct things
		 */
		switch ($_EXT['image'])
		{
			case 'jpg':
				$_IMG['iinfo']	= array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
			break;
			case 'gif':
				$_IMG['iinfo']	= array ('image/gif', 'imagecreatefromgif', 'imagegif');
			break;
			case 'png':
				$_IMG['iinfo']	= array ('image/png', 'imagecreatefrompng', 'imagepng');
			break;
			default:
				$_IMG['iinfo']	= array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
			break;
		}
		switch ($_EXT['watermark'])
		{
			case 'jpg':
				$_IMG['winfo']	= array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
			break;
			case 'gif':
				$_IMG['winfo']	= array ('image/gif', 'imagecreatefromgif', 'imagegif');
			break;
			case 'png':
				$_IMG['winfo']	= array ('image/png', 'imagecreatefrompng', 'imagepng');
			break;
			default:
				$_IMG['winfo']	= array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
			break;
		}

		/**
		 * Create the images
		 */
		$_IMG['image_mark']		= $_IMG['winfo'][1] ($_IMG['watermark']); 
		$_IMG['image_image']	= $_IMG['iinfo'][1] ($_IMG['image']); 

		/**
		 * Set the correct widht/height etc...
		 */
		$_IMG['image_imagew']	= imagesx ($_IMG['image_image']); 
		$_IMG['image_imageh']	= imagesy ($_IMG['image_image']); 
		$_IMG['image_markw']	= imagesx ($_IMG['image_mark']); 
		$_IMG['image_markh']	= imagesy ($_IMG['image_mark']); 

		/**
		 * Make sure we use the correct position of the watermark
		 */
		if ($_IMG['hposition'] == 0)
			$_IMG['hposition']	= ($_IMG['image_imageh'] - $_IMG['image_markh']);

		if ($_IMG['wposition'] == 0)
			$_IMG['wposition']	= 5;

		/**
		 * Create the watermark
		 */
		imagecopymerge ($_IMG['image_image'], $_IMG['image_mark'], $_IMG['wposition'], $_IMG['hposition'], 0, 0, $_IMG['image_markw'], $_IMG['image_markh'], $_IMG['transparency']); 

		/**
		 * Set the correct content header
		 */
		header ('Content-Type: ' . $_IMG['iinfo'][0]);

		/**
		 * Create the image
		 */
		$_IMG['iinfo'][2] ($_IMG['image_image'], '', 100);

		/**
		 * Destroy the image
		 */
		imagedestroy ($_IMG['image_image']);

		/**
		 * Return true
		 */
		return true;
	}
}
?>
