Skip to main content
Topic: Image Orientation (Read 10695 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Image Orientation

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)

Code: [Select]
/**
 * 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:
Code: [Select]
			// 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.


Re: Image Orientation

Reply #1

And what those images look like with the function enabled (remember Imagick only)

Re: Image Orientation

Reply #2

Impressive! :D

 emanuele likes this stuff! nods
Bugs creator.
Features destroyer.
Template killer.

Re: Image Orientation

Reply #3

I also noticed that and also some users asked why their photos are rotated. I agree, all photos are from iPhone or Ipad.

It will be nice to have it, so I don't have to correct attachments.

P. S. Spuds do you think that it can be used together with resize attachments addon?
sorry for my bad english

Re: Image Orientation

Reply #4

Quote from: radu81 – P. S. Spuds do you think that it can be used together with resize attachments addon?
It should work perfectly with that addon.  Add the new function above to Graphics.subs.php and then in the function resizeImage()  find
Code: [Select]
			// Get a new instance of Imagick for use
$imagick = new Imagick($destName);
and add after that
Code: [Select]
			// Fix any rotations that we need to
autoRotateImage($imagick);

Like anything there could be a stray miss here or there, but in general it should work well.   Do note you must have Imagick enabled on the server, you can check by going to /index.php?action=admin;area=credits it will list it under Support Information if its installed.

Re: Image Orientation

Reply #5

I'll try, thanks for helping
sorry for my bad english

Re: Image Orientation

Reply #6

Cool. A notice with an action would be great

Re: Image Orientation

Reply #7

What do you think to add it in the core for next release 1.0.3?
sorry for my bad english

Re: Image Orientation

Reply #8

I already started preparing for 1.0.3, if Spuds thinks it is safe we can try, otherwise it may go into 1.0.4.
Also because 1.0.3 has already quite a few changes, probably much more than a micro release should have. LOL
Bugs creator.
Features destroyer.
Template killer.

Re: Image Orientation

Reply #9

Yeah I'll add it to 1.04, I running the code on a live site now to make sure there are no bizzaro issues ... it needs a checkbox in the ACP (I think) to disable the feature (I guess), or maybe just one of those unused modsettings so addons can turn it off ... thoughts on that?

And I may do a GD version if the EXIF lib is also available, no promises there :P

To note the only trade off that can happen is:
- someone has a rotated image that has the EXIF orientation flag is properly set, so a standard iphone snappy
- they take that image and manually rotate it upright, with a program that does not understand / update the EXIF orientation header.
- save the image, its now upright BUT the flag is not updated
- upload and it will get re-rotated.

OK that is going to occur a lot less frequently than what we are fixing with the function, just saying that is the downside, but if that occurs its no worse off.

Re: Image Orientation

Reply #10

How about more image controls in general? Seems like something that a lot of people would want and would fit in the realm of a forum.

Re: Image Orientation

Reply #11

This is very nice improvement. I love it.

Re: Image Orientation

Reply #12

Please don't forget this for the 1.0.4 version ;)
sorry for my bad english