导轨 + 载波 + RMagick:GIF 转换为 JPG,但不保存正确的文件扩展名



>我目前正在尝试获取 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

实际上还有另一种更干净的方法可以实现这一点; 它甚至在某种程度上记录在官方维基中: 如何:将版本名称移动到文件名的末尾,而不是前面

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

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    

所以...经过几个小时的头痛,我终于找到了解决方案,为什么这不起作用。事实证明,您必须先触摸/创建一个文件才能完成这项工作。我也从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 ......

最新更新