<?php
/******************************************
** Knjižnica, ki skrbi za delo z slikami **
** Author: Dave @ zabec.net	 			  **
** Date: 2008 						 			  **
******************************************/
/* class imageHandling */
/**********************/
class imageHandling
{
	var $file;
	var $file_type;
	var $location;

	/* constructor */
	function imageHandling()
	{

	}

	function crop($source,$destination,$x,$y,$w,$h){
		$konec = strtolower(strrchr($source,"."));
		$img = imagecreatetruecolor($w,$h);
		if (($konec == ".jpg")OR($konec == ".jpeg"))
		{
			$org_img = imagecreatefromjpeg($source);
		}
		if ($konec == ".png")
		{
			$org_img = imagecreatefrompng($source);
		}
		if ($konec == ".gif")
		{
			$org_img = imagecreatefromgif($source);
		}
		imagecopy($img,$org_img, 0, 0, $x, $y, $w, $h);
		if (($konec == ".jpg")OR($konec == ".jpeg"))
		{
			imagejpeg($img,$destination);
		}
		if ($konec == ".png")
		{
			imagepng($img,$destination);
		}
		if ($konec == ".gif")
		{
			imagegif($img,$destination);
		}
		imagedestroy($img);
	}

	function resize($source,$destination,$size,$type)
	{
		$konec = strtolower(strrchr($source,"."));
		if ($konec == ".jpg" OR $konec == ".jpeg")
		{
			$src_img = imagecreatefromjpeg($source);
		}
		if ($konec == ".png")
		{
			$src_img = imagecreatefrompng($source);
		}
		if ($konec == ".gif")
		{
			$src_img = imagecreatefromgif($source);
		}

		$original_x = imageSX($src_img);
		$original_y = imageSY($src_img);

		switch($type)
		{
			case "polna":
				if($original_x < $original_y)
				{
					$resize_x = $size;
					$resize_y = $original_y/($original_x/$resize_x);
				}
				elseif($original_y < $original_x)
				{
					$resize_y = $size;
					$resize_x = $original_x/($original_y/$resize_y);
				}
				else
				{
					$resize_y = $size;
					$resize_x = $size;
				}
			break;
			case "omejena":
				if($original_x > $original_y)
				{
					$resize_x = $size;
					$resize_y = $original_y/($original_x/$resize_x);
				}
				elseif($original_y > $original_x)
				{
					$resize_y = $size;
					$resize_x = $original_x/($original_y/$resize_y);
				}
				else
				{
					$resize_y = $size;
					$resize_x = $size;
				}
			break;
			case "fixed_w":
					$resize_x = $size;
					$resize_y = $original_y/($original_x/$resize_x);
			break;
		}
		$thumb_w = round($resize_x);
		$thumb_h = round($resize_y);

		$dst_img = imagecreatetruecolor($thumb_w,$thumb_h);
		$bgColor = imagecolorallocate($dst_img, 255,255,255);

		imagefill($dst_img , 0,0 , $bgColor);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);

		if ($konec == ".jpg" OR $konec == ".jpeg")
		{
			imagejpeg($dst_img,$destination,100);
		}
		if ($konec == ".png")
		{
			imagepng($dst_img,$destination);
		}
		if ($konec == ".gif")
		{
			imagegif($dst_img,$destination);
		}

		imagedestroy($dst_img);
		imagedestroy($src_img);
	}

	function createThumb($source,$destination,$size)
	{
		$this->resize($source,$destination,$size,"polna");
		$this->crop($destination, $destination, 0, 0,$size,$size);
		//$this->crop($source,$destination,0,0,$size,$size);
	}
}
?>