Getimagesize Problem

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
napper
Forum Newbie
Posts: 4
Joined: Sat Jul 05, 2003 10:08 am

Getimagesize Problem

Post by napper »

Hi everybody,

I just started with PHP and MySQL a couple of month ago, am making progress but still _so_ much to learn... Anyway, so now I am trying to build one of them fancy image galleries - you know - one of those where the user can click on a thumbnail on the "select" frame and the corresponding enlarged image would appear in the "details" frame. So far so good, works splendid and all - even with a MySQL database integrated and so forth. Now the only problem left is this: The images in the gallery have different proportions (3x4 and 4x3 respectively, depending on how my boss had turned his camera while taking them) and based on this the HTML of the detail frame should be different (e.g.: the small comments from the database should be on the bottom of the image for 3x4 images and on the right side for 4x3 images). I thought the easiest way to implement this would be read out the image proportions to build a small if loop of the kind:

if ($height > $width) { //some HTML}
else { //some othe HTML}

Now the only challenge I am unable to overcome is of how to convert the array that I get from getimagesize into variable ($width and $height respectively). Can anybody help me with this?

Regards,

Napper
napper
Forum Newbie
Posts: 4
Joined: Sat Jul 05, 2003 10:08 am

Post by napper »

Somebody showed me how it works by now. So the appropriate code works like this:

Code: Select all

$imagesize = getimagesize('imageFilename'); 
$width = $imagesizeї0]; 
$height = $imagesizeї1]; 

if ($height > $width) 
{ 
  # some HTML 
} 
else 
{ 
  # some HTML 
}
And the reason why is explained here:
Returns an array with 4 elements. Index 0 contains the width of the image in pixels. Index 1 contains the height. Index 2 is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF. These values correspond to the IMAGETYPE constants that were added in PHP 4.3. Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.
Splendid! And own he goes.... :)
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

you might wanna get used to the php tag. it's better than the code tage... also, i can cut that down....

Code: Select all

<?php list($width, $height)=getimagesize('imageFilename'); ?>
if the perosn isn't here you should get him to join. sounds like he'd be helpful. me.. i just saw this thread. i had the same issue earlier
napper
Forum Newbie
Posts: 4
Joined: Sat Jul 05, 2003 10:08 am

Post by napper »

I am not sure what you mean with getting used to the PHP tag instead of the of the code tag? As for the the piece of code you gave me: I was not familar with the list function as of yet so I did some reading on it. I think I understand of how it works except for one thing: how does PHP know which of the values in the getimagsize array it ought to assign to $width and which to $height? Does it have something to do with the order of values ([0] being width and [1] being height)? Would it work if I'd rephrase the whole thing to:

Code: Select all

<?php list($no, $clue)=getimagesize('imageFilename'); ?>
or something like that? :) Either way, thanks for the help. This language becomes more fascinating every day.

Napper
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i mean where in your post it reads code, in mine and your subsequent it reads php. that's what i meant by php in place of code

now for list getting ti from an array? list, if i understand it right, is also an array. so your making a one to one mapping
napper
Forum Newbie
Posts: 4
Joined: Sat Jul 05, 2003 10:08 am

Post by napper »

gotcha! 8)
Post Reply