<?php


class SimpleThumbMaker{
 
    var $extension;
    var $sourceFile;
    var $destinationFile;
  	var $maxWidth;
  	var $maxHeight;
  	
    function SimpleThumbMaker(){
    }
	
	function setMaxSize($width, $height){
	 	$this->maxWidth = $width;
	 	$this->maxHeight = $height;
 	}
	function createThumb($sourceFile, $destinationFile){
	
  		 $size = GetImageSize($sourceFile);
  		 $this->extension = substr($sourceFile, strrpos($sourceFile, '.') + 1);

         $width = $size[0];
         $height = $size[1];
        
         $x_ratio = $this->maxWidth / $width;
         $y_ratio = $this->maxHeight / $height;

         if( ($width <= $this->maxWidth) && ($height <= $this->maxHeight) ){
               $tn_width = $width;
               $tn_height = $height;
         }
         elseif (($x_ratio * $height) < $this->maxHeight){
               $tn_height = ceil($x_ratio * $height);
               $tn_width = $this->maxWidth;
         }
         else{
               $tn_width = ceil($y_ratio * $width);
               $tn_height = $this->maxHeight;
         }
         
	     if ($this->extension == "jpg"){
	      	 if(!function_exists(ImageCreateFromJpeg)) return -1;
	         $src = ImageCreateFromJpeg($sourceFile);
	         $dst = ImageCreateTrueColor($tn_width, $tn_height);
	         ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	         if(!function_exists(ImageJpeg)) return -2;
	         ImageJpeg($dst, $destinationFile);
	     } elseif ($this->extension == "png"){
	      	 if(!function_exists(ImageCreateFromPng) ) return -1;
	         $src = ImageCreateFromPng($sourceFile);
	         $dst = ImageCreateTrueColor($tn_width, $tn_height);
	         ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	       	 if(!function_exists(ImagePng)) return -2;
	         ImagePng($dst, $destinationFile);
	     } elseif ($this->extension == "gif"){
	      	 if(!function_exists(ImageCreateFromGif) ) return -1;
	         $src = ImageCreateFromGif($sourceFile);
	         $dst = ImageCreateTrueColor($tn_width, $tn_height);
	         ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	         if(!function_exists(ImageGif)) return -2;
	         ImageGif($dst, $destinationFile);
	     }else{
	      return -3;
		}
     	
		 return 1;
    }
}
?>