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

	/* constructor */
	function imageHandling_m()
	{

	}

	function crop($source,$destination,$x,$y,$w,$h){
		$resource = NewMagickWand();

		MagickReadImage($resource,$source);
		//MagickSetImageCompressionQuality($resource,100);
		MagickCropImage($resource,$w,$h,$x,$y);

		MagickWriteImage($resource, $destination );
	}

	function resize($source,$destination,$size,$type)
	{
		$resource = NewMagickWand();

		MagickReadImage($resource,$source);

		$original_x = MagickGetImageWidth($resource);
		$original_y = MagickGetImageHeight($resource);

		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;
			case "none":
					$resize_x = $original_x;
					$resize_y = $original_y;
			break;
		}
		$thumb_w = round($resize_x);
		$thumb_h = round($resize_y);

		//MagickSetImageCompressionQuality( $resource, 100);
		$resource = MagickTransformImage($resource,'0x0',$thumb_w . 'x' . $thumb_h);
		MagickUnsharpMaskImage($resource, 0.5,1.5,0.9,0);

		MagickWriteImage($resource,$destination);
	}
	function copy($source,$destination){
		copy($source,$destination);
	}
	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);
	}
}
?>