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)
{
	$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);
	$thumb_w=$new_w;
	$thumb_h=$new_h;
	$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); 
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img); 
}
 
// 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";
  }
mysql_close($con);
// Insert for display in flash

Open in new window

MultiversedAsked:
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.

Michael701Commented:
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.
$max_height=120;
$max_width=90;
 
 
      $original_image=imagecreatefromjpeg('../item_images/'.$file);
      $ratio=min($max_width/imagesx($original_image),$max_height/imagesy($original_image));
      $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);
      imagedestroy($original_image);
      imagedestroy($new_im);

Open in new window

0

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.
0
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
PHP

From novice to tech pro — start learning today.