Uploading Single Image & Resizing Multiple Times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
codeline
Forum Newbie
Posts: 12
Joined: Tue Aug 10, 2010 5:53 am

Uploading Single Image & Resizing Multiple Times

Post by codeline »

I've been building a submission form for a user to upload an image and I want the image to resize twice: resize to a thumbnail size and another resize to a full size.

So basically I duplicated each line of code that was used for resizing an image once. When I submit and upload, I do get my full size image resized at 600 pixels, and the thumbnail image at 250px, but the thumbnail is in the black placeholder image.

First of all, I wanted to see if there was a better direction to take when resizing multiple times, and what code I am looking over that is not saving this thumbnail properly.

Code: Select all

// IMAGE EXTENSION VERIFICATION

$fname = strtolower($_FILES['subimg']['name']); // grab uploaded image's filename and lowercase the extension (ex: .JPG)

// verify that image uploaded is proper extension
if(preg_match('/[.](jpg)|(gif)|(png)$/', $fname))
{
	// set image variables
	$path_image = '../submissions/';
	$path_image_thumb = '../submissions/thumbs/';
	$final_width_of_image = 600;
	$final_width_of_thumb = 250;
	$final_height_of_thumb = 180;

	$randomappend=rand(0000,9999); // generate random number to append to filename
	$src = $_FILES['subimg']['tmp_name']; // grab the src for where the image is temporarily held
	
	$filesub = $randomappend . $fname; // initiate new file name for submission's full image
	$filethumb = $randomappend . $fname; // initiate new file name for submission's thumbnail
	
	$target = $path_image . $filethumb; // set variable for submission image's new location with appended name
	$target_thumb = $path_image_thumb . $filethumb; // set variable for thumbnail's new location with appended name
	
	move_uploaded_file($src, $target);
	move_uploaded_file($src, $target_thumb);
	
	//
	
	// RESIZE TO FIT NEWS COLUMN WIDTH
	// CHECK FILE EXTENSION
	if(preg_match('/[.](jpg)$/', $filesub)){
		$img = imagecreatefromjpeg($path_image . $filesub);
	} else if (preg_match('/[.](gif)$/', $filesub)){
		$img = imagecreatefromgif($path_image . $filesub);
	} else if (preg_match('/[.](png)$/', $filesub)){
		$img = imagecreatefrompng($path_image . $filesub);
	}
	
	if(preg_match('/[.](jpg)$/', $filethumb)){
		$img2 = imagecreatefromjpeg($path_image_thumb . $filethumb);
	} else if (preg_match('/[.](gif)$/', $filethumb)){
		$img2 = imagecreatefromgif($path_image_thumb . $filethumb);
	} else if (preg_match('/[.](png)$/', $filethumb)){
		$img2 = imagecreatefrompng($path_image_thumb . $filethumb);
	}
	
	
	// FIND UPLOADED FILE'S ORIGINAL DIMENSIONS
	$ox = imagesx($img);
	$oy = imagesy($img);
	
	// SET NEW DIMENSIONS FOR SUBMISSION IMAGE
	$sx = $final_width_of_image;
	$sy = floor($oy * ($final_width_of_image / $ox)); 
	
	// SET NEW DIMENSIONS FOR THUMBNAIL
	$nx = $final_width_of_thumb;
	$ny = $final_height_of_thumb; 
	
	
	$sm = imagecreatetruecolor($sx, $sy); 
	$nm = imagecreatetruecolor($nx, $ny);  
	
	imagecopyresampled($sm, $img, 0,0,0,0,$sx,$sy,$ox,$oy);
	imagecopyresampled($nm, $img2, 0,0,0,0,$nx,$ny,$ox,$oy); 
	
	if(!file_exists($path_image)){
		if(!mkdir($path_image)){
			die("There was a problem.");
		} 
	}
	
	if(!file_exists($path_image_thumb)){
		if(!mkdir($path_image_thumb)){
			die("There was a problem.");
		} 
	}
	
	imagejpeg($sm, $path_image . $filesub, 80);	
	imagejpeg($nm, $path_image_thumb . $filethumb, 80);	
	
	$sub .= '<br /><img src="' . $path_image . $filesub . '" alt="submission image">';
	$sub .= '<br />Submission was successfuly created!<br />';
	echo $sub;
	
	$tn .= '<br /><img src="' . $path_image_thumb . $filethumb . '" alt="thumbnail image">';
	$tn .= '<br />Thumbnail was successfuly created!<br />';
	echo $tn;

	//
	
	// query the actual post forms
	$q = "INSERT INTO submissions (id, name, email, city, state, country, subname, subdesc, subthumb, subphoto, postdate, approved) VALUES ('', '$name', '$email', '$city', '$state', '', '$title', '$desc', '$target', '$target_thumb', NOW(), 'NO')";
	$r = mysql_query($q);
	
	if($r)
	{
		echo 'Congrats, successfully addded a feature submission';
	} else {
		echo 'Something went wrong';
	}
	
} else {
	echo '<script language="JavaScript">';
	echo 'alert("Unacceptable image extension.")';
	echo '</script>';
}
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Uploading Single Image & Resizing Multiple Times

Post by Jade »

Try this:

Code: Select all

move_uploaded_file($src, $target); //once you've done this move $src is no longer at the same location
move_uploaded_file($target, $target_thumb); //so when you call $src here it's not going to work
codeline
Forum Newbie
Posts: 12
Joined: Tue Aug 10, 2010 5:53 am

Re: Uploading Single Image & Resizing Multiple Times

Post by codeline »

Tried and it killed the PHP, not loading the page. I'm assuming it's because once I move $src to $target, then $target to $target_thumb.. any calls for $target after the move_uploaded_file statement are not pulling the location because it was given to target_thumb?
codeline
Forum Newbie
Posts: 12
Joined: Tue Aug 10, 2010 5:53 am

Re: Uploading Single Image & Resizing Multiple Times

Post by codeline »

Is the order of functions incorrect? Having all the move_uploaded_file, imagecreatetruecolor, imagecopyresampled functions right after each other.. should I be resizing the image to one size first, then when completed, resize the other one?
codeline
Forum Newbie
Posts: 12
Joined: Tue Aug 10, 2010 5:53 am

Re: Uploading Single Image & Resizing Multiple Times

Post by codeline »

Alright so it definitely has something to do with the move_uploaded_file area..

I changed my code so that the image resizes to the full size first, then I resize the image to the thumbnail. I removed the code where it resizes full, and the thumbnail outputs perfectly.

I did try changing move_uploaded_file($src, $target) to move_uploaded_file($target, $target_thumb) but no luck, it still returns a black image.

So the move_uploaded_file is where I'm finding my mixup.
codeline
Forum Newbie
Posts: 12
Joined: Tue Aug 10, 2010 5:53 am

Re: Uploading Single Image & Resizing Multiple Times

Post by codeline »

Here's the updated code.. still outputs both images, but the thumbnail size is only giving me the black placeholder.

Code: Select all

$fname = strtolower($_FILES['subimg']['name']); // grab uploaded image's filename and lowercase the extension (ex: .JPG)
						
						// verify that image uploaded is proper extension
						if(preg_match('/[.](jpg)|(gif)|(png)$/', $fname))
						{
							// set image variables
							$path_image = '../submissions/';
							$final_width_of_image = 600;
							$randomappend=rand(0000,9999); // generate random number to append to filename
							$src = $_FILES['subimg']['tmp_name']; // grab the src for where the image is temporarily held
							$filesub = $randomappend . $fname; // initiate new file name for submission's full image
							$target = $path_image . $filesub; // set variable for submission image's new location with appended name
							
							$path_image_thumb = '../submissions/thumbs/';
							$final_width_of_thumb = 250;
							$final_height_of_thumb = 180;
							$filethumb = $randomappend . $fname; // initiate new file name for submission's thumbnail
							$target_thumb = $path_image_thumb . $filethumb; // set variable for thumbnail's new location with appended name
							
							
							
							
							
							move_uploaded_file($src, $target);

							// RESIZE TO FIT NEWS COLUMN WIDTH
							// CHECK FILE EXTENSION
							if(preg_match('/[.](jpg)$/', $filesub)){
								$img = imagecreatefromjpeg($path_image . $filesub);
							} else if (preg_match('/[.](gif)$/', $filesub)){
								$img = imagecreatefromgif($path_image . $filesub);
							} else if (preg_match('/[.](png)$/', $filesub)){
								$img = imagecreatefrompng($path_image . $filesub);
							}
	
							// FIND UPLOADED FILE'S ORIGINAL DIMENSIONS
							$ox = imagesx($img);
							$oy = imagesy($img);
							
							// SET NEW DIMENSIONS FOR SUBMISSION IMAGE
							$sx = $final_width_of_image;
							$sy = floor($oy * ($final_width_of_image / $ox)); 
							
							$sm = imagecreatetruecolor($sx, $sy); 
							 
							imagecopyresampled($sm, $img, 0,0,0,0,$sx,$sy,$ox,$oy);

							if(!file_exists($path_image)){
								if(!mkdir($path_image)){
									die("There was a problem.");
								} 
							}
							
							imagejpeg($sm, $path_image . $filesub, 80);	
							
							$sub .= '<br /><img src="' . $path_image . $filesub . '" alt="submission image">';
							$sub .= '<br />Submission was successfuly created!<br />';
							echo $sub;
							
							
							
						
						
							
							move_uploaded_file($src, $target_thumb);

							if(preg_match('/[.](jpg)$/', $filethumb)){
								$img2 = imagecreatefromjpeg($path_image_thumb . $filethumb);
							} else if (preg_match('/[.](gif)$/', $filethumb)){
								$img2 = imagecreatefromgif($path_image_thumb . $filethumb);
							} else if (preg_match('/[.](png)$/', $filethumb)){
								$img2 = imagecreatefrompng($path_image_thumb . $filethumb);
							}
							
							$tox = imagesx($img2);
							$toy = imagesy($img2);
							
							// SET NEW DIMENSIONS FOR THUMBNAIL
							$tx = $final_width_of_thumb;
							$ty = $final_height_of_thumb; 
							
							$tm = imagecreatetruecolor($tx, $ty); 
							
							imagecopyresampled($tm, $img2, 0,0,0,0,$tx,$ty,$tox,$toy); 
							
							if(!file_exists($path_image_thumb)){
								if(!mkdir($path_image_thumb)){
									die("There was a problem.");
								} 
							}
							
							imagejpeg($tm, $path_image_thumb . $filethumb, 80);
							
							$tn .= '<br /><img src="' . $path_image_thumb . $filethumb . '" alt="thumbnail image">';
							$tn .= '<br />Thumbnail was successfuly created!<br />';
							echo $tn;
Post Reply