Class: Philiprehberger::ImageSize::ImageInfo
- Inherits:
-
Object
- Object
- Philiprehberger::ImageSize::ImageInfo
- Defined in:
- lib/philiprehberger/image_size/image_info.rb
Overview
Value object representing image dimensions and format
Instance Attribute Summary collapse
-
#alpha ⇒ Boolean?
readonly
whether the image has an alpha channel.
-
#animated ⇒ Boolean?
readonly
whether the image is animated.
-
#color_depth ⇒ Integer?
readonly
bits per pixel, nil if not detectable.
-
#dpi ⇒ Hash?
readonly
DPI as { x: Float, y: Float }, nil if not available.
-
#format ⇒ Symbol
readonly
image format (:png, :jpeg, :gif, :bmp, :webp, :tiff, :ico, :cur, :svg, :avif).
-
#height ⇒ Integer
readonly
image height in pixels.
-
#interlaced ⇒ Boolean?
readonly
whether the image uses interlaced/progressive encoding.
-
#orientation ⇒ Integer?
readonly
EXIF orientation (1-8), nil if not applicable.
-
#width ⇒ Integer
readonly
image width in pixels.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality comparison.
-
#alpha? ⇒ Boolean
Whether the image has an alpha channel.
-
#animated? ⇒ Boolean
Whether the image is animated.
-
#area ⇒ Integer
Total pixel area.
-
#aspect_ratio ⇒ Float
Calculate aspect ratio (width / height).
-
#fit_within(max_width, max_height) ⇒ Array<Integer>
Dimensions scaled to fit inside a bounding box while preserving aspect ratio.
-
#initialize(width:, height:, format:, animated: nil, alpha: nil, orientation: nil, dpi: nil, color_depth: nil, interlaced: nil) ⇒ ImageInfo
constructor
Create a new ImageInfo.
-
#inspect ⇒ String
Inspect representation.
-
#interlaced? ⇒ Boolean
Whether the image uses interlaced/progressive encoding.
-
#landscape? ⇒ Boolean
Whether the image is wider than tall.
-
#megapixels ⇒ Float
Megapixels (area / 1,000,000), rounded to 1 decimal place.
-
#portrait? ⇒ Boolean
Whether the image is taller than wide.
-
#rotated? ⇒ Boolean
Whether EXIF orientation indicates 90/270 degree rotation.
-
#square? ⇒ Boolean
Whether the image has equal width and height.
-
#to_a ⇒ Array<Integer>
Return dimensions as an array.
-
#to_h ⇒ Hash
Return image info as a hash.
-
#to_s ⇒ String
String representation.
Constructor Details
#initialize(width:, height:, format:, animated: nil, alpha: nil, orientation: nil, dpi: nil, color_depth: nil, interlaced: nil) ⇒ ImageInfo
Create a new ImageInfo
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 30 def initialize(width:, height:, format:, animated: nil, alpha: nil, orientation: nil, dpi: nil, color_depth: nil, interlaced: nil) @width = width @height = height @format = format @animated = animated @alpha = alpha @orientation = orientation @dpi = dpi @color_depth = color_depth @interlaced = interlaced end |
Instance Attribute Details
#alpha ⇒ Boolean? (readonly)
whether the image has an alpha channel
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def alpha @alpha end |
#animated ⇒ Boolean? (readonly)
whether the image is animated
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def animated @animated end |
#color_depth ⇒ Integer? (readonly)
bits per pixel, nil if not detectable
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def color_depth @color_depth end |
#dpi ⇒ Hash? (readonly)
DPI as { x: Float, y: Float }, nil if not available
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def dpi @dpi end |
#format ⇒ Symbol (readonly)
image format (:png, :jpeg, :gif, :bmp, :webp, :tiff, :ico, :cur, :svg, :avif)
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def format @format end |
#height ⇒ Integer (readonly)
image height in pixels
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def height @height end |
#interlaced ⇒ Boolean? (readonly)
whether the image uses interlaced/progressive encoding
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def interlaced @interlaced end |
#orientation ⇒ Integer? (readonly)
EXIF orientation (1-8), nil if not applicable
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def orientation @orientation end |
#width ⇒ Integer (readonly)
image width in pixels
16 17 18 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 16 def width @width end |
Instance Method Details
#==(other) ⇒ Boolean
Equality comparison
158 159 160 161 162 163 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 158 def ==(other) other.is_a?(self.class) && width == other.width && height == other.height && format == other.format end |
#alpha? ⇒ Boolean
Whether the image has an alpha channel
53 54 55 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 53 def alpha? @alpha == true end |
#animated? ⇒ Boolean
Whether the image is animated
46 47 48 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 46 def animated? @animated == true end |
#area ⇒ Integer
Total pixel area
97 98 99 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 97 def area width * height end |
#aspect_ratio ⇒ Float
Calculate aspect ratio (width / height)
67 68 69 70 71 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 67 def aspect_ratio return 0.0 if height.zero? width.to_f / height end |
#fit_within(max_width, max_height) ⇒ Array<Integer>
Dimensions scaled to fit inside a bounding box while preserving aspect ratio. Never upscales — returns the original dimensions when the image is already smaller than the box.
115 116 117 118 119 120 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 115 def fit_within(max_width, max_height) return [width, height] if width <= max_width && height <= max_height scale = [max_width.to_f / width, max_height.to_f / height].min [(width * scale).round, (height * scale).round] end |
#inspect ⇒ String
Inspect representation
175 176 177 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 175 def inspect "#<#{self.class} format=#{format} width=#{width} height=#{height}>" end |
#interlaced? ⇒ Boolean
Whether the image uses interlaced/progressive encoding
60 61 62 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 60 def interlaced? @interlaced == true end |
#landscape? ⇒ Boolean
Whether the image is wider than tall
76 77 78 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 76 def landscape? width > height end |
#megapixels ⇒ Float
Megapixels (area / 1,000,000), rounded to 1 decimal place
104 105 106 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 104 def megapixels (area / 1_000_000.0).round(1) end |
#portrait? ⇒ Boolean
Whether the image is taller than wide
83 84 85 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 83 def portrait? height > width end |
#rotated? ⇒ Boolean
Whether EXIF orientation indicates 90/270 degree rotation
125 126 127 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 125 def rotated? orientation ? orientation.between?(5, 8) : false end |
#square? ⇒ Boolean
Whether the image has equal width and height
90 91 92 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 90 def square? width == height end |
#to_a ⇒ Array<Integer>
Return dimensions as an array
132 133 134 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 132 def to_a [width, height] end |
#to_h ⇒ Hash
Return image info as a hash
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 139 def to_h { width: width, height: height, format: format, animated: animated?, alpha: alpha?, interlaced: interlaced?, orientation: orientation, dpi: dpi, color_depth: color_depth, megapixels: megapixels } end |
#to_s ⇒ String
String representation
168 169 170 |
# File 'lib/philiprehberger/image_size/image_info.rb', line 168 def to_s "#{format.upcase} #{width}x#{height}" end |