31

我的 Rails 应用程序中有照片上传功能。该应用程序通过rmagick 和fog 通过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

应用程序/模型/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

配置/初始化程序/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 上传的指南。

4

4 回答 4

36

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
于 2013-09-06T05:29:16.460 回答
15

我遇到了类似的问题,并使用与您的方法几乎相同的方法进行了修复。

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

(请注意,我不是在打电话auto_orient!- 只是auto_orient,没有bang。)

然后我将其作为我创建process :auto_orient的任何内容的第一行。version例如:

version :square do
  process :auto_orient
  process :resize_to_fill => [600, 600]
end
于 2013-08-30T21:43:34.267 回答
6

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

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

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

NoMethodError (undefined method `write' for "":String):
于 2014-01-10T05:54:55.867 回答
2

Lando2319 的回答对我不起作用。

我正在使用 RMagick。

我设法通过使用以下方法使 ImageMagick 应用正确的方向(并重置 EXIF 旋转数据以避免查看器进行双重旋转):

def fix_exif_rotation # put this before any other process in the Carrierwave uploader

manipulate! do |img|
  img.tap(&:auto_orient!)
end

我的解决方案和兰多的解决方案之间的区别在于爆炸(!)。就我而言,这是绝对必要的。

于 2016-04-12T12:53:59.513 回答