我的rails应用程序中有一个照片上传功能 . 该应用程序通过rmagick和雾直接通过carrierwave上传到s3 . 我遇到的问题是当照片通过移动设备通过“拍照选项”上传时(请注意这是iphone,但我相信Android有同样的问题) . 上传后,图像在手机上显示正常,但在桌面上查看时,图像会旋转90度 .
通过我的研究,它看起来是exif的一个问题 . 这个stackoverflow responder概述了2个潜在的解决方案 . 这个gist看起来也很有希望 .
到目前为止,我发现了一些解决方案,但没有一个有效 . 理想情况下,我希望将照片作为肖像保存到s3,然后按原样显示图像 .
任何建议都很受欢迎 .
以下是我的代码
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
app / models / 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上传的指南 .