【发布时间】:2019-04-04 21:08:11
【问题描述】:
当我通过 Carrierwave 和 MiniMagick 上传 gif 图像时,动画被删除。我尝试了很多解决方案...
这个上传有点奇怪。如果我输入图像类型,它会返回“image/png”,但我发送了一个 gif。我也尝试过其他文件。
文件 app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :aws
def store_dir
"#{model.class.to_s.underscore}/#{mounted_as}/"
end
def fix_exif_rotation
manipulate! do |img|
img.tap(&:auto_orient)
end
end
version :thumb do
process resize_to_limit: [150, 100000]
process :quality => 90
process :fix_exif_rotation
end
version :original do
process :quality => 90
process :fix_exif_rotation
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
"#{secure_token}.#{file.extension}" if original_filename.present?
end
private
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) || model.instance_variable_set(var, SecureRandom.uuid)
end
end
如果我将原来的版本更改为:
version :original do
process :get_image_type
process :quality => 90
process :fix_exif_rotation
end
def get_image_type
puts @file.content_type
end
在控制台(rails s)中,它返回“image/png”。我正在尝试申请 this solution,但不起作用,我怀疑问题出在错误的 content_type 上。
我的环境
rails -v: Rails 4.2.1
ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
carrierwave-aws: 1.2.0
mini_magick: 4.8.0
OS: Ubuntu 16.04.5 LTS
【问题讨论】:
-
看起来 gif 需要额外处理。你遇到过这个吗? gist.github.com/sunny/9b107f2be0d0dff4f3ba
-
@RudyOnRailsTks 你的评论!我尝试了这个解决方案,但不起作用。 content_type "image/png" 阻止了解决方案的工作。我正在使用 Dropzone JS,我怀疑 Dropzone 正在将我的 gif 转换为 png。今天我来测试一下。
-
我也使用 Dropzone。 console.log 在 @vdropzone-sending 事件的任何处理程序中的文件。我做了一个 .gif 文件,我看到了
type: "image/gif" -
@RudyOnRails 是的,问题出在 Dropzone 上。如果具有调整大小/更改质量,则将图像转换为 PNG。如果没有调整大小/质量,GIF 将被正确处理。现在,我必须弄清楚如何使用 JPG/PNG 和 GIF。谢谢你的评论!
-
好吧,也许你从默认定制了你的,它做的事情和我的不一样?我可以很好地上传 gif 并且动画被保留。我将首先检查您的 Rails 日志以查看传入图像的参数...确保它是图像/gif。也许
fix_exif_rotation进程弄乱了 gif 的图层?
标签: ruby-on-rails ruby-on-rails-4 carrierwave minimagick