【问题标题】:Rails + Carrierwave + RMagick: GIF converts to JPG but doesn't save right file extensionRails + Carrierwave + RMagick:GIF 转换为 JPG 但不保存正确的文件扩展名
【发布时间】:2013-11-09 12:07:50
【问题描述】:

我目前正在尝试获取 gif 文件的第一帧,调整其大小并将其保存为 jpg 文件。

我认为转换似乎很好。但它不会以正确的文件扩展名保存它。它仍然保存为 .gif 因此,当我尝试打开它时,它说无法打开图像,似乎不是 GIF 文件。然后我自己重命名扩展名,它就可以工作了。

这是我的处理代码:

version :gif_preview, :if => :is_gif? do
  process :remove_animation
  process :resize_to_fill => [555, 2000]
  process :convert => 'jpg'
end

def remove_animation
  manipulate! do |img, index|
    index == 0 ? img : nil
  end
end

【问题讨论】:

    标签: ruby-on-rails carrierwave rmagick


    【解决方案1】:

    实际上还有另一种更简洁的方法来实现这一点;它甚至在官方 wiki 中有一些记录:How To: Move version name to end of filename, instead of front

    使用此方法,您的 version 代码将如下所示:

    version :gif_preview, :if => :is_gif? do
      process :remove_animation
      process :resize_to_fill => [555, 2000]
      process :convert => 'jpg'
    
      def full_filename(for_file)
        super.chomp(File.extname(super)) + '.jpg'
      end
    end
    
    def remove_animation
      manipulate! do |img, index|
        index == 0 ? img : nil
      end
    end    
    

    【讨论】:

      【解决方案2】:

      所以......经过数小时的头痛后,我终于找到了解决方案,为什么这不起作用。原来你必须先触摸/创建一个文件才能完成这项工作。我也从 RMagick 切换到 Mini Magick。不是出于特定原因,只是尝试了它是否可以与 MiniMagick 一起使用,但我仍然遇到同样的问题。这是我使用 Mini Magick 的新流程代码:

      version :gif_preview, :if => :is_gif? do
        process :gif_to_jpg_convert
      end
      
      def gif_to_jpg_convert
        image = MiniMagick::Image.open(current_path)
        image.collapse! #get first gif frame
        image.format "jpg"
        File.write("public/#{store_dir}/gif_preview.jpg", "") #"touch" file
        image.write "public/#{store_dir}/gif_preview.jpg"
      end
      

      我只是不明白为什么关于这个的文档真的是 0 ...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-06
        • 1970-01-01
        • 2020-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-24
        相关资源
        最近更新 更多