1

All the code examples I see for PHP imagerotate using EXIF orientation value seem to not work when orientation is 6 or 8. On an orientation of 6, I do this to flip the image 90 degrees clockwise, which seems like it should be right and is similar to examples I've found: imagerotate($img, -90, 0) But I end up with an upside down photo.

Any ideas what I might be missing?

1 Answers1

0

An alternative solution is to use Imagick's getImageOrientation and rotateimage to do the job (Please note that your server must have Imagick installed -- most new servers have)

But in case it is exif data issue (as mentioned by StackSlave) then it may not work as expected.

This is the autoRoate function:

<?php
function autoRotateImage($image) {

    $orientation = $image->getImageOrientation();

    switch($orientation) {
        case imagick::ORIENTATION_BOTTOMRIGHT:
            $image->rotateimage("#000", 180); // rotate 180 degrees
        break;

        case imagick::ORIENTATION_RIGHTTOP:
            $image->rotateimage("#000", 90); // rotate 90 degrees CW
        break;

        case imagick::ORIENTATION_LEFTBOTTOM:
            $image->rotateimage("#000", -90); // rotate 90 degrees CCW
        break;

    }
    // Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image!

    $image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
}
?> 

To use it , you may use the codes below (which will save the image after rotation):

<?php

$image = new Imagick('./sourcepath/'.$upload1);
autoRotateImage($image);

// - Do other stuff to the image here -

$image->writeImage('./destinationpath/'. $upload1);
?>

if you are not saving the rotated image, you may use the following to display it (after rotation)

<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;

// Content type
header('Content-type: image/jpeg');

// Load
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, );

// Output
imagejpeg($rotate);

// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • Thanks, I will give that a try. But it seems that if ends up being an exif data issue there is no true solution. Any links to information on exif data issues and what might cause that? – user1550044 Jan 07 '21 at 14:39
  • Then it must be data issue. (as StackSlave has said - wrong direction in the first place). – Ken Lee Jan 07 '21 at 15:25
  • Ken thanks for your help but any clue as to why the data is wrong to start with? – user1550044 Jan 07 '21 at 16:18
  • Please try to use a smartphone to take a few photos (try to rotate the smartphone 90degree to take photos in landscape and portrait , and then test again (either by your codes or my codes). It should work (unless your smartphone is having problem which will cause data issue in the EXIF data) . Now if you tested your own smartphone and everything is fine, then please approach the one who sent you those photos and ask him whether his/her digital camera is having problem. – Ken Lee Jan 07 '21 at 16:53
  • Ken, I did test with my iPhone 7, and it seemed correct, as well as others that were sent to me. I did finally find the issue. I use dropzone.js and it's resizing functionality was saving the photo back out with exif data. Somehow, that was "doubling" the rotation even though the exif data was correct. The stackoverlow that helped me if anyone else happens to read this with the same problem: https://stackoverflow.com/questions/62759821/dropzone-is-modifying-my-image-before-sending-it – user1550044 Jan 08 '21 at 17:07
  • Great to know that you have found out the root cause of the problem. Have a nice day. – Ken Lee Jan 08 '21 at 17:10