hello,

i am facing problem with image cropping. can you provide any code for crop images. actually my problem is i want to display 400X500 images. actually the original image size is 1600x900. then i am cropping that to 400X500. but it is scretching out. please tell me is there any way to solve this issue.

here is sample my code:

if(is_uploaded_file($_FILES["subimage"]["tmp_name"])!='')
			{
			//echo"file in";exit;
			//echo"in ";exit;
			$rand_variable1=rand(10000,100000);
			if(is_uploaded_file($_FILES["subimage"]["tmp_name"]))
			{
			 @unlink("products/".$hidden_image);
	          
			  @unlink("products/400X500/".$hidden_image);
			 
			$file_image=$rand_variable1.$_FILES["subimage"]["name"];
			move_uploaded_file($_FILES["subimage"]["tmp_name"], "products/" .$file_image);
				
				$Attachments2=$file_image;
				
			}
			/************************************Resizing the image 120x150****************/
			$path3="products";
			$filetype3=$_FILES["subimage"]["type"];
			$bgim_file_name = $path3."/".$Attachments2; 
			$bgimage_attribs = getimagesize($bgim_file_name);
			 list($width, $height, $type, $attr) = 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);
				}
				if($width>400)
				{
				$new_width=400;
				}
				else
				{
				$new_width=$width;
				}
				if($height>500)
				{
				$new_height=500;
				}
				else
				{
				$new_height=$height;
				}
						
			$bgth_max_width = $new_width; //for Album image
			$bgth_max_height = $new_height;
			$bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];
		
			$bgth_width = $new_width;//$image_attribs[0] * $ratio; 
			$bgth_height =$new_height;//$image_attribs[1] * $ratio;
		
			$bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
			imageantialias($bgim_new,true); 
			$bgth_file_name = "products/400X500/$Attachments2";
			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,9);
				}				
			/************End Resize of 400*500*******************/

You need to change this:

imagecopyresampled($bgim_new,$bgim_old,0,0,600,200,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);

This should be the values:

imagecopyresampled('newfile.jpg','original.jpg',0,0,600,200,400,500,1600,900);

In order to crop, GD needs to know where to start cropping. On a 1600px width, if you want 400px width, you have to do 1200px/2, so you end with 600px, the same is done for height, and you get 200.

With imagemagick all this is easier:

convert -crop 400x500 original.jpg newfile.jpg
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.