While loop error?
Posted: Sat Dec 17, 2011 3:36 pm
Hello Everyone,
I've been taking code snippets and functions from sites such as this and rewriting them for use on a project. I've been able to take these functions and iterate through a series of uploaded files to do various types of processing on them. I found a resizing function for uploaded images and have modified it only slightly – and it works great on a single file. I will allow users to upload multiple images, and I need to loop through the files and resize each one in turn.
I've used While Loops for other functions and they have worked fine, but on this particular function I get a parse error, unexpected “}” (see the note in the code). I suspect that the While Loop is throwing every thing off, but I don't know of any other approach (and what I've tested so far hasn't worked). Here's the code:
Does anyone have a suggestion as to changing this and getting it running? Thanks much for your time!
Cheers,
Rick
I've been taking code snippets and functions from sites such as this and rewriting them for use on a project. I've been able to take these functions and iterate through a series of uploaded files to do various types of processing on them. I found a resizing function for uploaded images and have modified it only slightly – and it works great on a single file. I will allow users to upload multiple images, and I need to loop through the files and resize each one in turn.
I've used While Loops for other functions and they have worked fine, but on this particular function I get a parse error, unexpected “}” (see the note in the code). I suspect that the While Loop is throwing every thing off, but I don't know of any other approach (and what I've tested so far hasn't worked). Here's the code:
Code: Select all
function resize_rename_img() {
//list the width and height and maintain the photo ratio.
$num = 0;
while ($num < 5) {
list($width, $height) = getimagesize('./uploads/'.$_FILES['userfile']['name'][$num]);
//calculate the image ratio
$imgratio=$width/$height;
// determine the orientation of the photo
if ($width > $height) { // LANDSCAPE ORIENTATION
if ($width > 600) {
$newWidth = 600;
$newHight = $newWidth/$imgratio;
//function for resize image.
$resized_img = imagecreatetruecolor($newWidth,$newHight);
// Identify the source photo
$source = imagecreatefromjpeg('./uploads/'.$_FILES['userfile']['name'][$num]);
//the resizing is going on here!
imagecopyresized($resized_img, $source, 0, 0, 0, 0, $newWidth, $newHight, $width, $height);
// NEED TO SAVE AND RENAME THE PHOTO
ImageJpeg ($resized_img,'./uploads/'.$_FILES['userfile']['name'][$num]);
// DISTROY THE TEMP IMG FILE
ImageDestroy ($resized_img);
} else {
// Change the name of each photo
rename ('./uploads/'.$_FILES['userfile']['name'][$num],
'./uploads/'.$_FILES['userfile']['name'][$num]);
}
} elseif ($height > $width) { // PORTRATE ORIENTATION
if ($height > 600) {
$newHight = 600;
$newWidth = $newHight*$imgratio;
//function for resize image.
$resized_img = imagecreatetruecolor($newWidth,$newHight);
// Identify the source photo
$source = imagecreatefromjpeg('./uploads/'.$_FILES['userfile']['name'][$num]);
//the resizing is going on here!
imagecopyresized($resized_img, $source, 0, 0, 0, 0, $newWidth, $newHight, $width, $height);
// NEED TO SAVE AND RENAME THE PHOTO
ImageJpeg ($resized_img,'./uploads/'.$_FILES['userfile']['name'][$num]);
// DISTROY THE TEMP IMG FILE
ImageDestroy ($resized_img);
} else {
// Change the name of each photo
rename ('./uploads/'.$_FILES['userfile']['name'][$num],
'./uploads/'.$_FILES['userfile']['name'][$num]);
}
}
$num++
} // HERE'S WHERE THE PHP ENGINE SAYS I HAVE AN ERROR
}Cheers,
Rick