Uploading Single Image & Resizing Multiple Times
Posted: Fri Aug 13, 2010 1:23 pm
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.
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>';
}