2013-11-09 35 views
1

我目前正在嘗試獲取gif文件的第一幀,調整其大小並將其保存爲jpg文件。Rails + Carrierwave + RMagick: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 

回答

3

其實另一個沒有,更清潔的方式來實現這一目標;它甚至有些記錄在官方的wiki:How To: Move version name to end of filename, instead of front

使用這種方法,您版本代碼應該是這樣的:

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  
1

所以...我終於找到了解決的頭痛小時後爲什麼沒有工作。原來你必須先觸摸/創建一個文件才能完成這項工作。我也從RMagick轉到Mini Magick。不是因爲特殊的原因才試用它,如果它能與MiniMagick一起使用,但我仍然有同樣的問題。這是我與小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 documenation ...

相關問題