<?php

/**
*	class Image2Thumbnail
*   Thumbnail creation with PHP4 and GDLib 2.0.1 !
*
*
*   @author     Andreas Martens <heyn@plautdietsch.de>
*	@version	1.0
*/

class Img2Thumb	{

/**
*   Constructor - requires following vars:
*	
*	@param string $filename			image path
*
*	These are additional vars:
*
*	@param int $newxsize			new maximum image width
*	@param int $newysize			new maximum image height
*	@param string $fileout			output image path
*
*/
	function Img2Thumb($filename, $newxsize = 60, $newysize = 60, $fileout = '')	{
	
		if (isset($_POST)) $httpvars =  $_POST;
   	else if (isset($_POST)) $httpvars =  $_POST;
		
		$this -> NewImgCreate($filename,$newxsize,$newysize,$fileout);
//		$check = $this->resize_jpeg( $filename, $fileout, $newxsize, $nezwysize);

	}
	
/**
*  
*	private function - do not call
*
*/
	function NewImgCreate($filename,$newxsize,$newysize,$fileout)	{
		$type = $this->GetImgType($filename);

		if ($type=="png")	{
			 $orig_img = imagecreatefrompng($filename);
		}
		if ($type=="jpg")	{
			 $orig_img = imagecreatefromjpeg($filename);
		}
	
		$new_img =$this->NewImgResize($orig_img,$newxsize,$newysize,$filename);
		
		if (!empty($fileout))	{
			 $this-> NewImgSave($new_img,$fileout,$type);
		}
		else	{
			 $this->NewImgShow($new_img,$type);
		}

		ImageDestroy($new_img);
		ImageDestroy($orig_img);
	}
	
	/**
*  
*	private function - do not call
*	includes function ImageCreateTrueColor and ImageCopyResampled which are available only under GD 2.0.1 or higher !
*/
	function NewImgResize($orig_img, $newxsize, $newysize, $filename)	{

		$orig_size = getimagesize($filename);

//		br($newxsize, "x1");
//		br($newysize, "y1");
//		dumpa($orig_size, "orig_size");
		
		if ($orig_size[0] < $orig_size[1]){
			$newxsize = $newysize * ($orig_size[0]/$orig_size[1]);
//			br($newxsize, "x-int");
//			br($newysize, "y-int");
//			br("width < height");
		}	else {
			$newysize = $newxsize / ($orig_size[0]/$orig_size[1]);
		}
//		br($newxsize, "x2");
//		br($newysize, "y2");
		
		$im_out = ImageCreateTrueColor($newxsize,$newysize);
		ImageCopyResampled($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
		return $im_out;
	}
	
	/**
*  
*	private function - do not call
*
*/
	function NewImgSave($new_img,$fileout,$type)	{
		if ($type=="png")	{
			if (substr($fileout,strlen($fileout)-4,4)!=".png")
				$fileout .= ".png";
			 return imagepng($new_img,$fileout);
		}
		if ($type=="jpg")	{
			if (substr($fileout,strlen($fileout)-4,4)!=".jpg")
				$fileout .= ".jpg";
			 return imagejpeg($new_img,$fileout);
		}
	}
	
	/**
*  
*	private function - do not call
*
*/
	function NewImgShow($new_img,$type)	{
		if ($type=="png")	{
			header ("Content-type: image/png");
			 return imagepng($new_img);
		}
		if ($type=="jpg")	{
			header ("Content-type: image/jpeg");
			 return imagejpeg($new_img);
		}
	}
	
	/**
*  
*	private function - do not call
*
*/
	function GetImgType($filename)	{
		$size = getimagesize($filename);
		if($size[2]==2) return "jpg";
		elseif($size[2]==3) return "png";
		
	}
	
	//New function added by Aleksandar Ilic to make it possible to resize images with gd-1.6
	function resize_jpeg( $image_file_path, $new_image_file_path, $max_width, $max_height) 
	{ 
	     
	    $return_val = 1; 
	     
	    $return_val = ( ($img = ImageCreateFromJPEG ( $image_file_path )) && $return_val == 1 ) ? "1" : "0"; 
	     
	    $FullImage_width = imagesx ($img);    // Original image width 
	    $FullImage_height = imagesy ($img);    // Original image height 
	     
	            // now we check for over-sized images and pare them down 
	            // to the dimensions we need for display purposes 
	    $ratio =  ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ; 
	    $new_width = ((int)($FullImage_width * $ratio));    //full-size width 
	    $new_height = ((int)($FullImage_height * $ratio));    //full-size height 
	            //check for images that are still too high 
	    $ratio =  ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ; 
	    $new_width = ((int)($new_width * $ratio));    //mid-size width 
	    $new_height = ((int)($new_height * $ratio));    //mid-size height 
	     
	            // --Start Full Creation, Copying-- 
	    // now, before we get silly and 'resize' an image that doesn't need it... 
	    if ( $new_width == $FullImage_width && $new_height == $FullImage_height ) 
	        copy ( $image_file_path, $new_image_file_path ); 
	     
	    $full_id = ImageCreate( $new_width , $new_height );        //create an image 
	    ImageCopyResized( $full_id, $img, 
	                    0,0,  0,0, //starting points 
	                    $new_width, $new_height, 
	                    $FullImage_width, $FullImage_height ); 
	    $return_val = ( $full = ImageJPEG( $full_id, $new_image_file_path, 80 ) 
	                 && $return_val == 1 ) ? "1" : "0"; 
	    ImageDestroy( $full_id ); 
	            // --End Creation, Copying-- 
	     
	    return ($return_val) ? TRUE : FALSE ; 
	     
	} 	
}

?>