Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve image orientation in PHP

How can I get the image orientation (landscape or portrait) of an image (JPEG or PNG) in PHP?

I created a php site where users can upload pictures. Before I scale them down to a smaller size, I want to know how the image is orientated in order to scale it properly.

Thanks for your answer!

like image 751
nimrod Avatar asked Nov 26 '12 15:11

nimrod


People also ask

How to get image orientation in PHP?

The imagerotate() function is an inbuilt function in PHP which is used to rotate an image with a given angle in degrees. The rotation center of the image is center.

How to rotate image PHP?

we will use imagecreatefrompng(), imagerotate() and imagepng() for rotate png image and save to server. same imagecreatefromjpeg(), imagerotate() and imagejpeg() for jpeg image. we will simple use imagecreatefrompng(), imagerotate() and imagepng() gd function of php for rotate and save image.


3 Answers

I've always done this:

list($width, $height) = getimagesize('image.jpg');
if ($width > $height) {
    // Landscape
} else {
    // Portrait or Square
}
like image 145
Jason Avatar answered Oct 06 '22 06:10

Jason


list($width, $height) = getimagesize("path/to/your/image.jpg");

if( $width > $height)
    $orientation = "landscape";
else
    $orientation = "portrait";
like image 40
Paddyd Avatar answered Oct 06 '22 06:10

Paddyd


I suppose you could check if the Image width is longer than the length for Landscape and for Portrait if the Length is longer than width.

You can do that with a simple IF / ELSE statement.

You could also use the function: Imagick::getImageOrientation

http://php.net/manual/en/imagick.getimageorientation.php

like image 27
ajtrichards Avatar answered Oct 06 '22 05:10

ajtrichards