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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple conditional problem 1

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
I have this code snippet which annoyingly doesn't work.
It did until I inserted the bits in red.
I have been using it for years but now need to add the conditional.
Main question is why it fails to work but also, why is $size (in blue) not an array. It works as a scalar but contains 2 pieces of data.
Is there a format of construct like the Perl 'if(($x > 3) or ($x < 40)){' in PHP? The manuals just refer to if and else then move on to something else
Code:
	[blue]$size[/blue]=GetImageSize($img_name);
[red]	$flag=0;
	if($size[0] > $MAXIMUM_WIDTH){
		$flag=1;
	}
	if($size[1] > $MAXIMUM_HEIGHT){
		$flag=1;
	}
	if($flag > 1){
[/red]
		$width_ratio = (double) ($size[0] / $max_width);
		$height_ratio = (double) ($size[1] / $max_height);
	
		if($width_ratio >=$height_ratio) 
		{
		   $ratio = $width_ratio;
		}
			else
		{
		   $ratio = $height_ratio;
		}
		$new_width    = ($size[0] / $ratio);
		$new_height   = ($size[1] / $ratio);
[red]	}else{
		$new_width    = $size[0];
		$new_height   = ($size[1];
	}
[/red]


Keith
 
$flag can never be > 1. Its only values are 0 and 1. I'd do it this way:
Code:
if($size[0] > $MAXIMUM_WIDTH || $size[1] > $MAXIMUM_HEIGHT){
and ditch all the "$flag" stuff. If you want to keep the $flag stuff, such as to add more checks that don't fit nicely into an if statement,
Code:
    if($flag == 1){

 
Thanks
That's the construct I have been looking for but the manuals I have and the online searches only show simple if else conditionals.
I had it set to always force the else option just for testing.
The stray bracket didn't help much either
Code:
   }else{
        $new_width    = $size[0];
        $new_height   = [red]([/red]$size[1];
    }
Thanks for the help - I'm on my way again


Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top