有没有一种简单的方法可以在Ruby中获得图像尺寸?

有没有一种简单的方法可以在Ruby中获得图像尺寸?,ruby,image,osx-snow-leopard,dimensions,Ruby,Image,Osx Snow Leopard,Dimensions,我正在寻找一种简单的方法来获取Ruby中图像文件的宽度和高度尺寸,而不必使用ImageMagick或ImageScience(运行Snow Leapard)。您可以尝试以下方法(未经测试): 巴布亚新几内亚: GIF: 骨形态发生蛋白: JPG: class-JPEG 属性读取器:宽度、高度、位 def初始化(文件) 如果file.u是什么?木卫一 检查(文件) 其他的 open(File'rb'){| io | inspect(io)} 结束 结束 私有的 def检查(io) 除非io.ge

我正在寻找一种简单的方法来获取Ruby中图像文件的宽度和高度尺寸,而不必使用ImageMagick或ImageScience(运行Snow Leapard)。

您可以尝试以下方法(未经测试):

巴布亚新几内亚:

GIF:

骨形态发生蛋白:

JPG:

class-JPEG
属性读取器:宽度、高度、位
def初始化(文件)
如果file.u是什么?木卫一
检查(文件)
其他的
open(File'rb'){| io | inspect(io)}
结束
结束
私有的
def检查(io)
除非io.getc==0xFF&&io.getc==0xD8#SOI,否则引发“格式错误的JPEG”

class是一个Ruby库,用于计算各种图形格式的图像大小。gem可用,或者您可以下载源tarball并提取
image\u size.rb
文件。

回形针gem中有一种简便的方法:

>> Paperclip::Geometry.from_file("/path/to/image.jpg")
=> 180x180
这仅在安装了
identify
时有效。如果没有,如果安装了PHP,您可以执行以下操作:

system(%{php -r '$w = getimagesize("#{path}"); echo("${w[0]}x${w[1]}");'})
# eg returns "200x100" (width x height)

我终于找到了一种快速获取图像尺寸的好方法。您应该使用

require 'mini_magick'

image = MiniMagick::Image.open('http://www.thetvdb.com/banners/fanart/original/81189-43.jpg')
assert_equal 1920, image[:width]
assert_equal 1080, image[:height]
还有一个新的库(2011年7月)在最初提出这个问题时并不存在:rubygem(它似乎是由同样负责这里所建议的字节操作技术的Sam Stephenson编写的)

下面是来自项目自述文件的代码示例

require 'dimensions'

Dimensions.dimensions("upload_bird.jpg")  # => [300, 225]
Dimensions.width("upload_bird.jpg")       # => 300
Dimensions.height("upload_bird.jpg")      # => 225
截至2012年6月,这是一个很好的选择,它“通过尽可能少的抓取来查找给定uri的图像的大小或类型”。它适用于本地映像和远程服务器上的映像

自述文件中的IRB示例:

require 'fastimage'

FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
=> [266, 56]  # width, height
脚本中的标准数组分配:

require 'fastimage'

size_array = FastImage.size("http://stephensykes.com/images/ss.com_x.gif")

puts "Width: #{size_array[0]}"
puts "Height: #{size_array[1]}"
require 'fastimage'

width, height = FastImage.size("http://stephensykes.com/images/ss.com_x.gif")

puts "Width: #{width}"
puts "Height: #{height}"
或者,在脚本中使用多个赋值:

require 'fastimage'

size_array = FastImage.size("http://stephensykes.com/images/ss.com_x.gif")

puts "Width: #{size_array[0]}"
puts "Height: #{size_array[1]}"
require 'fastimage'

width, height = FastImage.size("http://stephensykes.com/images/ss.com_x.gif")

puts "Width: #{width}"
puts "Height: #{height}"

下面是ChristopheD的答案中的JPEG类的一个版本,它在Ruby 1.8.7和Ruby 1.9中都能工作。这允许您通过直接查看位来获得JPEG(.jpg)图像文件的宽度和高度。(或者,按照另一个答案中的建议,使用尺寸宝石。)

class-JPEG
属性读取器:宽度、高度、位
def初始化(文件)
如果file.u是什么?木卫一
检查(文件)
其他的
open(File'rb'){| io | inspect(io)}
结束
结束
私有的
def检查(io)
如果RUBY_版本>=“1.9”

类对于PNG,我得到了ChristopeD方法的这个修改版本

File.binread(path, 64)[0x10..0x18].unpack('NN')

这是一个很好的解决方案,但是它依赖于ImageMagick的
identify
工具来安装oo,sweet,直接跟踪字节!老派!Ruby 1.9打破了您在这里引用的JPEG类;我已经提交了一个额外的答案,并对该类进行了修改,使其能够在Ruby 1.8.7和Ruby 1.9下工作。@ChristopheD:谢谢;我刚刚进入Ruby 1.9(使用1.9.3),之前使用的是您找到的代码,所以我很喜欢弄清楚为什么代码会被破坏以及如何修复它!它使用了ImageMagick,海报要求不要使用它。至少它在2018年帮助了我,这个宝石对我来说非常好。从表面上看,它做的不多,但是内部类非常灵活并且编写得很好。只是出于好奇,
File.binread
IO.read
之间有什么区别?
require 'fastimage'

width, height = FastImage.size("http://stephensykes.com/images/ss.com_x.gif")

puts "Width: #{width}"
puts "Height: #{height}"
class JPEG
  attr_reader :width, :height, :bits
  def initialize(file)
    if file.kind_of? IO
      examine(file)
    else
      File.open(file, 'rb') { |io| examine(io) }
    end
  end
private
  def examine(io)
    if RUBY_VERSION >= "1.9"
      class << io
        def getc; super.bytes.first; end
        def readchar; super.bytes.first; end
      end
    end
    class << io
      def readint; (readchar << 8) + readchar; end
      def readframe; read(readint - 2); end
      def readsof; [readint, readchar, readint, readint, readchar]; end
      def next
        c = readchar while c != 0xFF
        c = readchar while c == 0xFF
        c
      end
    end
    raise 'malformed JPEG' unless io.getc == 0xFF && io.getc == 0xD8 # SOI
    while marker = io.next
      case marker
        when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF # SOF markers
          length, @bits, @height, @width, components = io.readsof
          raise 'malformed JPEG' unless length == 8 + components * 3
        # colons not allowed in 1.9, change to "then"
        when 0xD9, 0xDA then  break # EOI, SOS
        when 0xFE then        @comment = io.readframe # COM
        when 0xE1 then        io.readframe # APP1, contains EXIF tag
        else                  io.readframe # ignore frame
      end
    end
  end
end
File.binread(path, 64)[0x10..0x18].unpack('NN')