If() to big resize image function

i need a resize function so i started with this… but somehow it does’nt work… whats wrong with this…


$imgsize = getimagesize($pieces[3]); 
	if($imgsize[0] < 300 || $imgsize[1] < 250) { 
     echo "<img src=\" ". $pieces[3] ." \" width=\85%\" height=\"85%\" hspace=\"2\" vspace=\"2\"></td>";
		}else{
 		 echo "</td>"; }
	elseif($pieces[3] != '') {
    	echo "<img src=\" ". $pieces[3] ." \"  hspace=\"2\" vspace=\"2\"></td>";
    		}else{
 		 		echo "</td>";
	}

it must resize the $pieces[3] if its to big, if possible resize to within 300x300px… can someone help???