Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with NESTED IF statements...i think

Status
Not open for further replies.

i3iz

Technical User
Jan 3, 2005
30
0
0
US
Why am i getting an error at the start of my nested if code?

<?php

function thumb($source, $quality = 80)
{
/* Check for the image's exisitance */
if (!file_exists($source)) {
echo 'File does not exist!';
}
else {
$size = getimagesize($source); // Get the image dimensions and mime type
$oldwidth = '$size[0]'; // Width divided
$oldheight = '$size[1]'; // Height divided
# taller
   if ('$oldheight' > '$oldwidth') {
       $w = ('$oldwidth' / '$oldheight') * '150';
       $h = '150';
   }

# wider
   else if ('$oldwidth' > '$oldheight') {
       $h = ('$oldheight' / '$oldwidth') * '150';
       $w = '150';
   }
# equal
   else ('$oldwidth' = '$oldheight') {
       $h = '150';
       $w = '150';
   }
$resize = imagecreatetruecolor($w, $h); // Create a blank image

/* Check quality option. If quality is greater than 100, return error */
if ($quality > 100) {
echo 'The maximum quality is 100. <br>Quality changes only affect JPEG images.';
}
else {
header('Content-Type: '.$size['mime']); // Set the mime type for the image

switch ($size['mime']) {
case 'image/jpeg':
$im = imagecreatefromjpeg($source);
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original JPEG
imagejpeg($resize, '', $quality); // Output the new JPEG
break;

case 'image/png':
$im = imagecreatefrompng($source);
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original PNG
imagepng($resize, '', $quality); // Output the new PNG
break;
}


imagedestroy($im);
}

}
}
thumb($_GET['source'], $_GET['quality']);
?>
 
i find it vital to maintain a strict use of tabs in procedural code in order to improve the chances of an easy debug. by doing so it becomes easy to see unclosed brackets or curly braces.

in your code this was not the issue.

your variables were not properly declared as or used. as sleipnir pointed out, you had quotes around the variable names.

you also had an else where there should have been an elseif or no conditional.

posted below is cleaned up code. i have not tested whether your code actually works.

Code:
<?php

function thumb($source, $quality = 80)
{
/* Check for the image's exisitance */
	if (!file_exists($source)) 
	{
		echo 'File does not exist!';
	}
	else 
	{
		$size = getimagesize($source); // Get the image dimensions and mime type
		$oldwidth = $size[0]; // Width divided
		$oldheight = $size[1]; // Height divided
		# taller
   		if ($oldheight > $oldwidth) 
		{
       		$w = ($oldwidth/$oldheight) * 150;
	       	$h = '150';
		
   		}
		# wider
   		elseif ($oldwidth > $oldheight) 
		{
       		$h = ($oldheight/$oldwidth) * 150;
       		$w = 150;
   		}
		# equal
	   elseif ($oldwidth == $oldheight) //this should be an elseif statement or you should remove the condition
	   {
	       $h = 150;
    	   $w = 150;
	   }

		$resize = imagecreatetruecolor($w, $h); // Create a blank image

		/* Check quality option. If quality is greater than 100, return error */
		if ($quality > 100) 
		{
			echo 'The maximum quality is 100. <br>Quality changes only affect JPEG images.';
		}
		else 
		{
			header('Content-Type: '.$size['mime']); // Set the mime type for the image

			switch ($size['mime']) 
			{
				case 'image/jpeg':
					$im = imagecreatefromjpeg($source);
					imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original JPEG
					imagejpeg($resize, '', $quality); // Output the new JPEG
					break;

				case 'image/png':
					$im = imagecreatefrompng($source);
					imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original PNG
					imagepng($resize, '', $quality); // Output the new PNG
					break;
			} //ends the switch statement

			imagedestroy($im);
		} //closes the if quality conditional

	} //closes the if file-exists conditional
}//closes function 

thumb($_GET['source'], $_GET['quality']);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top