Problem With Image Resize Function

Discussion in 'PHP' started by adamjblakey, Jan 29, 2009.

  1. #1
    Hi,

    I have the following function which is working fine apart from with the second image resize it is not keeping the height aspect and is squashing the image down. I cannot see why this would be happening though.

    function uploadimage($value){
    	$file_type = $value['type'];
    	$file_name = $value['name'];
    	$file_size = $value['size'];
    	$file_tmp = $value['tmp_name'];
    
    //check file extension
           $ext = strrchr($file_name,'.');
           $ext = strtolower($ext);
           if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
              echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
              exit();
           }
    
    	//get the file extension.
    	$getExt = explode ('.', $file_name);
    	$file_ext = $getExt[count($getExt)-1];
    
    	//create a random file name
    	$rand_name = md5(time());
    	$rand_name= rand(0,999999999);
    	//get the new width variable.
    	$ThumbWidth = 200;
    	$ImgWidth = 600;
    
    	//keep image type
    	if($file_size){
    		if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
    			$new_img = imagecreatefromjpeg($file_tmp);
    		}elseif($file_type == "image/x-png" || $file_type == "image/png"){
    			$new_img = imagecreatefrompng($file_tmp);
    		}elseif($file_type == "image/gif"){
    			$new_img = imagecreatefromgif($file_tmp);
    		}
    		//list width and height and keep height ratio.
    		list($width, $height) = getimagesize($file_tmp);
    		$imgratio=$width/$height;
    		if ($imgratio>1){
    			$newwidth = $ThumbWidth;
    			$newheight = $ThumbWidth/$imgratio;
    			$newwidth2 = $ImgWidth;
    			$newheight2 = $ImgWidth/$imgratio;
    			
    		}else{
    			$newheight = $ThumbWidth;
    			$newwidth = $ThumbWidth*$imgratio;
    			
    			$newheight2 = $ImgWidth;
    			$newwidth2 = $ImgWidth*$imgratio;
    		}
    		
    		
    		$resized_img = imagecreatetruecolor($newwidth, $newheight);
    		$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
    		imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    		imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
    		
    		//save image
    		move_uploaded_file($resized_img, "uploads/images/thumbs/$rand_name.$file_ext");
    		move_uploaded_file($resized_img2, "uploads/images/big/$rand_name.$file_ext");
    		imagejpeg ($resized_img,"uploads/images/thumbs/$rand_name.$file_ext");
    		imagejpeg ($resized_img2,"uploads/images/big/$rand_name.$file_ext");
    		imagedestroy($resized_img);
    		imagedestroy($resized_img2);
    		imagedestroy($new_img);
    
    
    	}	
    	$value = "$rand_name.$file_ext";
    	return $value;
    } 
    PHP:
    Cheers,
    Adam
     
    adamjblakey, Jan 29, 2009 IP
    hassanahmad1 likes this.
  2. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try this for determining the ratios:

                   
    $bigger = ($width >= $height) ? $width : $height; 
        
    $imgratio = $bigger / $ThumbWidth;
    $newwidth = intval($ThumbWidth / $imgratio);
    $newheight = intval($ThumbHeight / $imgratio);
    
    $imgratio = $bigger / $ImgWidth;
    $newwidth2 = intval($ImgWidth / $imgratio);
    $newheight2 = intval($ImgHeight / $imgratio);
    
    Code (markup):
    I haven't tried it but i am sure it will work! :)
     
    hassanahmad1, Jan 29, 2009 IP
  3. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Sorry there was a small mistake in the above code regarding variables..
    do this instead:

    
    $bigger = ($width >= $height) ? $width : $height; 
        
    $imgratio = $bigger / $ThumbWidth;
    $newwidth = intval($width / $imgratio);
    $newheight = intval($height / $imgratio);
    
    $imgratio = $bigger / $ImgWidth;
    $newwidth2 = intval($width / $imgratio);
    $newheight2 = intval($height / $imgratio);
    
    Code (markup):
     
    hassanahmad1, Jan 30, 2009 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Works a treat, thanks a lot.
     
    adamjblakey, Jan 30, 2009 IP