<?php
/**
 * This is a class file used to resize image at time of uplaod.
 *
 * PHP version 5
 *
 * @category Admin_Tool
 * @package  Admin_Tool
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  ACE
 * @version  CVS:1.0.1
 * @link     http://auminfosys.com/
 * @filesource
 */

/**
 * This is a class named CmsMgt.
 *
 * @category Admin_Tool
 * @package  Admin_Tool
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  ACE
 * @version  Release: 1.0.1
 * @link     http://auminfosys.com/
 *
 */
class ResizeImageMgmt
{
	/**
	 *	Orignal Image used to resize
	 *	@var string	  
	 */
	var $image = "";

	/**
	 *	New resizeed image 
	 *	@var string	  
	 */
	var $newimage = "";

	/**
	 *	New height to resize
	 *	@var int	  
	 */
	var $max_height = "";

	/**
	 *	New width to resize
	 *	@var int	  
	 */
	var $max_width = "";


	/**
	 * Resize image to new height/width
	 * 
	 * @param sting 	$image
	 * @param int 		$maxwidth
	 * @param int 		$maxheight
	 * @param boolean 	$blnfixheight
	 * @param boolean 	$blnfixwidth										
	 */

	function resize($image, $maxwidth, $maxheight, $blnfixheight, $blnfixwidth)
	{

		$this->image = $image;

		$ext = explode(".", $this->image);
		$extention = strtolower($ext[count($ext) - 1]);

		$this->newimage = $image;
		$this->max_height = $maxheight;
		$this->max_width = $maxwidth;


		if ($extention == "gif") {
			$src_img = imagecreatefromgif($this->image);
		} else if ($extention == "png") {
			$src_img = imagecreatefrompng($this->image);
		} else {
			$src_img = imagecreatefromjpeg($this->image);
		}

		$orig_x = imagesx($src_img);
		$orig_y = imagesy($src_img);

		$image_quality = 100;
		$addborder = 0;

		$new_y = $this->max_height;
		$new_x = $orig_x / ($orig_y / $this->max_height);

		if ($new_x > $this->max_width) {
			$new_x = $this->max_width;
			$new_y = $orig_y / ($orig_x / $this->max_width);
		}

		if ($blnfixheight) {
			if ($new_y != $maxheight) {
				$new_y = $maxheight;
			}
		}

		if ($blnfixwidth) {
			if ($new_x != $maxwidth) {
				$new_x = $maxwidth;
			}
		}

		$dst_img = imagecreatetruecolor($new_x, $new_y);
		imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x, $orig_y);

		// Add border
		if ($addborder == 1) {
			$black = imagecolorallocate($dst_img, 0, 0, 0);
			imagesetthickness($dst_img, 1);
			imageline($dst_img, 0, 0, $new_x, 0, $black);
			imageline($dst_img, 0, 0, 0, $new_y, $black);
			imageline($dst_img, $new_x - 1, 0, $new_x - 1, $new_y, $black);
			imageline($dst_img, 0, $new_y - 1, $new_x, $new_y - 1, $black);
		}

		if ($extention == "gif") {
			imagegif($dst_img, $this->newimage);
		} else if ($extention == "png") {
			imagepng($dst_img, $this->newimage);
		} else {
			imagejpeg($dst_img, $this->newimage);
		}

		imagedestroy($src_img);
		imagedestroy($dst_img);
	}
	function imagebmp($im, $fn = false)
	{
		if (!$im)
			return false;

		if ($fn === false)
			$fn = 'php://output';
		$f = fopen($fn, "w");
		if (!$f)
			return false;

		//Image dimensions
		$biWidth = imagesx($im);
		$biHeight = imagesy($im);
		$biBPLine = $biWidth * 3;
		$biStride = ($biBPLine + 3) & ~3;
		$biSizeImage = $biStride * $biHeight;
		$bfOffBits = 54;
		$bfSize = $bfOffBits + $biSizeImage;

		//BITMAPFILEHEADER
		fwrite($f, 'BM', 2);
		fwrite($f, pack('VvvV', $bfSize, 0, 0, $bfOffBits));

		//BITMAPINFO (BITMAPINFOHEADER)
		fwrite($f, pack('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));

		$numpad = $biStride - $biBPLine;
		for ($y = $biHeight - 1; $y >= 0; --$y) {
			for ($x = 0; $x < $biWidth; ++$x) {
				$col = imagecolorat($im, $x, $y);
				fwrite($f, pack('V', $col), 3);
			}
			for ($i = 0; $i < $numpad; ++$i)
				fwrite($f, pack('C', 0));
		}
		fclose($f);
		return true;
	}

}
?>