Module: Philiprehberger::ImageSize

Defined in:
lib/philiprehberger/image_size.rb,
lib/philiprehberger/image_size/version.rb,
lib/philiprehberger/image_size/detector.rb,
lib/philiprehberger/image_size/image_info.rb

Defined Under Namespace

Modules: Detector Classes: Error, ImageInfo

Constant Summary collapse

VERSION =
'0.6.0'

Class Method Summary collapse

Class Method Details

.dimensions(path_or_io) ⇒ Array<Integer>

Get image dimensions as [width, height]

Parameters:

  • path_or_io (String, Pathname, IO, StringIO)

    file path or IO-like object

Returns:

  • (Array<Integer>)
    width, height

Raises:

  • (Error)

    if the file cannot be read or format is unrecognized



28
29
30
# File 'lib/philiprehberger/image_size.rb', line 28

def self.dimensions(path_or_io)
  of(path_or_io).to_a
end

.format(path_or_io) ⇒ Symbol

Detect image format

Parameters:

  • path_or_io (String, Pathname, IO, StringIO)

    file path or IO-like object

Returns:

  • (Symbol)

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

Raises:

  • (Error)

    if the file cannot be read or format is unrecognized



37
38
39
# File 'lib/philiprehberger/image_size.rb', line 37

def self.format(path_or_io)
  of(path_or_io).format
end

.of(path_or_io) ⇒ ImageInfo

Detect image dimensions and format from a file path or IO object

Parameters:

  • path_or_io (String, Pathname, IO, StringIO)

    file path or IO-like object

Returns:

  • (ImageInfo)

    image info with width, height, and format

Raises:

  • (Error)

    if the file cannot be read or format is unrecognized



16
17
18
19
20
21
# File 'lib/philiprehberger/image_size.rb', line 16

def self.of(path_or_io)
  info = read_with(path_or_io) { |io| Detector.detect(io) }
  raise Error, 'Unrecognized image format' if info.nil?

  info
end