2010-03-15 26 views

cevap

2

libimage-size grafik biçimlerinde geniş bir çeşitliliği için resim boyutlarını hesaplamak için bir Ruby kütüphanedir. Bir mücevher mevcut veya kaynak tarball'ı indirebilir ve image_size.rb dosyasını ayıklayabilirsiniz.

+1

Bunun ruby ​​ile çalıştığını sanmıyorum 1.9 – Jonathan

26

Bu (denenmemiş) deneyebilirsiniz:

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 
+3

Ooo, tatlı, doğrudan bytes'ten sonra gidin! Eski okul! –

+0

Ruby 1.9, burada belirttiğiniz JPEG sınıfını kırdı; Hem Ruby 1.8.7 hem de Ruby 1.9 altında çalışmak için bu sınıftaki değişiklikimi ekledim. – matt

+0

@matt: Yorum yazdığın için teşekkür et, teşekkürlerimi aldın! – ChristopheD

14

ataç taş kullanışlı bir yöntem var:

>> Paperclip::Geometry.from_file("/path/to/image.jpg") 
=> 180x180 

Bu, yalnızca identify yüklüyse. Değilse PHP yüklüyse, böyle bir şey yapabileceğini:

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

Bu harika bir çözümdür, ancak ImageMagick'in 'define' aracının yüklenmesine dayanır –

+0

İyi bir nokta, alternatif yöntemle güncellendi. – Zubin

7

Sonunda bir resmin boyutlarını almak için güzel bir hızlı yol bulduk. MiniMagick'u kullanmalısınız. bayt manipülasyon sorumlu Aynı Sam Stephenson tarafından kaleme gibi görünüyor Dimensions rubygem (:

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

Posterin kullanmasını istemediği ImageMagick'i kullanır. – jcalvert

+0

En azından 2018'de bana yardımcı oldu <3 –

26

soru başlangıçta istendi anda etrafında değildi bir (Temmuz 2011) yeni kütüphane de bulunmaktadır tekniklerinin burada önerdi.) kod örneği Aşağıda

projenin README Haziran 2012 itibarıyla

require 'dimensions' 

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

Bu mücevher benim için çok iyi çalıştı. Yüzeyde fazla bir şey yapmaz ama iç sınıflar oldukça esnek ve iyi yazılmış. – bloudermilk

41

, "gerekli olduğu kadar az getirecek şekilde uri verilen bir resmin boyutunu veya türünü bulur" FastImage daniyi bir seçenek. Yerel görüntüler ve uzak sunuculardakilerle çalışır.

Benioku'da gelen bir IRB örnek:

require 'fastimage' 

FastImage.size("http://stephensykes.com/images/ss.com_x.gif") 
=> [266, 56] # width, height 

bir komut standart dizi tahsisi: Bir komut birden atama kullanarak

require 'fastimage' 

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

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

Veya: Burada

require 'fastimage' 

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

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

Bence bu iyi bir çözümdür – seapy

+1

Bu iyi çalışıyor. – Matt

3

bir bulunuyor ChristopheD'nin hem Ruby 1.8.7 hem de Ruby 1.9'da çalışan yanıtından alınan JPEG sınıfının sürümü. Bu, doğrudan bitlere bakarak bir JPEG (.jpg) görüntü dosyasının genişliğini ve yüksekliğini elde etmenizi sağlar. (Alternatif olarak, başka bir cevapta önerildiği gibi Dimensions gem'i kullanın.)

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 
İlgili konular