Home >Backend Development >PHP Tutorial >The Orientation attribute in PHP determines whether the uploaded image needs to be rotated, _PHP tutorial
When using Apple’s iOS system to take photos and upload images, you may encounter the problem of the image being rotated. This is mainly It depends on the position of the photo button when you take a photo. If you rotate the phone with the bottom facing up when taking a photo, the photo will also be rotated.
The following code will ensure that all uploaded photos are oriented correctly when uploaded:
<?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 ?>
After testing, the Orientation attribute of Android photos is all 1, and it cannot be determined whether it has been rotated.