Class: Philiprehberger::ImageSize::ImageInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/image_size/image_info.rb

Overview

Value object representing image dimensions and format

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, format:, animated: nil, alpha: nil, orientation: nil, dpi: nil, color_depth: nil, interlaced: nil) ⇒ ImageInfo

Create a new ImageInfo

Parameters:

  • width (Integer)

    image width in pixels

  • height (Integer)

    image height in pixels

  • format (Symbol)

    image format

  • animated (Boolean, nil) (defaults to: nil)

    whether the image is animated

  • alpha (Boolean, nil) (defaults to: nil)

    whether the image has an alpha channel

  • orientation (Integer, nil) (defaults to: nil)

    EXIF orientation (1-8)

  • dpi (Hash, nil) (defaults to: nil)

    DPI as { x: Float, y: Float }

  • color_depth (Integer, nil) (defaults to: nil)

    bits per pixel

  • interlaced (Boolean, nil) (defaults to: nil)

    whether the image uses interlaced/progressive encoding



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

#alphaBoolean? (readonly)

whether the image has an alpha channel

Returns:

  • (Boolean, nil)

    the current value of alpha



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def alpha
  @alpha
end

#animatedBoolean? (readonly)

whether the image is animated

Returns:

  • (Boolean, nil)

    the current value of animated



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def animated
  @animated
end

#color_depthInteger? (readonly)

bits per pixel, nil if not detectable

Returns:

  • (Integer, nil)

    the current value of color_depth



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def color_depth
  @color_depth
end

#dpiHash? (readonly)

DPI as { x: Float, y: Float }, nil if not available

Returns:

  • (Hash, nil)

    the current value of dpi



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def dpi
  @dpi
end

#formatSymbol (readonly)

image format (:png, :jpeg, :gif, :bmp, :webp, :tiff, :ico, :cur, :svg, :avif)

Returns:

  • (Symbol)

    the current value of format



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def format
  @format
end

#heightInteger (readonly)

image height in pixels

Returns:

  • (Integer)

    the current value of height



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def height
  @height
end

#interlacedBoolean? (readonly)

whether the image uses interlaced/progressive encoding

Returns:

  • (Boolean, nil)

    the current value of interlaced



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def interlaced
  @interlaced
end

#orientationInteger? (readonly)

EXIF orientation (1-8), nil if not applicable

Returns:

  • (Integer, nil)

    the current value of orientation



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def orientation
  @orientation
end

#widthInteger (readonly)

image width in pixels

Returns:

  • (Integer)

    the current value of width



16
17
18
# File 'lib/philiprehberger/image_size/image_info.rb', line 16

def width
  @width
end

Instance Method Details

#==(other) ⇒ Boolean

Equality comparison

Parameters:

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


46
47
48
# File 'lib/philiprehberger/image_size/image_info.rb', line 46

def animated?
  @animated == true
end

#areaInteger

Total pixel area

Returns:

  • (Integer)


97
98
99
# File 'lib/philiprehberger/image_size/image_info.rb', line 97

def area
  width * height
end

#aspect_ratioFloat

Calculate aspect ratio (width / height)

Returns:

  • (Float)


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.

Parameters:

  • max_width (Integer)

    maximum width of the bounding box in pixels

  • max_height (Integer)

    maximum height of the bounding box in pixels

Returns:

  • (Array<Integer>)
    width, height

    scaled to fit



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

#inspectString

Inspect representation

Returns:

  • (String)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


76
77
78
# File 'lib/philiprehberger/image_size/image_info.rb', line 76

def landscape?
  width > height
end

#megapixelsFloat

Megapixels (area / 1,000,000), rounded to 1 decimal place

Returns:

  • (Float)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


90
91
92
# File 'lib/philiprehberger/image_size/image_info.rb', line 90

def square?
  width == height
end

#to_aArray<Integer>

Return dimensions as an array

Returns:

  • (Array<Integer>)
    width, height


132
133
134
# File 'lib/philiprehberger/image_size/image_info.rb', line 132

def to_a
  [width, height]
end

#to_hHash

Return image info as a hash

Returns:

  • (Hash)

    { width:, height:, format:, animated:, alpha:, orientation:, dpi:, color_depth:, megapixels: }



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_sString

String representation

Returns:

  • (String)


168
169
170
# File 'lib/philiprehberger/image_size/image_info.rb', line 168

def to_s
  "#{format.upcase} #{width}x#{height}"
end