Module: Philiprehberger::ImageSize::Detector

Defined in:
lib/philiprehberger/image_size/detector.rb

Overview

Core detection logic that reads minimal bytes from image file headers to determine format and dimensions without decoding the full image.

Constant Summary collapse

SOF_MARKERS =
[0xFFC0, 0xFFC2].freeze
PNG_SIGNATURE =
[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A].pack('C8').freeze
JPEG_SIGNATURE =
[0xFF, 0xD8].pack('C2').freeze
TIFF_LE_SIGNATURE =
'II'
TIFF_BE_SIGNATURE =
'MM'

Class Method Summary collapse

Class Method Details

.detect(io) ⇒ ImageInfo?

Detect image format and dimensions from an IO-like object

Parameters:

  • io (IO, StringIO)

    an IO object positioned at the start

Returns:

  • (ImageInfo, nil)

    image info or nil if format is unrecognized



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/philiprehberger/image_size/detector.rb', line 19

def detect(io)
  header = io.read(64)
  return nil if header.nil? || header.length < 8

  header.force_encoding('BINARY')

  detect_png(io, header) ||
    detect_jpeg(io, header) ||
    detect_gif(io, header) ||
    detect_bmp(header) ||
    detect_webp(io, header) ||
    detect_tiff(io, header) ||
    detect_ico_cur(header) ||
    detect_avif(io, header) ||
    detect_svg(io, header)
end