Find the image direction (portrait/landscape)

Is there a way to decide which direction a picture is; like which way (horizontal/vertical) has the camera been held when the picture was taken.

I’ve been looking at the [fphp]exif[/fphp] extension and other places but haven’t been able to find anything.

Thanks. :slight_smile:


$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];
$aspect = $height / $width;
if ($aspect >= 1) $mode = "vertical";
else $mode = "horizontal";

I don’t believe it’s set in Exif headers, so’s solution will work if it remains that in the ‘horizontal’ position, the image has a larger width than height.

The technical terms in art and photography are “portrait” for vertical mode and “landscape” for horizontal mode. The aspect ratio for a portrait will always be > 1, and the ratio for landscape will always be < 1. If the aspect ratio is exactly 1, you’re talking about a square as opposed to an oblong rectangle. Since you didn’t specify how you wished to handle that case, I arbitrarily lumped square images in with portraits.

Another note. If you want to get the dimensions of a GD image resource rather than an image file, use this instead of getimagesize():

$width = imagesx($img);
$height = imagesy($img);

Thank you both. I already knew about the aspect ratio so I guess I wasn’t clear enough with my question.

I’ll try again: A photographer take a portrait picture. He import the picture into his computer and, for some reason, he rotates it 90 degrees, so it is now landscape/horizontal orientated. He now uploads it to a website. Then I wonder, is there a way to determine that the picture really should be a portrait and not a landscape?

-Helge :slight_smile:

Off Topic: Thanks for translating the portrait and landscape. I just used the “norwegian” words and hoped people would understand. :slight_smile: