The project is in a healthy, maintained state
Reads width, height, and format from image file headers (PNG, JPEG, GIF, BMP, WebP) without loading the full image. Zero dependencies, fast, and memory-efficient.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-image_size

Tests Gem Version Last updated

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_size

Usage

Basic Detection

require "philiprehberger/image_size"

info = Philiprehberger::ImageSize.of("photo.png")
info.width   # => 1920
info.height  # => 1080
info.format  # => :png

Dimensions Only

width, height = Philiprehberger::ImageSize.dimensions("banner.jpg")

Format Detection

format = Philiprehberger::ImageSize.format("image.webp")
# => :webp

IO Objects

File.open("photo.gif", "rb") do |f|
  info = Philiprehberger::ImageSize.of(f)
  puts info.to_s  # => "GIF 320x240"
end

Animation Detection

info = Philiprehberger::ImageSize.of("animation.gif")
info.animated?  # => true

Alpha Channel Detection

info = Philiprehberger::ImageSize.of("transparent.png")
info.alpha?  # => true

EXIF 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 rubocop

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT