gru*_*ner 32 ruby image dimensions osx-snow-leopard
我正在寻找一种简单的方法来获取Ruby中图像文件的宽度和高度尺寸,而无需使用ImageMagick或ImageScience(运行Snow Leapard).
Ala*_*ith 46
截至2012年6月,FastImage "通过尽可能少地获取图像来查找图像的大小或类型"是一个不错的选择.它适用于本地映像和远程服务器上的映像.
自述文件中的IRB示例:
require 'fastimage'
FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
=> [266, 56] # width, height
Run Code Online (Sandbox Code Playgroud)
脚本中的标准数组赋值:
require 'fastimage'
size_array = FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
puts "Width: #{size_array[0]}"
puts "Height: #{size_array[1]}"
Run Code Online (Sandbox Code Playgroud)
或者,在脚本中使用多个赋值:
require 'fastimage'
width, height = FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
puts "Width: #{width}"
puts "Height: #{height}"
Run Code Online (Sandbox Code Playgroud)
Chr*_*heD 31
你可以尝试这些(未经测试):
http://snippets.dzone.com/posts/show/805
PNG:
IO.read('image.png')[0x10..0x18].unpack('NN')
=> [713, 54]
Run Code Online (Sandbox Code Playgroud)
GIF:
IO.read('image.gif')[6..10].unpack('SS')
=> [130, 50]
Run Code Online (Sandbox Code Playgroud)
BMP:
d = IO.read('image.bmp')[14..28]
d[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS')
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
hop*_*per 30
还有一个新的(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
Run Code Online (Sandbox Code Playgroud)
Zub*_*bin 14
paperclip gem中有一个方便的方法:
>> Paperclip::Geometry.from_file("/path/to/image.jpg")
=> 180x180
Run Code Online (Sandbox Code Playgroud)
这仅在identify安装时有效.如果不是,如果安装了PHP,你可以这样做:
system(%{php -r '$w = getimagesize("#{path}"); echo("${w[0]}x${w[1]}");'})
# eg returns "200x100" (width x height)
Run Code Online (Sandbox Code Playgroud)
我终于找到了一种快速获取图像尺寸的方法.你应该使用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]
Run Code Online (Sandbox Code Playgroud)