问题
I need to find the real type of images, as a lot of times they come with the wrong extension.
Is this possible with RMagick? How?
回答1:
Solution:
image.format
#
回答2:
if you want more generic solution (on 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
来源:https://stackoverflow.com/questions/9840112/how-to-get-image-type-with-rmagick