<?
function createthumb1($name,$filename,$new_w,$new_h){
	$system=explode('.',$name);
	if ($system[1]=='jpg' || $system[1]=='JPG' || $system[1]=='jpeg' || $system[1]=='JPEG')
	{
		$src_img=imagecreatefromjpeg($name);
	}
	if (preg_match('/png/',$system[1])){
		$src_img=imagecreatefrompng($name);
	}
	if (preg_match('/gif/',$system[1])){
		$src_img=imagecreatefromgif($name);
	}
	
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$thumb_w=$new_w;
$thumb_h=$new_h;

if ($old_x > $old_y) {
    if($old_x >$new_w)
	  $thumb_w=$new_w;
	 else
	    $thumb_w=$old_x;
		
	$ratio=$old_x/$old_y;
    if($old_y >$new_h)
	  $thumb_h=$new_w/$ratio;
	 else
	    $thumb_h=$old_y; 
}
if ($old_x < $old_y) {
    $ratio=$old_x/$old_y;
  if($old_x >$new_w)
	$thumb_w=$new_h*$ratio;
	else
	    $thumb_w=$old_x;

	 if($old_y >$new_h)
	   $thumb_h=$new_h;
	 else
	    $thumb_h=$old_y;
	
}
if ($old_x == $old_y) {
	$thumb_w=$new_w;
	$thumb_h=$new_h;
}

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
	if (preg_match("/png/",$system[1]))
     {
	   imagepng($dst_img,$filename); 
      }
	  elseif (preg_match("/gif/",$system[1]))
     {
	   imagegif($dst_img,$filename); 
      }
	 else 
	 {
	  imagejpeg($dst_img,$filename); 
     }
imagedestroy($dst_img); 
imagedestroy($src_img); 
}




function createthumb11($name,$filename,$new_w,$new_h){
	$system=explode('.',$name);
	if ($system[1]=='jpg' || $system[1]=='JPG' || $system[1]=='jpeg' || $system[1]=='JPEG')
	{
		$src_img=imagecreatefromjpeg($name);
	}
	if (preg_match('/png/',$system[1])){
		$src_img=imagecreatefrompng($name);
	}
	if (preg_match('/gif/',$system[1])){
		$src_img=imagecreatefromgif($name);
	}
	
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$thumb_w=$new_w;
$thumb_h=$new_h;

if ($old_x > $old_y) {
    if($old_x >$new_w)
	  $thumb_w=$new_w;
	 else
	    $thumb_w=$old_x;
		
	$ratio=$old_x/$old_y;
    if($old_y >$new_h)
	  $thumb_h=$new_w/$ratio;
	 else
	    $thumb_h=$old_y; 
}
if ($old_x < $old_y) {
    $ratio=$old_x/$old_y;
  if($old_x >$new_w)
	$thumb_w=$new_h*$ratio;
	else
	    $thumb_w=$old_x;

	 if($old_y >$new_h)
	   $thumb_h=$new_h;
	 else
	    $thumb_h=$old_y;
	
}
if ($old_x == $old_y) {
	$thumb_w=$new_w;
	$thumb_h=$new_h;
}

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
	if (preg_match("/png/",$system[1]))
     {
	   imagepng($dst_img,$filename); 
      }
	  elseif (preg_match("/gif/",$system[1]))
     {
	   imagegif($dst_img,$filename); 
      }
	 else 
	 {
	  imagejpeg($dst_img,$filename); 
     }
imagedestroy($dst_img); 
imagedestroy($src_img); 
}

