Here is a function written by cgraz, who had a similar problem:
<?
function jpgresize ( $srcfile, $dsrd_width, $dsrd_height, $upload_dir, $dsrd_img_name ) {
//function written for using a form to upload an image
list($imagew, $imageh, $type, $attrib) = getimagesize($srcfile); // get variables for image width and height
// check image size to make sure it's under desired width x desired height
if((($imagew > $dsrd_width) || ($imageh > $dsrd_height)) && ($type == "2")) { // image width or height is greater than what you want
if($imageh > $imagew) {
$ratio = ($imageh/$imagew);
$new_h = $dsrd_height;
$new_w = ($new_h/$ratio);
}
if($imagew > $imageh) {
$ratio = ($imagew / $imageh);
$new_w = $dsrd_width;
$new_h = ($new_w/$ratio);
}
if($imagew == $imageh) {
$ratio = "1";
$new_w = $dsrd_height;
$new_h = $dsrd_height;
}
$dst_img=ImageCreate($new_w,$new_h);
$src_img=ImageCreateFromJpeg($srcfile);
ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,
$new_h,ImageSX($src_img),ImageSY($src_img));
$newfilename1 = "$upload_dir/$dsrd_img_name";
ImageJpeg($dst_img, $newfilename1);
echo "Image was resized and uploaded";
} else { //image wasn't too big (or was format other than jpeg)... upload as normal
copy($srcfile, "$upload_dir/$dsrd_img_name");
echo "Image didn't need to be resized and has been uploaded";
}
}
?>
You should be able to modify it to suit your needs if not just use it, assuming it's ok with him.
You can find the original thread here (The function is in the last post). Hope this helps 😉