使用carrierwave和rmagick上传到S3 EXIF​​图像旋转问题图像、问题、rmagick、carrierwave

2023-09-11 08:18:00 作者:梨窝浅笑╯

我已经在我的Rails应用程序中的照片上传功能。该应用程序直接上传通过rmagick和雾通过carrierwave到S3。我遇到的问题是,当照片通过手机,通过上传拍照选项,在纵向(注意,这是与iPhone,但我认为Android已经同样的问题)。一旦上传图像出现细微的移动,桌面上的图像会出现旋转90度,但是看的时候。

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 mobile through the "take a photo option" in portrait (note this is with iphone but I believe android has the same issue). Once uploaded the image appears fine on mobile, however when viewed on desktop the image appears rotated 90 degrees.

通过我的研究,它看起来是一个问题EXIF。这计算器响应概述2可能的解决方案。这主旨也看起来很有希望为好。

Through my research it looks to be an issue with exif. This stackoverflow responder outlines 2 potential solutions. This gist also looks promising as well.

到目前为止,我已经找到张贴,但没有制定出台了一些解决方案。理想情况下,我想照片保存到S3作为一个人像,然后只显示图像不变。

So far I have found a few solutions posted but none have worked. Ideally I would like the photo to be saved to s3 as a portrait, then just display the image as is.

任何建议非常AP preciated。

Any suggestions are well appreciated.

下面是我的code

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上传的指南。

btw I used this Railscast as a guide for setting up my s3 upload.

推荐答案

嗯,我得到这个使用雾工作,而不是或carrierwave_direct。

Well I got this working using fog instead or carrierwave_direct.

下面是code,结束了为我工作:

Below is the code that ended up working for me:

应用程序/上传者/ image_uploader.rb

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

应用程序/模型/ s3_image.rb

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

初​​始化/ carrierwave.rb

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