function createthumb111($name,$filename,$new_w,$new_h, $file_type)
{
	if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
	{
		$src_img = imagecreatefromjpeg($name);
	}
	elseif($file_type == "image/x-png" || $file_type == "image/png")
	{
		$src_img = imagecreatefrompng($name);
	}
	elseif($file_type == "image/gif")
	{
		$src_img = imagecreatefromgif($name);
	}	
	$old_x=imagesx($src_img);
	$old_y=imagesy($src_img);
	$thumb_w=$new_w;
	$thumb_h=$new_h;
	if ($old_x > $old_y) 
	{
		if($old_x >$new_w)
			  $thumb_w=$new_w;
	 	else
			$thumb_w=$old_x;		
		$ratio=$old_x/$old_y;
		if($old_y >$new_h)
			  $thumb_h=$new_w/$ratio;
	 	else
			$thumb_h=$old_y; 
	}
	if ($old_x < $old_y) 
	{
		$ratio=$old_x/$old_y;
		  if($old_x >$new_w)
			$thumb_w=$new_h*$ratio;
		else
			$thumb_w=$old_x;
	 	if($old_y >$new_h)
	   		$thumb_h=$new_h;
	 	else
			$thumb_h=$old_y;	
	}
	if ($old_x == $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
	if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
	{
		imagejpeg($dst_img,$filename);
	}
	elseif($file_type == "image/x-png" || $file_type == "image/png")
	{
		imagepng($dst_img,$filename);
	}
	elseif($file_type == "image/gif")
	{
		imagegif($dst_img,$filename);
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img); 
}

function createThumbnailFromFile($filename, $dest, $width = 125, $height = -1, $pictype = "thumb")
      {
	  
	        $format = strtolower(substr(strrchr($filename,"."),1));
	        switch($format)
	        {
	            case 'gif' :
	                $type = "gif";
	                $img  = imagecreatefromgif($filename);
	            break;
	            
	            case 'png' :
	                $type = "png";
	                $img  = imagecreatefrompng($filename);
	            break;
	            
	            case 'bmp' :
	                $type = "bmp";
	                $img  = imagecreatefromwbmp($filename);
	            break;
	            
	            case 'jpg' :
	                $type = "jpg";
	                $img  = imagecreatefromjpeg($filename);
	            break;
	            
	            case 'jpeg' :
	                $type = "jpg";
	                $img  = imagecreatefromjpeg($filename);
	            break;
	            
	            default : 
	                die ("ERROR; UNSUPPORTED IMAGE TYPE");
	            break;
	        }
	        
	        list($org_width, $org_height) = getimagesize($filename);
	        
	        # if height is not given, we should calculate it
	        if(-1 == $height) 
	        {
	            $height = round($org_height/$org_width*$width);
	        }
	        if(-1 == $width) 
	        {
	            $width = round($org_width/$org_height*$height);
	        }   
	        
	        $xoffset = 0;
	        $yoffset = 0;
	        
	        if ($pictype == "thumb") // To minimize destortion
	        {
	            if ($org_width / $width > $org_height/ $height)
	            {
	                $xtmp = $org_width;
	                $xratio = 1-((($org_width/$org_height)-($width/$height))/2);
	                $org_width = $org_width * $xratio;
	                $xoffset = ($xtmp - $org_width)/2;
	            }
	            elseif ($org_height/ $height > $org_width / $width)
	            {
	                $ytmp = $org_height;
	                $yratio = 1-((($width/$height)-($org_width/$org_height))/2);
	                $org_height = $org_height * $yratio;
	                $yoffset = ($ytmp - $org_height)/2;
	            }
	        }
	        
	        $img_n =imagecreatetruecolor($width, $height);    
	        imagecopyresampled($img_n, $img, 0, 0, $xoffset, $yoffset, $width, $height, $org_width, $org_height);
	        
	        if($type=="gif")
	        {
	            imagegif($img_n, $dest);
	        }
	        elseif($type=="jpg")
	        {
	            imagejpeg($img_n, $dest);
	        }
	        elseif($type=="png")
	        {
	            imagepng($img_n, $dest);
	        }
	        elseif($type=="bmp")
	        {
	            imagewbmp($img_n, $dest);
	        }
	        
	        return true;
      }


?>