gpt4 book ai didi

ruby-on-rails - 使用载波和rmagick上传到s3的exif图像旋转问题

转载 作者:行者123 更新时间:2023-12-03 09:45:57 25 4
gpt4 key购买 nike

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

最佳答案

好吧,我使用雾代替了这个方法,或者使用了carrierwave_direct。

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

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

初始值设定项/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

关于ruby-on-rails - 使用载波和rmagick上传到s3的exif图像旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com