philiprehberger-image_size
Image dimension detection from file headers without full decode.
Requirements
- Ruby >= 3.1
Installation
Add to your Gemfile:
gem "philiprehberger-image_size"Or install directly:
gem install philiprehberger-image_sizeUsage
Basic Detection
require "philiprehberger/image_size"
info = Philiprehberger::ImageSize.of("photo.png")
info.width # => 1920
info.height # => 1080
info.format # => :pngDimensions Only
width, height = Philiprehberger::ImageSize.dimensions("banner.jpg")Format Detection
format = Philiprehberger::ImageSize.format("image.webp")
# => :webpIO Objects
File.open("photo.gif", "rb") do |f|
info = Philiprehberger::ImageSize.of(f)
puts info.to_s # => "GIF 320x240"
endAnimation Detection
info = Philiprehberger::ImageSize.of("animation.gif")
info.animated? # => trueAlpha Channel Detection
info = Philiprehberger::ImageSize.of("transparent.png")
info.alpha? # => trueEXIF Orientation
info = Philiprehberger::ImageSize.of("rotated.jpg")
info.orientation # => 6
# Width and height reflect actual display dimensions (swapped for 90/270 rotation)ImageInfo Value Object
info = Philiprehberger::ImageSize.of("photo.bmp")
info.to_a # => [640, 480]
info.to_h # => { width: 640, height: 480, format: :bmp, animated: false, alpha: false, orientation: nil }API
Philiprehberger::ImageSize
| Method | Description |
|---|---|
.of(path_or_io) |
Returns ImageInfo with width, height, format, and metadata |
.dimensions(path_or_io) |
Returns [width, height] array |
.format(path_or_io) |
Returns format symbol (:png, :jpeg, :gif, :bmp, :webp, :tiff, :ico, :cur, :svg, :avif) |
Philiprehberger::ImageSize::ImageInfo
| Method | Description |
|---|---|
#width |
Image width in pixels (display dimensions for rotated JPEG) |
#height |
Image height in pixels (display dimensions for rotated JPEG) |
#format |
Format symbol |
#animated? |
Whether the image is animated (GIF, WebP, APNG) |
#alpha? |
Whether the image has an alpha channel |
#orientation |
EXIF orientation (1-8), nil if not applicable |
#to_a |
Returns [width, height]
|
#to_h |
Returns hash with all attributes |
#to_s |
Returns "FORMAT WxH" string |
Development
bundle install
bundle exec rspec
bundle exec rubocopSupport
If you find this project useful: