L
L
lexstile2021-07-22 15:59:23
PHP
lexstile, 2021-07-22 15:59:23

How to work with photo orientation in php?

I'm doing an avatar upload in my app, but ran into problems with some of the photos.
They have a different orientation than what I needed.

Started digging and came across :

$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;
    }
}


Then I ran into a problem that some of my photos began to turn in the wrong direction.
After some digging, I found orientation 6 in them.
exif-orientation-values.jpg?scale.option=fill&scale.width=512&scale.height=252

I look at the code that I took in the php tutorial and see that it would be logical to rotate clockwise, which is what the imagerotate function does in the sixth case (the negative angle, as I understand it, should provide rotation clockwise).

What did I miss? Why is my display upside down?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question