Detect images orientation in PHP after upload

phone-690091_640Sometimes it happens to have to make an app that allow the user to make photos.

It’s seems to be a typical (and then an easy) procedure or action for a mobile app. Nevertheless it might be no so easy.

First of all you have to consider your enemy: iOS images orientation. iOS? Yes!
In fact if you’re using an Apple device you’ve to check the orientation. In Android it’s not necessary. The user could make his photo in landscape or portrait. Then, after the upload, you have to read the “header” info file and then make your choice.

In this article I’ll talk about just how to read this info.

So let’s start with the example:

Assuming you did an upload, you can use the exif_read_data method to know the image orientation:

if(move_uploaded_file($_FILES[‘file’][‘tmp_name’], $newFile)){
$exif = exif_read_data($newFile);
$ort = $exif[‘Orientation’];

where “$newFile” is the path of the image uploaded on the server.

After that a simple switch over the $ort property will solve the problem:

case 3:
//rotate +90°
case 6:
//rotate -90° . Better a positive value: 270°