Ruby on Rails:如何检查文件是否为图像? [英] Ruby on Rails: How do you check if a file is an image?

查看:45
本文介绍了Ruby on Rails:如何检查文件是否为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查文件是否为图像?我想你可以使用这样的方法:

How would you check if a file is an image? I'm thinking you could use an method like so:

def image?(file)
  file.to_s.include?(".gif") or file.to_s.include?(".png") or file.to_s.include?(".jpg")
end

但这可能有点低效且不正确.有什么想法吗?

But that might be a little inefficient and not correct. Any ideas?

(顺便说一句,我正在使用回形针插件,但我没有看到任何方法来确定文件是否是回形针中的图像)

(I'm using the paperclip plugin, btw, but I don't see any methods to determine whether a file is an image in paperclip)

推荐答案

我会使用 ruby-filemagic gem,它是 libmagic 的 Ruby 绑定.

I would use the ruby-filemagic gem which is a Ruby binding for libmagic.

这篇关于Ruby on Rails:如何检查文件是否为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