Class: WaxIiif::ImageVariant

Inherits:
Object
  • Object
show all
Includes:
MiniMagick, Utilities::Helpers
Defined in:
lib/wax_iiif/image_variant.rb

Overview

Class ImageVariant represents a single image file within a manifest.

Direct Known Subclasses

FullImage, ImageTile, Thumbnail

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities::Helpers

#escape_yaml, #generate_build_location, #generate_id, #generate_image_location, #get_data_path, #save_to_disk

Constructor Details

#initialize(data, config, size = nil) ⇒ ImageVariant

Initializing an ImageVariant will create the actual image file on the file system.

To initialize an image, you will need the data hash to have an “id”, a “image_path”, and a “page_number”.

Parameters:

  • data (Hash)

    A Image Data object.

  • config (WaxIiif::Config)

    The configuration object

  • width (Number)

    the desired width of this object in pixels

  • height (Number)

    the desired height of this object in pixels

Raises:

  • WaxIiif::Error::InvalidImageData



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wax_iiif/image_variant.rb', line 27

def initialize(data, config, size = nil)
  @config = config

  raise WaxIiif::Error::InvalidImageData, 'Each image needs an ID' if data.id.nil? || data.id.to_s.empty?
  raise WaxIiif::Error::InvalidImageData, 'Each image needs an path.' if data.image_path.nil? || data.image_path.to_s.empty?

  # open image
  begin
    @image = Image.open(data.image_path)
  rescue MiniMagick::Invalid => e
    raise WaxIiif::Error::InvalidImageData, "Cannot read this image file: #{data.image_path}. #{e}"
  end

  width = size.nil? ? width : size
  resize(width)

  @image.format 'jpg'
  @id   = generate_image_id(data.id)
  @uri  = "#{id}#{filestring}/default.jpg"

  # Create the on-disk version of the file
  path = "#{@id}#{filestring}".gsub(@config.base_url, @config.output_dir)
  FileUtils.mkdir_p path
  filename = "#{path}/default.jpg"
  @image.write filename unless File.exist? filename
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



60
61
62
# File 'lib/wax_iiif/image_variant.rb', line 60

def id
  @id
end

#uriString (readonly)

Returns The URI for the jpeg image.

Returns:

  • (String)

    The URI for the jpeg image



56
57
58
# File 'lib/wax_iiif/image_variant.rb', line 56

def uri
  @uri
end

Instance Method Details

#generate_image_id(id) ⇒ <type>

Generate a URI for an image

Parameters:

  • id (String)

    The specific ID for the image

  • page_number (String, Number)

    The page number for this particular image.

Returns:

  • (<type>)

    <description>



91
92
93
# File 'lib/wax_iiif/image_variant.rb', line 91

def generate_image_id(id)
  "#{@config.base_url}#{@config.prefix}/#{@config.image_directory_name}/#{id}"
end

#heightNumber

Get the image height

Returns:

  • (Number)

    The height of the image in pixels



72
73
74
# File 'lib/wax_iiif/image_variant.rb', line 72

def height
  @image.height
end

#mime_typeString

Get the MIME Content-Type of the image.

Returns:

  • (String)

    the MIME Content-Type (typically ‘image/jpeg’)



80
81
82
# File 'lib/wax_iiif/image_variant.rb', line 80

def mime_type
  @image.mime_type
end

#widthNumber

Get the image width

Returns:

  • (Number)

    The width of the image in pixels



65
66
67
# File 'lib/wax_iiif/image_variant.rb', line 65

def width
  @image.width
end