ElkArte Community

Elk Development => Feature Discussion => Topic started by: Spuds on March 04, 2015, 11:36:07 am

Title: Image Orientation
Post by: Spuds on March 04, 2015, 11:36:07 am
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.

Title: Re: Image Orientation
Post by: Spuds on March 04, 2015, 11:41:34 am
And what those images look like with the function enabled (remember Imagick only)
Title: Re: Image Orientation
Post by: emanuele on March 04, 2015, 11:54:00 am
Impressive! :D

/me likes this stuff! nods
Title: Re: Image Orientation
Post by: radu81 on March 04, 2015, 01:06:09 pm
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?
Title: Re: Image Orientation
Post by: Spuds on March 04, 2015, 01:23:32 pm
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.
Title: Re: Image Orientation
Post by: radu81 on March 04, 2015, 03:48:20 pm
I'll try, thanks for helping
Title: Re: Image Orientation
Post by: Joshua Dickerson on March 04, 2015, 09:04:08 pm
Cool. A notice with an action would be great
Title: Re: Image Orientation
Post by: radu81 on March 11, 2015, 06:09:16 am
What do you think to add it in the core for next release 1.0.3?
Title: Re: Image Orientation
Post by: emanuele on March 11, 2015, 11:52:00 am
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
Title: Re: Image Orientation
Post by: Spuds on March 11, 2015, 02:16:37 pm
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.
Title: Re: Image Orientation
Post by: Joshua Dickerson on March 12, 2015, 06:53:30 pm
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.
Title: Re: Image Orientation
Post by: ahrasis on March 13, 2015, 11:31:06 pm
This is very nice improvement. I love it.
Title: Re: Image Orientation
Post by: radu81 on May 18, 2015, 05:33:32 am
Please don't forget this for the 1.0.4 version ;)
Title: Re: Image Orientation
Post by: emanuele on May 19, 2015, 03:00:47 am
https://github.com/elkarte/Elkarte/pull/2073
Title: Re: Image Orientation
Post by: Jorin on October 18, 2016, 07:12:02 am
Your link says:

Quotehas no ACP settings, its simply On should you have imagik support.

My PHPInfo says I have imagik support, but my pictures are false orientied:

http://www.elkarte.net/community/index.php?topic=4068.0

 :(