1

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?

HappyDeveloper
  • 12,480
  • 22
  • 82
  • 117

2 Answers2

5

Solution:

image.format
#
HappyDeveloper
  • 12,480
  • 22
  • 82
  • 117
0

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
Vodun
  • 1,377
  • 1
  • 10
  • 12