Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getimagesize() does not account for EXIF orientation #2745

Open
PrettyAnalystGirl opened this issue Sep 6, 2023 · 6 comments
Open

getimagesize() does not account for EXIF orientation #2745

PrettyAnalystGirl opened this issue Sep 6, 2023 · 6 comments

Comments

@PrettyAnalystGirl
Copy link

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
Copy link
Member

damianwadley commented Sep 6, 2023

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
Copy link
Author

This was fantastic, thank you!

@damianwadley
Copy link
Member

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 damianwadley reopened this Sep 6, 2023
@damianwadley damianwadley changed the title getimagesize() function parameter sequence getimagesize() does not account for EXIF orientation Sep 6, 2023
@jimav
Copy link

jimav commented Jun 5, 2024

@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
Copy link
Member

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

@cmb69
Copy link
Member

cmb69 commented Jul 21, 2024

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants