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!
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.
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.
I've always done this:
list($width, $height) = getimagesize('image.jpg');
if ($width > $height) {
// Landscape
} else {
// Portrait or Square
}
list($width, $height) = getimagesize("path/to/your/image.jpg");
if( $width > $height)
$orientation = "landscape";
else
$orientation = "portrait";
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With