2012-11-20 22 views

回答

12

我覺得MiniMagick已經發生了一些變化,因爲我花了三個小時試圖找出爲什麼安德烈的代碼沒不爲我工作。

我得到了以下錯誤:

ActiveRecord::RecordInvalid (Validation failed: 
Image Failed to manipulate with MiniMagick, maybe it is not an image? 
Original Error: Command 
("mogrify -scene /var/folders/0o/0oqNck+++TI/-Tmp-/mini_magick2022-499-15zc.gif") 
failed: {:status_code=>1, :output=>"mogrify: invalid argument for option 
`/var/folders/0o/0oqNck+++TI/-Tmp-/mini_magick2022-499-15zc.gif': -scene 
@ error/mogrify.c/MogrifyImageCommand/5558.\n"}) 

最後我發現,MiniMagick ::圖像具有方法collapse!(這裏找到:http://www.ruby-doc.org/gems/docs/j/jf--mini_magick-3.1/MiniMagick/Image.html#method-i-collapse-21),它解決了這個問題:

process :remove_animation 

def remove_animation 
    manipulate! do |img| 
    if img.mime_type.match /gif/ 
     img.collapse! 
    end 
    img 
    end 
end 
+0

我還沒試過,但聽起來很理想。感謝發佈。 – alistairholt

+0

可以重寫方法來說:'操作!(&:崩潰!)if content_type =='image/gif'' –

3

它的作品對我來說:

def only_first_frame 
    manipulate! do |img| 
     if img.mime_type.match /gif/ 
     if img.scene == 0 
      img = img.cur_image #Magick::ImageList.new(img.base_filename)[0] 
      else 
      img = nil # avoid concat all frames 
     end 
     end 
     img 
    end 
    end 

然後,你必須調用:

process :only_first_frame 
+0

道歉,它花我很長時間才能接受你的答案! – alistairholt