Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP GetImageSize reverses Width/Height

I have the image attached below with these specifics: Width: 3024px Height: 4032px

The getimagesize() function returns the opposite:

$size = GetimageSize("test.jpg");
echo "image width: " . $size[0] . ", height: " . $size[1];

image width: 4032, height: 3024

How is this possible?

You can try yourself using the image below.

* UPDATE * Removed image from the post and added a link to zip file (containing the image) because the image works fine after being processed from stack overflow. Tinyupload ZIP

Screenshot of the result: enter image description here

like image 689
MeV Avatar asked Sep 28 '26 17:09

MeV


1 Answers

Your image is probably being auto-rotated when you view it. Orientation will play a part in getting the height and width the right way around. This code taken from the php documentation will make sure your image is correctly rotated. There are many other examples in the link to choose from.

<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
    switch($exif['Orientation']) {
        case 8:
            $image = imagerotate($image,90,0);
            break;
        case 3:
            $image = imagerotate($image,180,0);
            break;
        case 6:
            $image = imagerotate($image,-90,0);
            break;
    }
}
// $image now contains a resource with the image oriented correctly
?>
like image 141
lloydpick Avatar answered Sep 30 '26 06:09

lloydpick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!