我的Rails应用中有一个照片上传功能。该应用通过CarrierWave通过Rmagick和Fog直接上传到Amazon S3。我的问题是,当用户使用移动设备上的“拍照”选项(注:这是iPhone,但我认为Android也会有同样的问题)拍摄照片时,照片在移动设备上看起来很好,但在桌面浏览器上却旋转了90度。
经过我的调查,看起来似乎是由于Exif引起的问题。这个Stack Overflow回答者提出了两种潜在的解决方案。这个gist看起来也很有前途。
到目前为止,我已经找到了一些发布的解决方案,但都没有奏效。理想情况下,我希望将照片保存为竖屏照片,然后按原样显示图像。
欢迎提出任何建议。
以下是我的代码
app/uploaders/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
应用/模型/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
config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: " ... ",
aws_secret_access_key: " ... "
}
config.fog_directory = " ... "
end
顺便提一下,我使用了这个Railscast作为设置我的s3上传的指南。
image.auto_orient
返回一个空字符串。“image.tap(&:auto_orient)”大致相当于“image.auto_orient; image”。 - beniziimg.tap(&:auto_orient!)
(注意感叹号)才对我起作用。但我也使用了CarrierWave::RMagick
而不是CarrierWave::MiniMagick
。 - Sandro Limg.tap(&:auto_orient!)
。感谢您提供的fix_exif_rotation
方法。 - hoitomt