Jump to content

While loop error


rick.emmet

Recommended Posts

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:

 

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
}

 

Does anyone have a suggestion as to changing this and getting it running? Thanks much for your time!

Cheers,

Rick

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.