我正在寻找一种简单的方法来获取 Ruby 中图像文件的宽度和高度尺寸,而无需使用 ImageMagick 或 ImageScience(运行 Snow Leapard)。
8 回答
截至 2012 年 6 月, “通过尽可能少地获取所需的 uri 来查找图像的大小或类型”的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}"
你可以试试这些(未经测试):
http://snippets.dzone.com/posts/show/805
PNG:
IO.read('image.png')[0x10..0x18].unpack('NN')
=> [713, 54]
动图:
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
还有一个新的(2011 年 7 月)库,在最初提出这个问题的时候并不存在:Dimensions rubygem(它似乎是由负责字节操作技术的 Sam Stephenson 创作的,也在这里提出了建议。)
下面来自项目自述文件的代码示例
require 'dimensions'
Dimensions.dimensions("upload_bird.jpg") # => [300, 225]
Dimensions.width("upload_bird.jpg") # => 300
Dimensions.height("upload_bird.jpg") # => 225
回形针宝石中有一个方便的方法:
>> 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)
我终于找到了一种快速获取图像尺寸的好方法。你应该使用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]
libimage-size是一个 Ruby 库,用于计算各种图形格式的图像大小。可以使用 gem,或者您可以下载源 tarball 并解压缩image_size.rb
文件。
这是 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
对于 PNG,我得到了 ChristopeD 方法的修改版本。
File.binread(path, 64)[0x10..0x18].unpack('NN')