Class: DynamicImage::ImageReader
- Inherits:
-
Object
- Object
- DynamicImage::ImageReader
- 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
-
#format ⇒ DynamicImage::Format?
The format of the image, sniffed from its header.
-
#initialize(data) ⇒ ImageReader
constructor
A new instance of ImageReader.
-
#read ⇒ Vips::Image
Reads the image.
-
#valid_header? ⇒ Boolean
Returns true if the header belongs to a supported format.
Constructor Details
#initialize(data) ⇒ ImageReader
Returns a new instance of ImageReader.
15 16 17 |
# File 'lib/dynamic_image/image_reader.rb', line 15 def initialize(data) @data = data end |
Instance Method Details
#format ⇒ DynamicImage::Format?
The format of the image, sniffed from its header.
22 23 24 |
# File 'lib/dynamic_image/image_reader.rb', line 22 def format DynamicImage::Format.sniff(file_header) end |
#read ⇒ Vips::Image
Reads the image.
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.
44 45 46 |
# File 'lib/dynamic_image/image_reader.rb', line 44 def valid_header? format ? true : false end |