Giter Site home page Giter Site logo

image_size's Introduction

Gem Version Build Status Rubocop

image_size

Measure image size/dimensions using pure Ruby. Formats: apng, avif, bmp, cur, emf, gif, heic, heif, ico, j2c, jp2, jpeg, jpx, mng, pam, pbm, pcx, pgm, png, ppm, psd, svg, swf, tiff, webp, xbm, xpm.

Installation

gem install image_size

Bundler

Add to your Gemfile:

gem 'image_size', '~> 3.0'

Usage

image_size = ImageSize.path('spec/images/jpeg/436x429.jpeg')

image_size.format       #=> :jpeg
image_size.width        #=> 436
image_size.height       #=> 429
image_size.w            #=> 436
image_size.h            #=> 429
image_size.size         #=> [436, 429]
image_size.size.to_s    #=> "436x429"
"#{image_size.size}"    #=> "436x429"
image_size.size.width   #=> 436
image_size.size.height  #=> 429
image_size.size.w       #=> 436
image_size.size.h       #=> 429
image_size.media_type   #=> "image/jpeg"
image_size.media_types  #=> ["image/jpeg"]

Or using IO object:

image_size = File.open('spec/images/jpeg/436x429.jpeg', 'rb'){ |fh| ImageSize.new(fh) }

Any object responding to read and eof?:

require 'image_size'

image_size = ImageSize.new(ARGF)

Works with open-uri, see experimental HTTP server interface below:

require 'image_size'
require 'open-uri'

image_size = URI.parse('http://www.rubycgi.org/image/ruby_gtk_book_title.jpg').open('rb') do |fh|
  ImageSize.new(fh)
end

image_size = open('http://www.rubycgi.org/image/ruby_gtk_book_title.jpg', 'rb') do |fh|
  ImageSize.new(fh)
end

Note that starting with version 2.0.0 the object given to ImageSize will not be rewound before or after use. So rewind if needed before passing to ImageSize and/or rewind after passing to ImageSize before reading data.

require 'image_size'

File.open('spec/images/jpeg/436x429.jpeg', 'rb') do |fh|
  image_size = ImageSize.new(fh)

  fh.rewind
  data = fh.read
end

File.open('spec/images/jpeg/436x429.jpeg', 'rb') do |fh|
  data = fh.read
  fh.rewind

  image_size = ImageSize.new(fh)
end

Experimental: fetch image meta from HTTP server

If server recognises Range header, only needed chunks will be fetched even for TIFF images, otherwise required amount of data will be fetched, in most cases first few kilobytes (TIFF images is an exception).

require 'image_size/uri'

url = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg'
p ImageSize.url(url).size

This interface is as fast as dedicated gem fastimage for images with meta information in the header:

url = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg'
puts Benchmark.measure{ p FastImage.size(url) }
[9545, 6623]
  0.004176   0.001974   0.006150 (  0.282889)
puts Benchmark.measure{ p ImageSize.url(url).size }
[9545, 6623]
  0.005604   0.001406   0.007010 (  0.238629)

And considerably faster for images with meta information at the end of file:

url = "https://upload.wikimedia.org/wikipedia/commons/c/c7/Curiosity%27s_Vehicle_System_Test_Bed_%28VSTB%29_Rover_%28PIA15876%29.tif"
puts Benchmark.measure{ p FastImage.size(url) }
[7360, 4912]
  0.331284   0.247295   0.578579 (  6.027051)
puts Benchmark.measure{ p ImageSize.url(url).size }
[7360, 4912]
  0.006247   0.001045   0.007292 (  0.197631)

Licence

This code is free to use under the terms of the Ruby's licence.

Original author: Keisuke Minami [email protected].
Further development 2010-2024 Ivan Kuchin https://github.com/toy/image_size

image_size's People

Contributors

mtasaka avatar opoudjis avatar petergoldstein avatar toy avatar walf443 avatar yachi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

image_size's Issues

spec testsuite fails on IBM s390x system for pcx file format

