I often run in to attachments that have the wrong orientation when placed in a post.
Sometimes they are sideways, sometimes upside down. This always seems to come from some "smart" phone picture since with the phones "auto rotate" enabled the user simply does not know they are holding the bloody thing upside down or sideways. I mostly see this with apple iPhone or iPad stuff.
It seems those pictures do have the EXIF data in them that says which way the image should be oriented, its a value from 0-8. This page shows it pretty well http://www.impulseadventure.com/photo/exif-orientation.html for normal images, those same issues can exist for flipped images as well.
So in playing I have a quick function for auto correcting those images (Imagick only, no GD. May be a GD solution but I'm not going to do one)
/**
* Autorotate an image based on the EXIF Orientation tag.
*
* @param object $image
*/
function autoRotateImage($image)
{
// This method should exist if Imagick has been compiled against ImageMagick version
// 6.3.0 or higher which is forever ago, but we check anyway ;)
if (!method_exists($image , 'getImageOrientation'))
return;
$orientation = $image->getImageOrientation();
switch ($orientation)
{
// (0 & 1) Not set or Normal
case imagick::ORIENTATION_UNDEFINED:
case imagick::ORIENTATION_TOPLEFT:
break;
// (2) Mirror image, Normal orientation
case imagick::ORIENTATION_TOPRIGHT:
$image->flopImage();
break;
// (3) Normal image, rotated 180
case imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateImage("#000", 180);
break;
// (4) Mirror image, rotated 180
case imagick::ORIENTATION_BOTTOMLEFT:
$image->rotateImage("#000", 180);
$image->flopImage();
break;
// (5) Mirror image, rotated 90 CCW
case imagick::ORIENTATION_LEFTTOP:
$image->rotateImage("#000", 90);
$image->flopImage();
break;
// (6) Normal image, rotated 90 CCW
case imagick::ORIENTATION_RIGHTTOP:
$image->rotateImage("#000", 90);
break;
// (7) Mirror image, rotated 90 CW
case imagick::ORIENTATION_RIGHTBOTTOM:
$image->rotateImage("#000", -90);
$image->flopImage();
break;
// (8) Normal image, rotated 90 CW
case imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateImage("#000", -90);
break;
}
// Now that it's auto-rotated, make sure the EXIF data is correctly updated
if ($orientation >= 2)
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
}
To use you have to add that function to Graphics.subs.php, and then call it like:
// Get a new instance of Imagick for use
$imagick = new Imagick($destName);
// Fix any rotations that we need to
autoRotateImage($imagick);
Maybe an option for 1.1 to Auto Correct Rotation if Imagick is available. Below are the 8 examples of images you can get if you do not correct for them.