Open PrettyAnalystGirl opened 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.
This was fantastic, thank you!
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.
@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)?
@jimav I believe there are a few user comments on the documentation page for exif_read_data that show how.
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.)
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.