image_size 2.1.1 sees spec testsuite failure on IBM s390x system for pcx file format. Note that s390x system is big endian:

........................
Failures:
  1) ImageSize for spec/images/pcx/70x60.pcx given as data gets format and dimensions
     Failure/Error: expect(image_size).to have_attributes(attributes)
       expected #<ImageSize:0x000002aa0b7a5dc0 @format=:pcx, @width=17665, @height=15105> to have attributes {:format => :pcx, :h => 60, :height => 60, :size => [70, 60], :w => 70, :width => 70} but had attributes {:format => :pcx, :h => 15105, :height => 15105, :size => [17665, 15105], :w => 17665, :width => 17665}
       Diff:
       @@ -1,7 +1,7 @@
        :format => :pcx,
       -:h => 60,
       -:height => 60,
       -:size => [70, 60],
       -:w => 70,
       -:width => 70,
       +:h => 15105,
       +:height => 15105,
       +:size => [17665, 15105],
       +:w => 17665,
       +:width => 17665,
     # ./spec/image_size_spec.rb:35:in `block (5 levels) in <top (required)>'
  2) ImageSize for spec/images/pcx/70x60.pcx given as IO gets format and dimensions
     Failure/Error: expect(image_size).to have_attributes(attributes)
       expected #<ImageSize:0x000002aa0b88cd10 @format=:pcx, @width=17665, @height=15105> to have attributes {:format => :pcx, :h => 60, :height => 60, :size => [70, 60], :w => 70, :width => 70} but had attributes {:format => :pcx, :h => 15105, :height => 15105, :size => [17665, 15105], :w => 17665, :width => 17665}
       Diff:
       @@ -1,7 +1,7 @@
        :format => :pcx,
       -:h => 60,
       -:height => 60,
       -:size => [70, 60],
       -:w => 70,
       -:width => 70,
       +:h => 15105,
       +:height => 15105,
       +:size => [17665, 15105],
       +:w => 17665,
       +:width => 17665,
     # ./spec/image_size_spec.rb:44:in `block (6 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:42:in `open'
     # ./spec/image_size_spec.rb:42:in `block (5 levels) in <top (required)>'
  3) ImageSize for spec/images/pcx/70x60.pcx given as StringIO gets format and dimensions
     Failure/Error: expect(image_size).to have_attributes(attributes)
       expected #<ImageSize:0x000002aa0b8cfc28 @format=:pcx, @width=17665, @height=15105> to have attributes {:format => :pcx, :h => 60, :height => 60, :size => [70, 60], :w => 70, :width => 70} but had attributes {:format => :pcx, :h => 15105, :height => 15105, :size => [17665, 15105], :w => 17665, :width => 17665}
       Diff:
       @@ -1,7 +1,7 @@
        :format => :pcx,
       -:h => 60,
       -:height => 60,
       -:size => [70, 60],
       -:w => 70,
       -:width => 70,
       +:h => 15105,
       +:height => 15105,
       +:size => [17665, 15105],
       +:w => 17665,
       +:width => 17665,
     # ./spec/image_size_spec.rb:57:in `block (5 levels) in <top (required)>'
  4) ImageSize for spec/images/pcx/70x60.pcx given as Tempfile gets format and dimensions
     Failure/Error: expect(image_size).to have_attributes(attributes)
       expected #<ImageSize:0x000002aa0b8aa6f8 @format=:pcx, @width=17665, @height=15105> to have attributes {:format => :pcx, :h => 60, :height => 60, :size => [70, 60], :w => 70, :width => 70} but had attributes {:format => :pcx, :h => 15105, :height => 15105, :size => [17665, 15105], :w => 17665, :width => 17665}
       Diff:
       @@ -1,7 +1,7 @@
        :format => :pcx,
       -:h => 60,
       -:height => 60,
       -:size => [70, 60],
       -:w => 70,
       -:width => 70,
       +:h => 15105,
       +:height => 15105,
       +:size => [17665, 15105],
       +:w => 17665,
       +:width => 17665,
     # ./spec/image_size_spec.rb:72:in `block (6 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:67:in `block (5 levels) in <top (required)>'
  5) ImageSize for spec/images/pcx/70x60.pcx using path method gets format and dimensions
     Failure/Error: expect(image_size).to have_attributes(attributes)
       expected #<ImageSize:0x000002aa0b8ed638 @format=:pcx, @width=17665, @height=15105> to have attributes {:format => :pcx, :h => 60, :height => 60, :size => [70, 60], :w => 70, :width => 70} but had attributes {:format => :pcx, :h => 15105, :height => 15105, :size => [17665, 15105], :w => 17665, :width => 17665}
       Diff:
       @@ -1,7 +1,7 @@
        :format => :pcx,
       -:h => 60,
       -:height => 60,
       -:size => [70, 60],
       -:w => 70,
       -:width => 70,
       +:h => 15105,
       +:height => 15105,
       +:size => [17665, 15105],
       +:w => 17665,
       +:width => 17665,
     # ./spec/image_size_spec.rb:84:in `block (5 levels) in <top (required)>'
