有没有一种简单的方法来获取Ruby中的图像尺寸?

时间:2010-03-15 22:28:01

标签: ruby image osx-snow-leopard dimensions

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

8 个答案:

答案 0 :(得分:45)

截至2012年6月,FastImage“通过尽可能少地获取图片来查找图像的大小或类型”是一个不错的选择。它适用于本地映像和远程服务器上的映像。

自述文件中的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}"

答案 1 :(得分:31)

您可以尝试这些(未经测试):

http://snippets.dzone.com/posts/show/805

PNG:

IO.read('image.png')[0x10..0x18].unpack('NN')
=> [713, 54]

GIF:

IO.read('image.gif')[6..10].unpack('SS')
=> [130, 50]

BMP:

d = IO.read('image.bmp')[14..28]
d[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS')

JPG:

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)
    raise 'malformed JPEG' unless io.getc == 0xFF && io.getc == 0xD8 # SOI

    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

    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
        when 0xD9, 0xDA:  break # EOI, SOS
        when 0xFE:        @comment = io.readframe # COM
        when 0xE1:        io.readframe # APP1, contains EXIF tag
        else              io.readframe # ignore frame
      end
    end
  end
end

答案 2 :(得分:30)

还有一个新的(2011年7月)图书馆,当时问题最初还没有出现:Dimensions ruby​​gem(似乎是由负责字节操作的Sam Stephenson编写的)技术也在这里提出。)

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

require 'dimensions'

Dimensions.dimensions("upload_bird.jpg")  # => [300, 225]
Dimensions.width("upload_bird.jpg")       # => 300
Dimensions.height("upload_bird.jpg")      # => 225

答案 3 :(得分:14)

回形针宝石中有一个方便的方法:

>> 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)

答案 4 :(得分:8)

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

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]

答案 5 :(得分:4)

libimage-size是一个Ruby库,用于计算各种图形格式的图像大小。可以使用gem,也可以下载源tarball并提取image_size.rb文件。

答案 6 :(得分:3)

以下是ChristopheD回答的JPEG类的一个版本,它适用于Ruby 1.8.7和Ruby 1.9。这允许您通过直接查看位来获取JPEG(.jpg)图像文件的宽度和高度。 (或者,只需使用Dimensions gem,如另一个答案所示。)

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

答案 7 :(得分:0)

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

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