Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails paperclip and upside down oriented images

I've recently encountered a problem where a user uploads an image and somewhere along the lines, paperclip is flipping it upside down.

The image in question can be seen here http://photoramblr.com/photos/36

As you see, the image is upside-down; but drag the image to your desktop and it will appear rightside-up. Since this image was taken on an iPhone I can only assume this is related to the image's orientation setting on the iPhone. Has anyone encountered anything like this or have any suggestions on how to address this?

The code here is pretty straightforward Paperclip lingo:

class Photo < ActiveRecord::Base
  has_attached_file :image,
    :storage => :s3,
    :s3_credentials => S3_CREDENTIALS,
    :styles => {
      :thumb => "100x100#",
      :small => "138x138>",
      :large => "580x580>",
      :x_large => "1600x1600>"}

Update

Hm, I was able to fix this by taking a screenshot of the image and uploading that. There must have been something in the meta-data that was setting the correct orientation that didn't make its way through.

like image 603
Tony Beninate Avatar asked Mar 04 '12 20:03

Tony Beninate


2 Answers

Source File Options

Paperclip added a source_file_options that allow you to pass processor options that get applied directly on the source file and before generating the subsequent thumbnails and styles.

You can add this to automatically orient your source file, like so:

class Photo < ActiveRecord::Base
  has_attached_file :image,
    storage:             :s3,
    s3_credentials:      S3_CREDENTIALS,
    source_file_options: { all:     '-auto-orient' },
    styles:              { thumb:   "100x100#",
                           small:   "138x138>",
                           large:   "580x580>",
                           x_large: "1600x1600>" }

This should be available since version 2.3.16 of the gem.

For more information, see the following issue on Paperclip's Github repo:

https://github.com/thoughtbot/paperclip/issues/591

Original Style

It's also not a terrible idea to set an original style in order to create an auto-oriented and size-limited version, like so:

original: "5000x5000>"

CAUTION: However, if you're expecting to get uploads that are not just images, like PDFs, it will cause problems by not keeping the original PDF and just storing an image of the first page of the PDF.

like image 27
Joshua Pinter Avatar answered Oct 14 '22 10:10

Joshua Pinter


Yes, this is a problem we solved last week where I work. :) If you're using ImageMagick/RMagic for image processing, you can use Image#auto_orient to "rotate or flip the image based on the image's EXIF orientation tag"; call this method on the image in a Paperclip processor and you should be good to go.

[Edit]

You may be interested in Rails, Paperclip, -auto-orient, and resizing.... I also found it interesting that CarrierWave made this process very easy:

class ImageUploader < CarrierWave::Uploader::Base
  ... # config here

  process :rotate

  def rotate
    manipulate! do |image|
      image.auto_orient
    end
  end
end
like image 191
Michelle Tilley Avatar answered Oct 14 '22 10:10

Michelle Tilley