exif image rotation issue using carrierwave and rmagick to upload to s3

前端 未结 4 1643
自闭症患者
自闭症患者 2020-11-30 03:36

I\'ve got a photo upload feature in my rails app. The app uploads direct to s3 through carrierwave via rmagick and fog. The issue I am having is when a photo is uploaded via

相关标签:
4条回答
  • 2020-11-30 03:36

    I had a similar problem and fixed it with an approach nearly identical to yours.

    # In the uploader:
    def auto_orient
      manipulate! do |img|
        img = img.auto_orient
      end
    end
    

    (Note that I am not calling auto_orient! - just auto_orient, without the bang.)

    Then I have process :auto_orient as the first line of any version I create. For example:

    version :square do
      process :auto_orient
      process :resize_to_fill => [600, 600]
    end
    
    0 讨论(0)
  • 2020-11-30 03:36

    Lando2319's answer was not working for me.

    I am using RMagick.

    I managed to make ImageMagick apply the correct orientation (and to reset the EXIF rotation data in order to avoid a double rotation by the viewer) by using :

    def fix_exif_rotation # put this before any other process in the Carrierwave uploader
    
    manipulate! do |img|
      img.tap(&:auto_orient!)
    end
    

    The difference between my solution & Lando's is the bang (!). In my case it was absolutely necessary.

    0 讨论(0)
  • 2020-11-30 03:41

    My solution (quite similar to Sumeet) :

    # painting_uploader.rb
    process :right_orientation
    def right_orientation
      manipulate! do |img|
        img.auto_orient
        img
      end
    end
    

    It's really important to return an image. Otherwise, you'll get an

    NoMethodError (undefined method `write' for "":String):
    
    0 讨论(0)
  • 2020-11-30 03:56

    Well I got this working using fog instead or carrierwave_direct.

    Below is the code that ended up working for me:

    app/uploaders/image_uploader.rb

    class ImageUploader < CarrierWave::Uploader::Base
       include CarrierWave::MiniMagick
    
       include Sprockets::Helpers::RailsHelper
       include Sprockets::Helpers::IsolatedHelper
    
       storage :fog
    
      # Override the directory where uploaded files will be stored.
      # This is a sensible default for uploaders that are meant to be mounted:
      def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
    
    
      def fix_exif_rotation #this is my attempted solution
        manipulate! do |img|
          img.tap(&:auto_orient)
        end
      end
    
      process :fix_exif_rotation
    end
    

    app/models/s3_image.rb

    class S3Image < ActiveRecord::Base
      attr_accessible :image, :name, :user_id, :image_cache
      mount_uploader :image, ImageUploader
    
      belongs_to :user
    end
    

    initializers/carrierwave.rb

    CarrierWave.configure do |config|
      config.fog_credentials = {
        provider: "AWS",
        aws_access_key_id: " ... ",
        aws_secret_access_key: " ... ",
        region: 'us-west-2'
      }
      config.fog_directory = " ... "
    end
    
    0 讨论(0)
提交回复
热议问题