how to resize and make the thumbnail view of the photos like in facebook or other sites without distorting the photos

how to resize and make the thumbnail view of the photos like in facebook or other sites without distorting the photos

Here is a function that i use which works like a charm for me...

function img_resize($name,$filename,$new_w,$new_h)
{
	$system=explode(".",$name);
	if (preg_match("/jpg|jpeg/",$system[1])){$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);
 
		if ($old_x > $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y) 
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	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); 
}

Hope this helps..
cheers!!

how to resize and make the thumbnail view of the photos like in facebook or other sites without distorting the photos

use this code.

/************Start of main image upload*******************/
		if(is_uploaded_file($_FILES["productimage"]["tmp_name"])!='')
		{
		      $rand_variable=rand(10000,100000);
			  $image_name=$rand_variable.$_FILES["productimage"]["name"];

			if(is_uploaded_file($_FILES["productimage"]["tmp_name"]))
			{
			
				move_uploaded_file($_FILES["productimage"]["tmp_name"], "productimages/" . $image_name);
				
				$Attachments=$image_name;
				
			}
			/************************************Resizing the image ****************/
			$path3="productimages";
			$filetype3=$_FILES["productimage"]["type"];
			$bgim_file_name = $path3."/".$Attachments; 
			$bgimage_attribs = getimagesize($bgim_file_name);
		
			if($filetype3=='image/gif')	
				{
					$bgim_old = imagecreatefromgif($bgim_file_name); 
				}	
			else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))	
				{
					 $bgim_old = imagecreatefromjpeg($bgim_file_name);
				}
			  else	if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
				{
					 $bgim_old = imagecreatefrompng($bgim_file_name);
				}
						
			$bgth_max_width = 337; //for Album image
			$bgth_max_height = 337;
			$bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];
		
			$bgth_width = 337;//$image_attribs[0] * $ratio; 
			$bgth_height = 337;//$image_attribs[1] * $ratio;
		
			$bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
			imageantialias($bgim_new,true); 
			$bgth_file_name = "productimages/340X400/$Attachments";
			imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
			if($filetype3=='image/gif')	
				{
				    imagegif($bgim_new,$bgth_file_name,100);
					//$bgim_old = imagegif($bgim_file_name); 
				}	
			else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))	
				{
					 imagejpeg($bgim_new,$bgth_file_name,100);
				}
			  else	if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
				{
					 imagepng($bgim_new,$bgth_file_name,100);
				}				
			/************End Resize *******************/

productimage means input fielld name. productimages means you create a folder for store cropimages

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.