How are you all working round the fact that pics uploaded from an iphone are 90 degrees rotated on elgg?

Hi - after a couple of days' researching I can see that anyone who runs blogs/social sites has dealt with the fact that most iphones, most of the time, do not upload images that show with the correct orientation when posted on the site. The workarounds can be backend (I've read about Drupal solutions - too complicated for me), I've read about solutions written into apps (such as Poster for Wordpress and the mobile solutions for PHPfox), and I've read about on-site solutions - where you can rotate the image once it has been uploaded. But there is nothing in elgg that I can find that deals with this - at a plugin or an app level (I can only find one iOS app for elgg). So there must be a workaround that you are all using that I am missing - could you please share it? Or perhaps explain why it isn't a natural inclusion in the core, in TinyMCE or tidypics etc? Thanks so much. P

  • I found the same problem. I've hacked together a fix that seems to work - but I need to make it into a plugin that is more maintainable. We are using a custom files plugin that stores files on Amazon S3 rather than on the local machine - but the fix should be the same. 

    In the file upload action, after saving the file object and before it begins dealing with the uploaded file, you need to detect the photo orientation and rotate it. The orientation is stored in the exif data. Code looks something like this:

    $exif = exif_read_data($_FILES['upload']['tmp_name'], 'IFDO', true);
    $orientation = $exif['IFD0']['Orientation'];;
    if($orientation != 0) {
          $image = imagecreatefromstring(file_get_contents($_FILES['upload']['tmp_name']));
          switch($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;
           }
           imagejpeg($image, $_FILES['upload']['tmp_name']);
    }

    Hope this helps. I'm slammed this week and probably next - but I'll see if I can pry out some time to put this into a plugin and upload it to the community. I hanen't  heard anyone else mention it so I figured it wasn't much of a problem. I think in TidyPics you can rotate images (I'm not using it at the moment) so perhaps that is why it has not surfaced as an issue.

  • I hadn't noticed this issue until now, that seems like a reasonable fix.  Tidypics isn't really under active development at the moment so I wouldn't expect this to find its way in on its own.

  • @Matt: ahem...: http://community.elgg.org/plugins/1194049/1.8.1beta5/elgg-18-tidypics

    Tidypics does currently not allow to rotate images and also does not check for image orientation using exif data or any other way.

    @Jimmy: thanks for the code snippet. I'll try to include it in the next Tidypics release.

  • sorry, I stand corrected :)

    iionly seems to have picked it up.  Good on you!

  • It probably wants to find its way into TidyPics, files, profile and groups. If iionly can put it into TidyPics that leaves the other three to be done. Since files, groups and user avatars all make various icon sizes it probably makes sense to hook the upload save action before it saves the file - rather than change each icon after the fact. Perhaps a plugin called image_orientation_fix?

    I think anyway - any thoughts? (Matt you always have pretty good insight into the technicalities)

    Its a bit of work and will require maintenance releases (or review) with each Elgg release.

  • @Jimmy, How would I use your code above in the action for the "files" plugin? Im using the "files" plugin as my image plugin (I don't want to use tidypics because I don't think users understand the album structure) I have a strictly mobile site and users just want to upload pictures not mess around with creating albums and such. Anyway "files" works great for what I'm doing, the only problem is it doesn't read exif data on upload thus leaving pictures in the wrong orientation. Oh one more thing, I think I'm the only person left on the planet that uses 1.7 (1.7.21). Any help would be much appreciated because I've searched high and low and I haven't come across much in the way of help for this. Thanks!

  • Elgg 1.9 will come with the same image orientation correction also implemented in Tidypics. I don't think it will be backported to Elgg 1.8 or 1.7 though. But you can study the code of the files plugin of the current dev version of Elgg 1.9 at github to figure out how you can backport the image orientation correction in the files plugin.

  • @ iionly

    Do you have a link to the github repository? I heard that the orientation fix was going to implemented in engine/lib/filestore.php not in the action folder of the "file" plugin. Is that true? I hope this isnt going to be a beast to figure out cause this is kinda a deal breaker for a project I have in the works. Thanks for all your help.

  • @ Pawel yeah thats the filestore file and I wouldnt know where to begin making that work for ELGG 1.7.