Finished in 0.06602 seconds (files took 0.11639 seconds to load)
168 examples, 5 failures
Failed examples:
rspec ./spec/image_size_spec.rb[1:13:1:1] # ImageSize for spec/images/pcx/70x60.pcx given as data gets format and dimensions
rspec ./spec/image_size_spec.rb[1:13:2:1] # ImageSize for spec/images/pcx/70x60.pcx given as IO gets format and dimensions
rspec ./spec/image_size_spec.rb[1:13:3:1] # ImageSize for spec/images/pcx/70x60.pcx given as StringIO gets format and dimensions
rspec ./spec/image_size_spec.rb[1:13:4:1] # ImageSize for spec/images/pcx/70x60.pcx given as Tempfile gets format and dimensions
rspec ./spec/image_size_spec.rb[1:13:5:1] # ImageSize for spec/images/pcx/70x60.pcx using path method gets format and dimensions

Cannot calculate image size on file_content under JRuby

I tried the following with JRuby (1.7.3):

data = File.read('/path/to/img.jpg')
ImageSize.new(data)

It raises me an argument error:

ArgumentError: invalid byte sequence in UTF-8
from org/jruby/RubyRegexp.java:1520:in `=~'

This code works with MRI (1.9.3).

Additionally the code:

ImageSize.path('/path/to/img.jpg').size

works on JRuby

Please include actual license text

Now I am considering to package this and redistribute this on Fedora project, I want to clarify the license of this software. Would you include actual license text?

Especially, I want to know what "Ruby's license" says here.

  • Looks like this software was firstly released on 2010/Nov, at this time the license of Ruby was GPLv2 or "Ruby".
  • The license of Ruby changed on the version of 1.9.3 to BSDL or "Ruby": https://www.ruby-lang.org/en/news/2011/08/01/ruby-1-9-3-preview1-has-been-released/ , which was on 2011/Aug, so I am not sure under which this software is licensed.
  • Also, the term "Ruby's licence" is somewhat ambiguous. Someone interprets this as dual license, while other interprets this as "Ruby" license (as a narrow meaning): note https://spdx.org/licenses/Ruby.html seems to interpret "Ruby's license" as a narrow meaning.

So to clarify this, would you include actual license text? Thank you.

Collaboration with FastImage

I was looking for a library to grab width/height out of an image, and found this one plus another (https://github.com/sdsykes/fastimage/).

Both look great! ๐Ÿ… There is some overlap in the supported image formats.

Here are the ones FastImage support:
https://github.com/sdsykes/fastimage/blob/master/lib/fastimage.rb#L93

Would there be any benefit in collaborating across these two projects?

Obviously fine to keep the two running side by side too.

It could also be a chance to flesh out the code base slightly, e.g. break out each file format into a separate ruby file and and break out utils methods into dedicated files.

cc @SamSaffron

Nil error in detect_format

I am running image_optim, but keep getting this error: (it does work with image_optim v0.31.0, so I think the image_size update caused it)

> image_optim --verbose -r .
image_optim v0.31.1
config:
  svgo: false
  skip_missing_workers: true
  verbose: true
nice: 10
threads: 8
pack: true
skip_missing_workers: true
allow_lossy: false
cache_dir: 
cache_worker_digests: false
timeout: 
image_optim_pack: all bins from /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64 worked
PATH: /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64:/Users/me/.rbenv/versions/3.1.2/bin:/opt/homebrew/Cellar/rbenv/1.2.0/libexec:/Users/me/.asdf/shims:/Users/me/.asdf/bin:/Users/me/.rbenv/shims:/opt/homebrew/opt/mysql@5.7/bin:/Users/me/Library/Python/2.7/bin:/Users/me/.yarn/bin:/Users/me/.config/yarn/global/node_modules/.bin:/Users/me/.cargo/bin:.git/safe/../../bin:/Users/me/.bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/MacGPG2/bin:/Library/Apple/usr/bin:/Users/me/code/webdoctor/node_modules/.bin:/opt/homebrew/opt/fzf/bin:/Users/me/.antigen/bundles/robbyrussell/oh-my-zsh/lib:/Users/me/.antigen/bundles/Aloxaf/fzf-tab:/Users/me/.antigen/bundles/Tarrasch/zsh-bd:/Users/me/.antigen/bundles/supercrabtree/k:/Users/me/.antigen/bundles/zsh-users/zsh-autosuggestions:/Users/me/.antigen/bundles/tymm/zsh-directory-history:/Users/me/.antigen/bundles/smallhadroncollider/antigen-git-rebase:/Users/me/.antigen/bundles/romkatv/powerlevel10k:/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/vendor
Resolved pngcrush 1.8.13 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/pngcrush
Resolved pngout 20200115 at /opt/homebrew/bin/pngout
Resolved advpng 2.3 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/advpng
Resolved optipng 0.7.7 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/optipng
Resolved pngquant 2.17.0 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/pngquant
Resolved oxipng 5.0.1 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/oxipng
Resolved jhead 3.04 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/jhead
Resolved jpegtran 9e at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/jpegtran
Resolved jpegoptim 1.4.7 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/jpegoptim
Resolved jpegrescan 1a762f62 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/vendor/jpegrescan
Resolved gifsicle 1.93 at /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim_pack-0.8.0.20220418/vendor/darwin-x86_64/gifsicle
Workers by format:
jpeg:
  jhead:
  jpegoptim:
    allow_lossy: false
    strip: ["all"]
    max_quality: 100
  jpegtran:
    copy_chunks: false
    progressive: true
    jpegrescan: true
png:
  pngcrush:
    chunks: ["alla"]
    fix: false
    brute: false
    blacken: true
  optipng:
    level: 6
    interlace: false
    strip: true
  oxipng:
    level: 3
    interlace: false
    strip: true
  pngquant:
    allow_lossy: false
    max_colors: 256
    quality: 100..100
    speed: 3
  pngout:
    copy_chunks: false
    strategy: 0
  advpng:
    level: 4
gif:
  gifsicle:
    interlace: false
    level: 3
    careful: false
  gifsicle:
    interlace: true
    level: 3
    careful: false
undefined method `[]' for nil:NilClass

    when head[0, 6] =~ /GIF8[79]a/                                then :gif
             ^^^^^^
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size.rb:79:in `detect_format'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size.rb:51:in `block in initialize'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size/reader.rb:18:in `block in open'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size/reader.rb:18:in `open'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size/reader.rb:18:in `open'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size/reader.rb:18:in `open'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size.rb:50:in `initialize'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size.rb:35:in `new'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_size-3.0.1/lib/image_size.rb:35:in `path'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/image_meta.rb:9:in `format_for_path'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/path.rb:68:in `image_format'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim.rb:109:in `workers_for_image'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim.rb:224:in `optimizable?'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/runner.rb:108:in `block in find_to_optimize_recursive'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/3.1.0/find.rb:49:in `block (2 levels) in find'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/3.1.0/find.rb:48:in `catch'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/3.1.0/find.rb:48:in `block in find'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/3.1.0/find.rb:43:in `each'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/3.1.0/find.rb:43:in `find'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/runner.rb:105:in `find_to_optimize_recursive'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/runner.rb:92:in `block in find_to_optimize'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/runner.rb:83:in `each'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/runner.rb:83:in `find_to_optimize'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/lib/image_optim/runner.rb:60:in `run!'
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/image_optim-0.31.1/bin/image_optim:21:in `<top (required)>'
/Users/me/.rbenv/versions/3.1.2/bin/image_optim:25:in `load'
/Users/me/.rbenv/versions/3.1.2/bin/image_optim:25:in `<main>'

My gemfile & lock:

# gemfile
source 'https://rubygems.org'

gem 'overcommit'
gem 'brakeman'
gem 'bundler'
gem 'haml-lint'
gem 'image_optim'
gem 'json'
gem 'mdl'
gem 'pronto'
gem 'pronto-haml'
gem 'pronto-rails_best_practices'
gem 'rubocop', require: false
gem 'rubocop-minitest', require: false
gem 'rubocop-performance', require: false
gem 'rubocop-rails', require: false
gem 'rubocop-rspec', require: false

# lock
GEM
  remote: https://rubygems.org/
  specs:
    activesupport (7.0.2.3)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
    addressable (2.8.0)
      public_suffix (>= 2.0.2, < 5.0)
    ast (2.4.2)
    brakeman (5.2.2)
    chef-utils (17.10.0)
      concurrent-ruby
    childprocess (4.1.0)
    code_analyzer (0.5.5)
      sexp_processor
    concurrent-ruby (1.1.10)
    erubis (2.7.0)
    exifr (1.3.9)
    faraday (1.10.0)
      faraday-em_http (~> 1.0)
      faraday-em_synchrony (~> 1.0)
      faraday-excon (~> 1.1)
      faraday-httpclient (~> 1.0)
      faraday-multipart (~> 1.0)
      faraday-net_http (~> 1.0)
      faraday-net_http_persistent (~> 1.0)
      faraday-patron (~> 1.0)
      faraday-rack (~> 1.0)
      faraday-retry (~> 1.0)
      ruby2_keywords (>= 0.0.4)
    faraday-em_http (1.0.0)
    faraday-em_synchrony (1.0.0)
    faraday-excon (1.1.0)
    faraday-httpclient (1.0.1)
    faraday-multipart (1.0.3)
      multipart-post (>= 1.2, < 3)
    faraday-net_http (1.0.1)
    faraday-net_http_persistent (1.2.0)
    faraday-patron (1.0.0)
    faraday-rack (1.0.0)
    faraday-retry (1.0.3)
    fspath (3.1.2)
    gitlab (4.18.0)
      httparty (~> 0.18)
      terminal-table (>= 1.5.1)
    haml (5.2.2)
      temple (>= 0.8.0)
      tilt
    haml-lint (0.999.999)
      haml_lint
    haml_lint (0.40.0)
      haml (>= 4.0, < 5.3)
      parallel (~> 1.10)
      rainbow
      rubocop (>= 0.50.0)
      sysexits (~> 1.1)
    httparty (0.20.0)
      mime-types (~> 3.0)
      multi_xml (>= 0.5.2)
    i18n (1.10.0)
      concurrent-ruby (~> 1.0)
    image_optim (0.31.1)
      exifr (~> 1.2, >= 1.2.2)
      fspath (~> 3.0)
      image_size (>= 1.5, < 4)
      in_threads (~> 1.3)
      progress (~> 3.0, >= 3.0.1)
    image_size (3.0.1)
    in_threads (1.6.0)
    iniparse (1.5.0)
    json (2.6.1)
    kramdown (2.4.0)
      rexml
    kramdown-parser-gfm (1.1.0)
      kramdown (~> 2.0)
    mdl (0.11.0)
      kramdown (~> 2.3)
      kramdown-parser-gfm (~> 1.1)
      mixlib-cli (~> 2.1, >= 2.1.1)
      mixlib-config (>= 2.2.1, < 4)
      mixlib-shellout
    mime-types (3.4.1)
      mime-types-data (~> 3.2015)
    mime-types-data (3.2022.0105)
    minitest (5.15.0)
    mixlib-cli (2.1.8)
    mixlib-config (3.0.9)
      tomlrb
    mixlib-shellout (3.2.7)
      chef-utils
    multi_xml (0.6.0)
    multipart-post (2.1.1)
    octokit (4.22.0)
      faraday (>= 0.9)
      sawyer (~> 0.8.0, >= 0.5.3)
    overcommit (0.58.0)
      childprocess (>= 0.6.3, < 5)
      iniparse (~> 1.4)
      rexml (~> 3.2)
    parallel (1.22.1)
    parser (3.1.2.0)
      ast (~> 2.4.1)
    progress (3.6.0)
    pronto (0.11.0)
      gitlab (~> 4.4, >= 4.4.0)
      httparty (>= 0.13.7)
      octokit (~> 4.7, >= 4.7.0)
      rainbow (>= 2.2, < 4.0)
      rexml (~> 3.2)
      rugged (>= 0.23.0, < 1.1.0)
      thor (>= 0.20.3, < 2.0)
    pronto-haml (0.11.1)
      haml_lint (~> 0.23)
      pronto (~> 0.11.0)
    pronto-rails_best_practices (0.11.0)
      pronto (~> 0.11.0)
      rails_best_practices (~> 1.16, >= 1.15.0)
    public_suffix (4.0.7)
    rack (2.2.3)
    rails_best_practices (1.23.1)
      activesupport
      code_analyzer (~> 0.5.5)
      erubis
      i18n
      json
      require_all (~> 3.0)
      ruby-progressbar
    rainbow (3.1.1)
    regexp_parser (2.3.1)
    require_all (3.0.0)
    rexml (3.2.5)
    rubocop (1.28.2)
      parallel (~> 1.10)
      parser (>= 3.1.0.0)
      rainbow (>= 2.2.2, < 4.0)
      regexp_parser (>= 1.8, < 3.0)
      rexml
      rubocop-ast (>= 1.17.0, < 2.0)
      ruby-progressbar (~> 1.7)
      unicode-display_width (>= 1.4.0, < 3.0)
    rubocop-ast (1.17.0)
      parser (>= 3.1.1.0)
    rubocop-minitest (0.19.1)
      rubocop (>= 0.90, < 2.0)
    rubocop-performance (1.13.3)
      rubocop (>= 1.7.0, < 2.0)
      rubocop-ast (>= 0.4.0)
    rubocop-rails (2.14.2)
      activesupport (>= 4.2.0)
      rack (>= 1.1)
      rubocop (>= 1.7.0, < 2.0)
    rubocop-rspec (2.10.0)
      rubocop (~> 1.19)
    ruby-progressbar (1.11.0)
    ruby2_keywords (0.0.5)
    rugged (1.0.1)
    sawyer (0.8.2)
      addressable (>= 2.3.5)
      faraday (> 0.8, < 2.0)
    sexp_processor (4.16.1)
    sysexits (1.2.0)
    temple (0.8.2)
    terminal-table (3.0.2)
      unicode-display_width (>= 1.1.1, < 3)
    thor (1.2.1)
    tilt (2.0.10)
    tomlrb (2.0.1)
    tzinfo (2.0.4)
      concurrent-ruby (~> 1.0)
    unicode-display_width (2.1.0)

PLATFORMS
  arm64-darwin-21

DEPENDENCIES
  brakeman
  bundler
  haml-lint
  image_optim
  json
  mdl
  overcommit
  pronto
  pronto-haml
  pronto-rails_best_practices
  rubocop
  rubocop-minitest
  rubocop-performance
  rubocop-rails
  rubocop-rspec

BUNDLED WITH
   2.3.12

JPEG2000 support

Hi, it seems like JPEG2000 isn't supported:

ImageSize.path("test-image.jp2").size
=> nil

Correct licence

Hi,

I just wanted to mention that the README file mentions that the this code is free to use under the terms of the Ruby's licence but the gemspec file sets the license to MIT.

MIT is the correct licence, right?

Not working in ruby 2.3.0

Can you please help out. Imagesize is not woking with ruby 2.3.0
[1] pry(main)> open("#{thumb_nail['url']}", 'rb') do |fh|
[1] pry(main)* data = fh.read
[1] pry(main)* p ImageSize.new(data).size
[1] pry(main)* end
[nil, nil]
=> [nil, nil]
But in ruby 1.8 the output is as follows for the same image:
[7] pry(main)> open("#{thumb_nail['url']}", 'rb') do |fh|
[7] pry(main)* data = fh.read
[7] pry(main)* p ImageSize.new(data).size
[7] pry(main)* end
[1280, 720]
=> nil

PSD size is inversed

For PSD (Photoshop) files, the result returned by "image_size" is inversed: width is height, and height is width.

undefined method `unpack' for nil:NilClass while fetching TIFF image size from partial data

