ImageMagick または ImageScience (Snow Leapard を実行) を使用せずに、Ruby で画像ファイルの幅と高さの寸法を取得する簡単な方法を探しています。
8 に答える
2012 年 6 月の時点で、「必要なだけフェッチすることで、指定された uri からイメージのサイズまたはタイプを見つける」FastImageが適切なオプションです。ローカル イメージとリモート サーバー上のイメージで動作します。
readme からの 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]
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
質問が最初に尋ねられた時点では存在しなかった新しい (2011 年 7 月) ライブラリもあります。
プロジェクトのREADMEからのコードサンプルの下
require 'dimensions'
Dimensions.dimensions("upload_bird.jpg") # => [300, 225]
Dimensions.width("upload_bird.jpg") # => 300
Dimensions.height("upload_bird.jpg") # => 225
paperclip 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)
私は最終的に、画像の寸法を取得するための素早い方法を見つけました。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
ファイルを抽出することができます。
これは、Ruby 1.8.7 と Ruby 1.9 の両方で動作する ChristopheD's answer の JPEG クラスのバージョンです。これにより、ビットを直接見ることで、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