exif图像旋转问题使用carrierwave和rmagick上传到s3

lan*_*319 28 exif ruby-on-rails rmagick amazon-s3 carrierwave

我的rails应用程序中有一个照片上传功能.该应用程序通过rmagick和雾直接通过carrierwave上传到s3.我遇到的问题是通过手机上的"拍照选项"上传照片时(注意这是iphone,但我相信Android有同样的问题).上传后,图像在手机上显示正常,但在桌面上查看时,图像会旋转90度.

通过我的研究,它看起来是exif的一个问题.此stackoverflow响应程序概述了2个可能的解决方案.这个要点也看起来也很有希望.

到目前为止,我发现了一些解决方案,但没有一个有效.理想情况下,我希望将照片作为肖像保存到s3,然后只按原样显示图像.

任何建议都很受欢迎.

以下是我的代码

应用程序/上传/ image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWaveDirect::Uploader

  include CarrierWave::RMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  include CarrierWave::MimeTypes
  process :fix_exif_rotation
  process :set_content_type


  version :thumb do
    process resize_to_fill: [200, 200]
  end

  def extension_white_list
    %w(jpg jpeg png)
  end


  def fix_exif_rotation #this is my attempted solution
    manipulate! do |img|
      img = img.auto_orient!
    end
  end


end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ s3_image.rb

class S3Image < ActiveRecord::Base
  attr_accessible :image, :name, :user_id
  mount_uploader :image, ImageUploader

  belongs_to :user


  def image_name
    File.basename(image.path || image.filename) if image
  end


  class ImageWorker
    include Sidekiq::Worker

    def perform(id, key)
      s3_image = S3Image.find(id)
      s3_image.key = key
      s3_image.remote_image_url = s3_image.image.direct_fog_url(with_path: true)
      s3_image.save!
      s3_image.update_column(:image_processed, true)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

配置/初始化/ carrierwave.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    provider: "AWS",
    aws_access_key_id: " ... ",
    aws_secret_access_key: " ... "
  }
  config.fog_directory = " ... "
end
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我使用这个Railscast作为设置我的s3上传的指南.

lan*_*319 35

好吧,我使用雾代替或者运行了carrierwave_direct.

以下是最终为我工作的代码:

应用程序/上传/ 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
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ s3_image.rb

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

  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

初始化/ 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
Run Code Online (Sandbox Code Playgroud)

  • @JonathanAllard`trobulate!`块想要一个图像,但`image.auto_orient`返回一个空字符串.`image.tap(&:auto_orient)`大致对应``image.auto_orient; image`. (12认同)

小智 15

我遇到了类似的问题并用与你的方法几乎相同的方法修复了它.

# In the uploader:
def auto_orient
  manipulate! do |img|
    img = img.auto_orient
  end
end
Run Code Online (Sandbox Code Playgroud)

(请注意,我没有打电话auto_orient!- 只是auto_orient,没有爆炸.)

然后我process :auto_orient作为version我创造的任何一条线的第一线.例如:

version :square do
  process :auto_orient
  process :resize_to_fill => [600, 600]
end
Run Code Online (Sandbox Code Playgroud)


Noé*_*her 6

我的解决方案(与Sumeet非常相似):

# painting_uploader.rb
process :right_orientation
def right_orientation
  manipulate! do |img|
    img.auto_orient
    img
  end
end
Run Code Online (Sandbox Code Playgroud)

返回图像非常重要.否则,你会得到一个

NoMethodError (undefined method `write' for "":String):
Run Code Online (Sandbox Code Playgroud)

  • 你的解决方案对我有用,而@ Sumeet没有,尽管有多么相似.另外,感谢有关*返回图像*的提示.为了记录,我有一个类似的错误:NoMethodError(未定义的方法'destroy!'为"":String)`. (3认同)