php / doc-en

English PHP documentation
502 stars 732 forks source link

getimagesize() does not account for EXIF orientation #2745

Open PrettyAnalystGirl opened 1 year ago

PrettyAnalystGirl commented 1 year ago

From manual page: https://php.net/function.getimagesize

I believe the return array indices 0 and 1 are intended to be reversed.

Currently, index 0 is returning image height, while index 1 is returning image width.

I believe that index 3 is expecting the same logic reversal, as it produces a string "width="[0]" height="[1]"" -- the height value is being printed in the width attribute and the width value is being printed in the height attribute.


damianwadley commented 1 year ago

getimagesize will report the dimensions of the actual image data. This will not account for an image that has an orientation set in the EXIF data - which is common for photos from a smartphone held horizontally.

Please check that the image you're working with doesn't have such an orientation. With PHP, you can use exif_read_data. Otherwise, there are a variety of image tools and editors which can tell you about image orientation.

PrettyAnalystGirl commented 1 year ago

This was fantastic, thank you!

damianwadley commented 1 year ago

Actually, let's keep this open: getimagesize ought to have some note about image orientation, like it already does about things like bit depth and animated GIFs.

jimav commented 5 months ago

@damianwadley -- can you point to (or give) an example of how to use exif_read_data to get the actually-as-displayed height & width (taking into account any rotation encoded in exif data)?

damianwadley commented 5 months ago

@jimav I believe there are a few user comments on the documentation page for exif_read_data that show how.

cmb69 commented 3 months ago

To get the actual orientation, you can use something like:

if (($exif = exif_read_data($filename))) {
    $orientation = $exif["Orientation"] ?? 0;
} else {
    $orientation = 0;
}

If $orientation >= 5, you need to swap width and height. See https://jpegclub.org/exif_orientation.html for details.

(Proper handling for GD is a bit more contrieved, but is doable, but this would be off-topic here.)