Try this one, it resizes an image to any size, (you can specify max height or width) and saves it at a perfect aspect ratio at 90 percent quality. This is an excellent script and has helped me many times.
Enjoy.
/*
Function checkgd()
checks the version of gd, and returns "yes" when it's higher than 2
*/
function checkgd(){
$gd2="";
ob_start();
phpinfo(8);
$phpinfo=ob_get_contents();
ob_end_clean();
$phpinfo=strip_tags($phpinfo);
$phpinfo=stristr($phpinfo,"gd version");
$phpinfo=stristr($phpinfo,"version");
preg_match('/\d/',$phpinfo,$gd);
if ($gd[0]=='2'){$gd2="yes";}
return $gd2;
}
/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h,$yallery){
global $gd2;
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($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;
}
// If smaller than max size, make a smaller image
if ($old_x < $new_w) {
$thumb_w=$old_x;
$thumb_h=$old_y;
}
if ($old_y < $new_h) {
$thumb_w=$old_x;
$thumb_h=$old_y;
}
if ($gd2==""){
$dst_img=ImageCreate($thumb_w,$thumb_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
} else {
$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);
} else {
imagejpeg($dst_img,$filename,90);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}