【问题标题】:Rails + Carrierwave + MiniMagick: How to preserve gif animation?Rails + Carrierwave + MiniMagick:如何保存 gif 动画?
【发布时间】: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


【解决方案1】:

问题在于 DropzoneJS。所有上传的文件都会在本地调整大小。当一个 GIF 被 DropzoneJS 调整大小时,它会被展平并转换为 PNG。

然后,Carrierwave 向服务器发送一个带有 GIF 扩展名的 PNG 文件。

我的最终 DropzoneJS 设置(有效)是:

$('.dropzone').dropzone({
    paramName: 'attachment[image]',
    maxFilesize: 3,
    thumbnailWidth: 189,
    previewsContainer: '#sended-images',
    previewTemplate: $('#preview').html(),
    timeout: 360000,
    acceptedFiles: 'image/*,.jpg,.png,.jpeg,.gif',
    accept: function(file, done) {
        var mime_type = file.type;
        if ( mime_type != 'image/gif' ){
            this.options.resizeWidth = 1800;
            this.options.resizeQuality = 90;
            done();
        } else {
            this.options.resizeWidth = null;
            this.options.resizeQuality = null;
            done();
        }
        file.status = Dropzone.ADDED;
        done();
    },
    init:function(){
        this.on('success',function(f,d){
            ...
        });
        this.on('sending',function(f,x,d){
            ...
        });
        this.on('addedfile',function(f,x,d){
            ...
        });
        this.on('error', function(f, response) {
            ...
        });
        this.on("maxfilesexceeded", function(file){
            ...
        });
    }
});

我的独特问题是:

如果我发送多个首先处理 GIF 的文件,则不会调整下一个文件的大小。如果处理的第一个文件不是 GIF,则将调整下一个文件的大小并且 GIF 将不起作用。

【讨论】:

    猜你喜欢
    • 2012-11-08
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多