2011-03-13 148 views
11

我想为多种格式使用单个文件字段。这是我的理解,回形针足够聪明,只能缩放图像,并保留其他格式,但这似乎不适用于FLV(它返回imagemagick /识别错误)。有什么方法可以帮助Paperclip稍微有点并且明确地设置特定的格式来进行缩放?仅回形针处理图像

UPDATE: 显然,防止这些错误:烦躁=>假(感谢fl00r),其用于定期上传工作正常。 但是,我想在这里做的是通过FTP上传文件,然后通过代码使用附件参数中的File.new([:path])创建新记录。这就像图像的魅力一样,但是:whiny => false-trick不会再做了。有没有人有任何提示呢?

回答

13

设置:whiny选项设置为false:

has_attached_file :my_attach, :whiny => false ... 

它不会帮助peparclip只处理图像,但如果处理失败

UPD

处理也不会抛出错误仅供图像:

has_attached_file :file, 
    :styles => lambda{ |a| ["image/jpeg", "image/png"].include?(a.content_type) ? { :small => "90x90#" } : {} } 

您可以在其中添加更多内容类型["image/jpeg", "image/png"] array

+0

更多细节

before_post_process :process_only_images def process_only_images %w(image/jpeg, image/png,image/gif,image/pjpeg, image/x-png).include?(attachment_content_type) end 

检查回形针的文档,似乎有很大的帮助,谢谢! – Jpunt 2011-03-15 16:38:35

+0

这里有些更新 – fl00r 2011-03-16 09:53:26

+0

是的,这应该工作,非常感谢! – Jpunt 2011-03-16 12:06:16

3

您还可以使用回形针的回调来处理图像,并指示回形针仅处理图像。如果before_post_process回调返回false,则处理停止。在https://github.com/thoughtbot/paperclip#events

+0

我喜欢这种方式,但代码不太正确: %w(image/jpeg image/png,image/gif image/pjpeg image/x-png).include?(attachment_content_type) – 2014-01-23 15:57:46

+2

你必须小心,这不是你如何使用%w声明一个数组。用这个代替'%w(image/jpeg image/png image/gif image/pjpeg image/x-png).include?(resource_content_type)' – fenec 2015-09-04 14:58:50