【问题标题】:How to get image type with RMagick?如何使用 RMagick 获取图像类型?
【发布时间】:2012-03-23 13:33:52
【问题描述】:

我需要找到图像的真实类型,因为很多时候它们带有错误的扩展名。

RMagick 可以做到这一点吗?怎么样?

【问题讨论】:

    标签: ruby image-processing rmagick


    【解决方案1】:

    解决方案:

    image.format
    
    #

    【讨论】:

      【解决方案2】:

      如果您想要更通用的解决方案(在 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-01
        • 1970-01-01
        • 2021-10-24
        • 1970-01-01
        • 2014-10-03
        • 1970-01-01
        相关资源
        最近更新 更多