CarrierWave的混合文件类型 [英] Mixed file types with CarrierWave
问题描述
我有一个CarrierWave上传器,可以接受各种文件类型。有些是图像类型(例如jpg,png),有些则不是。
I've a CarrierWave uploader that shall accept a variety of file types. Some are image types (e.g. jpg, png) others are not.
我想用
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?
目前CarrierWave尝试处理文件,无论其类型如何导致MiniMagick处理如果文件不是图像则出错。
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.
推荐答案
根据 Carrierwave Docs 您可以进行条件处理:
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
这篇关于CarrierWave的混合文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!