Class: DynamicImage::ImageReader

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_image/image_reader.rb

Overview

DynamicImage Image Reader

Reads an image into vips, identifying the format from the file header and not from anything the client claims. Accepts a Pathname, an IO object or a binary string.

Animated formats are opened with all their frames.

Constant Summary collapse

HEADER_BYTES =

Number of bytes needed to identify a format.

32

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ImageReader

Returns a new instance of ImageReader.

Parameters:

  • data (Pathname, IO, String)

    the image



15
16
17
# File 'lib/dynamic_image/image_reader.rb', line 15

def initialize(data)
  @data = data
end

Instance Method Details

#formatDynamicImage::Format?

The format of the image, sniffed from its header.

Returns:



22
23
24
# File 'lib/dynamic_image/image_reader.rb', line 22

def format
  DynamicImage::Format.sniff(file_header)
end

#readVips::Image

Reads the image.

Returns:

  • (Vips::Image)

Raises:



30
31
32
33
34
35
36
37
38
39
# File 'lib/dynamic_image/image_reader.rb', line 30

def read
  raise DynamicImage::Errors::InvalidHeader unless valid_header?

  if @data.is_a?(String)
    Vips::Image.new_from_buffer(@data, option_string)
  else
    path = @data.is_a?(Pathname) ? @data.to_s : @data.path
    Vips::Image.new_from_file(path + option_string, access: :random)
  end
end

#valid_header?Boolean

Returns true if the header belongs to a supported format.

Returns:

  • (Boolean)


44
45
46
# File 'lib/dynamic_image/image_reader.rb', line 44

def valid_header?
  format ? true : false
end