When trying to get TIFF image size from chunks of data, I end-up with the following exception:

Traceback (most recent call last):
        5: from bin/console:14:in `<main>'
        4: from (irb):17
        3: from (irb):17:in `new'
        2: from /Users/gottfrois/.rvm/gems/ruby-2.6.6/gems/image_size-2.0.2/lib/image_size.rb:65:in `initialize'
        1: from /Users/gottfrois/.rvm/gems/ruby-2.6.6/gems/image_size-2.0.2/lib/image_size.rb:227:in `size_of_tiff'
NoMethodError (undefined method `unpack' for nil:NilClass)

Steps to reproduce:

2.6.6 :001 > require 'open-uri'
 => true
2.6.6 :002 > fd = URI.parse('https://effigis.com/wp-content/themes/effigis_2014/img/Airbus-Spot6-50cm-St-Benoit-du-Lac-Quebec-2014-09-04.tif').open('rb')
 => #<Tempfile:/var/folders/_9/fzxprxqn2_10xdbghn1fb7q40000gn/T/open-uri20200724-10967-n9efp3>
2.6.6 :003 > ImageSize.new(fd.read(100))

The idea is to get the image size without reading the full image for performance reasons. But reading about the TIFF file header format, I wonder if it's even possible? As far as I understand, it's supposed to give you the offset where the image starts, which might be a large number which points to a nil value since all the data is not loaded...

Nevertheless, it might be a good idea to raise a ImageSize::FormatError in this case instead of raising an undefined method error. What do you think?

Can't work with ruby2.0.0-p0

I try to run test with MRI 2.0.0-p0. But test fails.
I'm researching for fix this problem. but just report it.

It seems that failing tests are jpg or png case.

$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.0]
spec -Ilib spec/image_size_spec.rb
............FFFFFF..................FFFFFF.....................................

Failures:

  1) ImageSize should get format and dimensions for test.jpg given IO
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:jpeg, 320, 240]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:26:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:24:in `open'
     # ./spec/image_size_spec.rb:24:in `block (3 levels) in <top (required)>'

  2) ImageSize should get format and dimensions for test.jpg given StringIO
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:jpeg, 320, 240]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:33:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:31:in `open'
     # ./spec/image_size_spec.rb:31:in `block (3 levels) in <top (required)>'

  3) ImageSize should get format and dimensions for test.jpg given file data
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:jpeg, 320, 240]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:40:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:38:in `open'
     # ./spec/image_size_spec.rb:38:in `block (3 levels) in <top (required)>'
  4) ImageSize should get format and dimensions for test.jpg given Tempfile
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:jpeg, 320, 240]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:50:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:46:in `block (3 levels) in <top (required)>'

  5) ImageSize should get format and dimensions for test.jpg given IO when run twice
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:jpeg, 320, 240]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:57:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:55:in `open'
     # ./spec/image_size_spec.rb:55:in `block (3 levels) in <top (required)>'

  6) ImageSize should get format and dimensions for test.jpg as path
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:jpeg, 320, 240]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:65:in `block (3 levels) in <top (required)>'

  7) ImageSize should get format and dimensions for test.png given IO
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:png, 640, 532]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:26:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:24:in `open'
     # ./spec/image_size_spec.rb:24:in `block (3 levels) in <top (required)>'

  8) ImageSize should get format and dimensions for test.png given StringIO
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:png, 640, 532]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:33:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:31:in `open'
     # ./spec/image_size_spec.rb:31:in `block (3 levels) in <top (required)>'

  9) ImageSize should get format and dimensions for test.png given file data
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:png, 640, 532]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:40:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:38:in `open'
     # ./spec/image_size_spec.rb:38:in `block (3 levels) in <top (required)>'

  10) ImageSize should get format and dimensions for test.png given Tempfile
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:png, 640, 532]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:50:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:46:in `block (3 levels) in <top (required)>'

  11) ImageSize should get format and dimensions for test.png given IO when run twice
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:png, 640, 532]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:57:in `block (4 levels) in <top (required)>'
     # ./spec/image_size_spec.rb:55:in `open'
     # ./spec/image_size_spec.rb:55:in `block (3 levels) in <top (required)>'

  12) ImageSize should get format and dimensions for test.png as path
     Failure/Error: [is.format, is.width, is.height].should == [format, width, height]
       expected: [:png, 640, 532]
            got: [nil, nil, nil] (using ==)
     # ./spec/image_size_spec.rb:65:in `block (3 levels) in <top (required)>'

