How to fix image orientation on PHP without Imagick and EXIF extension?

Alexey Fedorchak
2 min readApr 13, 2021

If you are here then you probably has troubles with image orientation after uploading on server?

The good news is there is nothing wrong with your image! :D

Problem is related to metadata which are saved in your image file. This metadata told your OS (Windows, Linux or MacOS) about correct image orientation. So, when you tried to look on your image from PC you see that images has correct orientation.. but when you do just a raw uploading, you see that the image is accidentally rotated left or right..

How to fix this issue?

The solution is simple: do as your OS does! Read this metadata and rotate image to correct orientation.

If you have Imagick installed then you just need to read exif image orientation metadata from your image file…

But what to do if you don’t have Imagick/EXIF and you are lazy enough to install it? :D

Well if you don’t have Imagick you need to use some third party utility. This utility called “exiftran” and you can install it using this commands:

sudo apt-get update -y
sudo apt-get install -y exiftran

After you can auto-orient your image by running this command:

exiftran -a -i image.png

This will auto-orient image.png and update the metadata.

Now to do it on PHP?

You can do it just by accessing your terminal with shell functions. For example you can do like this:

shell_exec(‘exiftran -a -i image.png‘);

That’s all!
This code will update the image.png and fix the orientation.

Hope it would be useful for you, at least, couple months ago I was looking for such article but nobody solved this issue this way :D

If you have questions of comments, please contact me via Linkedin:
shorturl.at/abrFK or via email: ofedorchak68@gmail.com.

--

--