PHP getimagesize () mixes width and height

I use a PHP script that loads an image, then gets the dimensions using PHP getImageSize (), and then does things with the image according to the orientation of the images (portrait or landscape).

However (PHP version 5.4.12) in some .jpg files, it gets the height and width as they are, and in some (taken from the iPhone) it changes them, thinking that portrait paintings are actually a landscape.
This happens not only on my local Wampserver, but also on a remote server (with a different version of PHP).

Someone tell me how

1) to repair it or 2) to find a way to solve the problem?

+7
php orientation
source share
1 answer

Some cameras include an orientation tag in the metadata section of the file itself. This means that the device itself can show it in the correct orientation every time, regardless of the orientation of the image in its original data.

Windows doesn't seem to support reading this orientation tag and instead just reads the pixel data and displays it as is.

The solution would be to either change the orientation tag in the metadata with scary images based on each image, OR

Use the exif_read_data() PHP function to read the orientation and orient the image as follows:

 <?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 ?> 

Literature:

+6
source share

All Articles