PHP GD resize to thumbnails and retain original image aspect ratio. Thanks.

Thank you for any advice, code, or hints I am just stuck on this and can't generate thumbnails while retaining original image aspect.
// Set local PHP vars from the POST vars sent from flash
$todayDate = $_POST['todayDate'];
$id = $_POST['id'];
$name = $_POST['Name'];
$email = $_POST['Email'];
$filename = $_FILES['Filedata']['name'];
$filename = strtolower($filename);// Must be Lower Case or else Black image
$filename = "uploaded.jpg"; // rename all files to 	
$filetmpname = $_FILES['Filedata']['tmp_name'];	
$fileType = $_FILES["Filedata"]["type"];
$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);
// Place file on server, into the images folder
move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$name."_".$todayDate."_original_".$filename); //Original
createthumb("images/".$name."_".$todayDate."_original_".$filename,"images/".$name."_".$todayDate."_print_".$filename,398,568); // Printable <---- NEED HELP TO MAKE DYNAMIC AND RESIZE with out distorting original image aspect ratio code works just need to improve here it's hard coded to 398x568 need it to except any image size 
createthumb("images/".$name."_".$todayDate."_original_".$filename,"images/".$name."_".$todayDate."_thumb_".$filename,75,100); // Thumbnail <---- NEED HELP TO MAKE DYNAMIC AND RESIZE with out distorting original image aspect ratio code works just need to improve here it's hard coded to 75x100 need it to except any image size 
function createthumb($name,$filename,$new_w,$new_h)
	if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
	if (preg_match("/png/",$system[1]))
	} else {
// Insert for display in flash
$con = mysql_connect("*****","****","***");
if (!$con)
  die('Could not connect: ' . mysql_error());
mysql_select_db("ftr", $con);
// Data Insert Image File Names
$original = "".$name."_".$todayDate."_original_".$filename; //Original
$thumbnail = "".$name."_".$todayDate."_thumb_".$filename; // Thumbnail
$printable = "".$name."_".$todayDate."_print_".$filename; // Printable
// Data Insert Image File Names
$sql="INSERT INTO ftr_images_tbl (id, ftr_original_img, ftr_thumb_img, ftr_print_img) VALUES ('$id','$original','$thumbnail','$printable')";
if (!mysql_query($sql,$con))
  die('Error: ' . mysql_error());
  } else {
$message = "******" );
print "answer=ok";
// Insert for display in flash

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here's a routine I've used on a bunch of my projects.

set $file to the image name. and you might have to change directories.
      $new_x=(int) ($ratio*imagesx($original_image)+.5);
      $new_y=(int) ($ratio*imagesy($original_image)+.5);
      $new_im = @ImageCreateTrueColor($new_x, $new_y) or die ("Cannot Initialize new GD image stream");
      $ok=imagecopyresampled( $new_im, $original_image, 0, 0, 0, 0, $new_x, $new_y, imagesx($original_image), imagesy($original_image));
      $ok=imagejpeg( $new_im, '../item_thumbs/'.$file, 100);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MultiversedAuthor Commented:
Hi Michael! Looks like a solid routine. I will give this a run first thing tomorrow morning. I'll let you know how it goes. Then I will post back working code. Both the AS3 and PHP. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.