[SOLVED] getimagesize() question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

[SOLVED] getimagesize() question

Post by $var »

I'm using getimagesize() to try and contrain an image uploaded by users.
Some use spaces in filename (eg: John Hughes Picture.jpg) which throws the code off,
but sometimes, even when the name is properly (eg: seasons_chris_full.jpg) the resize doesn't work...

For example, the constraints shouldn't allow an image to be wider or taller than 125px,
however, there is an image displaying as (72dpi) 1500 x 2100px...

Code: Select all

$ID = $memberresults[Mem_ID];
					  $ceo = $memberresults[Mem_CImage];					  
					  $image =  "http://www.advantageboard.com/Imi/Mem_Img/$ID/$ceo";               
					  if ($ceo == "none") {
					  }
					  else {
					  $size = getimagesize("$image");
					  }
					  
					   $height = $size[1];
					   $width = $size[1];
					 if ($width > 125)
						 {
							   $width = 125;
							   $percent = ($size[1] / $height);
							   $width = ($size[0] / $percent);
						 }
					 else if ($width > 125)
						 {
							   $width = 125;
							   $percent = ($size[0] / $width);
							   $height = ($size[1] / $percent);
						 }
					  
				    $imageCEO=$memberresults["Mem_CImage"];
					if(empty($imageCEO) || $imageCEO =="none" ){
				    echo ""; 
				    }
				    else { 
				    echo "<img src='$image' height='$height' align='left' width='$width' border='0' vspace='2' hspace='2' class='caltable'>";
			  	    }
Last edited by $var on Tue Jun 13, 2006 10:50 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rawurlencode($ceo)

Your $width and $height variables are set to the same entry. It's a good idea to floor() the calculated dimensions, and you have two instances of trying to work based on $width being greater than 125. You may want to see which is greater, $width or $height, then calculate from there.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

thanks, i used rawurlencode and that seems to work well, and i switched my calculations and they are working now too!
thanks feyd.

Code: Select all

$height = $size[1];
					   $width = $size[1];
					 if ($width > $height)
						 {
							   echo "width is greater than height";
							   $width = 125;
							   $percent = ($size[1] / $height);
							   $width = ($size[0] / $percent);
						 }
					 else 
						 {
							   $height = 250;
							   $percent = ($size[1] / $height);
							   $width = ($size[0] / $percent);
						 }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$width and $height are still being set to the same entry from $size.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

Code: Select all

$height = $size[1];
					   $width = $size[0];
better?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$var wrote:

Code: Select all

$height = $size[1];
					   $width = $size[0];
better?
You got it. :wink:
Post Reply