Finished in 0.03107 seconds
79 examples, 12 failures

Failed examples:

rspec ./spec/image_size_spec.rb:23 # ImageSize should get format and dimensions for test.jpg given IO
rspec ./spec/image_size_spec.rb:30 # ImageSize should get format and dimensions for test.jpg given StringIO
rspec ./spec/image_size_spec.rb:37 # ImageSize should get format and dimensions for test.jpg given file data
rspec ./spec/image_size_spec.rb:44 # ImageSize should get format and dimensions for test.jpg given Tempfile
rspec ./spec/image_size_spec.rb:54 # ImageSize should get format and dimensions for test.jpg given IO when run twice
rspec ./spec/image_size_spec.rb:63 # ImageSize should get format and dimensions for test.jpg as path
rspec ./spec/image_size_spec.rb:23 # ImageSize should get format and dimensions for test.png given IO
rspec ./spec/image_size_spec.rb:30 # ImageSize should get format and dimensions for test.png given StringIO
rspec ./spec/image_size_spec.rb:37 # ImageSize should get format and dimensions for test.png given file data
rspec ./spec/image_size_spec.rb:44 # ImageSize should get format and dimensions for test.png given Tempfile
rspec ./spec/image_size_spec.rb:54 # ImageSize should get format and dimensions for test.png given IO when run twice
rspec ./spec/image_size_spec.rb:63 # ImageSize should get format and dimensions for test.png as path

Severe: lib/image_size.rb has (root only) rwx------ rights LoadError

Now with version 1.0.2 it is NOT possible to use image_size straight of the box on unix stations/server after: gem install image_size because we get error:

`gem_original_require': no such file to load -- image_size (LoadError)

This is causes that lib/image_size.rb has right 700 (rwx------) which is way wrong, owner (root) only access with executable flag.

Please change rights of lib/image_size.rb to 644 (rw-r--r--) so it has owner (root) read-write, and read for the others flags. Executable flags shouldn't be there because it is module not standalone script.

Please issue new 1.0.3 fixed version of this nice gem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.