Search code examples
ruby-on-railscarrierwavermagick

Rails + Carrierwave + RMagick: GIF converts to JPG but doesn't save right file extension


I'm currently trying to get the first frame of a gif file, resize it and save it as a jpg file.

The conversion seems fine I think. But it doesn't save it with the right file extension. It still gets saved as .gif So when I try to open it it says can't open image, doesn't seem to be a GIF file. Then I rename the extension myself and it works.

Here is my processing code:

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

Solution

  • There is actually another, cleaner way to achieve this; and it is even somewhat documented in the official wiki: How To: Move version name to end of filename, instead of front

    Using this method your version code would look like this:

    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