Problem with imagecopyresampled

Hi, hoping someone can help me with an upload script I’m using with Flash. The script tests the width and height of the uploaded file and if either are over a max value the image gets resampled to the max width and height and saved.

It all works fine with one exception—if the width and height are both larger than the maximum variables AND the image is landscape.

Here’s the code:


<?php 
$matrixw=125;
$matrixcomp=70;
$imagecomp=90;
$maxwidth=1650;
$maxheight=1000;

//hi res directory
$hires="../images/";

//thumbnail directory
$lores="../matrix/";

//the uploaded file
$uploadedfile = $_FILES['Filedata']['tmp_name'];

//boolean
//create a source image to manipulate 
$src = imagecreatefromjpeg($uploadedfile);

//player path
$player = $hires . basename( $_FILES['Filedata']['name']) ; 

//matrix path
$matrix = $lores . basename( $_FILES['Filedata']['name']) ; 

//array image width and height
list($width,$height)=getimagesize($uploadedfile);


//write the thumbnail image (the image, directory, compression)
if(move_uploaded_file ($uploadedfile, $player)) { 

	//scale the thumb height to the thumb width;
	$matrixh=($height/$width)*$matrixw;
	
	//set a new width and height variables for oversize images
	if($width>$height){
		$imageh=($height/$width)*$maxwidth;
		$imagew=$maxwidth;
	}elseif($width<$height){
		$imagew=($width/$height)*$maxheight;
		$imageh=$maxheight;
	}else{
		$imagew=$maxwidth;
		$imageh=$maxheight;	
	}

	//create a blank image for the  thunmbnail
	$matrixtmp=imagecreatetruecolor($matrixw,$matrixh);

	//resample the thumbnail
	imagecopyresampled($matrixtmp,$src,0,0,0,0,$matrixw,$matrixh, $width,$height);

	//write the thumnail (the image, directory, compression)
	if(imagejpeg($matrixtmp, $matrix, $matrixcomp)){ 
		
		//destroy the matrix temp
		imagedestroy($matrixtmp);
	} 
	
	//test for an oversized image
	if (($width>$maxwidth) || ($height>$maxheight)){
			
		//create a canvas for the oversized image
		$imgtmp=imagecreatetruecolor($imagew,$imageh);
			
		//resample the image
		imagecopyresampled($imgtmp,$src,0,0,0,0,$imagew,$imageh, $width,$height);
			
		//write the new image
		if(imagejpeg($imgtmp, $player, $imagecomp)){ 
			imagedestroy($imgtmp);
			imagedestroy($src);
		}
	}else{
		imagedestroy($src);
	}
	echo "The file  has been uploaded"; 
} 

?>