如何使用RMagick获取图像类型?
我需要找到真正的图像类型,因为很多时候它们都带有错误的扩展。
这与RMagick有可能吗? 怎么样?
解:
image.format
#
如果你想要更通用的解决方案(在linux上):
require "open3" def File.sys_file_cmd(path) if File.exist? path stdin,stdout,stderr = Open3.popen3(%{file --mime -b "#{path}"}) file_err = stderr.gets file_out = stdout.gets if file_err raise Exception, "The 'file' command error: #{file_err} : with #{path}" end if file_err.nil? && (!file_out.nil? && !file_out.empty?) return file_out.split(";")[0].strip end end return nil end