PHP image resize and upload

Okay, I’m making an image gallery and I need this to happen:

  • User specifies image using one of those ‘Browse’ form elements
  • Local filename gets passed to PHP for uploading
  • PHP makes two separate images from the uploaded file (discarding the original):
    1. A thumbnail - 150px wide or 100px high
    2. A large image - 640px wide or 480px high
  • Saves them to something_thumb.jpg and something.jpg
    (We’ll just use “something” for now. I’ll have PHP name the files using a variable later)

The thing is, the images being uploaded all have different sizes and aspect-ratios, but they need to be resized to proportionally.

THUMBNAILS:
Resized proportionally to either 150px wide or 100px high, depending on whether the image is in landscape or portrait orientation.
Uploaded as "something_thumb.jpg"

LARGE IMAGES:
Resized proportionally to either 640px wide or 480px high, depending on whether the image is in landscape or portrait orientation.
Uploaded as "something.jpg"

I’d really really really appreciate any help with this, since it’s doing my head in.

Thanks in advance!

[COLOR=“Red”]EDIT: Made it a little less complicated…[/COLOR]

well, i did something similar awhile back. this code isn’t exactly what you want, but it should definitely get you started


$uploaddir = "images/";
	$uploadfile = $uploaddir . basename($_FILES["photo_upload"]["name"]);
	move_uploaded_file($_FILES["photo_upload"]["tmp_name"], $uploadfile);

	$image_type = strstr($uploadfile, '.');
		switch($image_type) {
			case '.JPG':
				$source = imagecreatefromjpeg($uploadfile);
				break;
			case '.jpg':
				$source = imagecreatefromjpeg($uploadfile);
				break;
			case '.png':
				$source = imagecreatefrompng($uploadfile);
				break;
			case '.gif':
				$source = imagecreatefromgif($uploadfile);
				break;
			default:
				unlink( $uploadfile );
				echo("Invalid file type.  Please use the back button in your browser and select an acceptable file type.");
				exit;
			}
	$max_width = 225;
	$photo_code = randomCode(12);
	$file = $photo_code.".jpg";
	$fullpath = $uploaddir . $file;
	$size = getimagesize($uploadfile);
	if($size[0]>=$max_width)
		{
		$new_width = $max_width;
		$scale = $new_width/$size[0];
		$new_height = $scale*$size[1];
		}
	else
		{
		$new_width = $size[0];
		$new_height = $size[1];
		}
	$thumb = imagecreatetruecolor($new_width, $new_height);
	imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
	imagejpeg($thumb, $fullpath, 90);
	$filepath = $fullpath;
	unlink($uploadfile);

Your script looks like it only outputs one resized image, and it only resizes if the width exceeds a ceratin value (ie. doesn’t take into consideration height)

Thanks anyway! But I’m not sure if I can dissect and alter that code myself… I’m no PHP guru :stuck_out_tongue:

Anyone else?

oh, i know it’s not exactly what you were looking for, i just posted that so you could start looking at the different functions you would need.

what usually helps me figure out how to approach problems like this is to draw out a decision tree. basically map out the logic, then take it one step at a time.

Yeah, I know exactly what you mean. I’m right into that ‘thought organization’ stuff :stuck_out_tongue: I’ve actually given this a lot of thought, but I really have no idea as to what PHP functions to use, how to use them, etc, etc… I thought it’d just be easier to ask someone who has experience. I’ve also got a lot of work to do for the front-end of the site…

=)

BWH2, Do you have the FLA files to go along with the php you posted?
If so, can you please post them?

i did not make any flash files for this. i simply used an html form.