Mixed file types with CarrierWave

不想你离开。 提交于 2019-12-30 18:07:06

问题


I've a CarrierWave uploader that shall accept a variety of file types. Some are image types (e.g. jpg, png) others are not.

I would like to create a medium version of the uploaded file with

version :medium do
  process :resize_to_fit => [300, 300]
end

As this only works for image files, how can I distinguish between images and other types and omit the resizing for non-image files?

At the moment CarrierWave tries to process the file regardless of it's type which leads to a MiniMagick processing error if the file is not an image.


回答1:


According to the Carrierwave Docs you can do the conditional processing:

version :medium, :if => :image? do
  process :resize_to_fit => [300, 300]
end

protected

def image?(new_file)
  new_file.content_type.include? 'image'
end

Actually more full answer I found here



来源:https://stackoverflow.com/questions/9126117/mixed-file-types-with-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!