How to get image type with RMagick?

依然范特西╮ 提交于 2019-12-13 02:24:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!