class WaxIiif::ImageVariant

Class ImageVariant represents a single image file within a manifest.

@author David Newbury <david.newbury@gmail.com>

Attributes

id[R]

@!attribute [r] id

@return [String] The URI for the variant.
uri[R]

@!attribute [r] uri @return [String] The URI for the jpeg image

Public Class Methods

new(data, config, size = nil) click to toggle source

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”.

@param [Hash] data A Image Data object. @param [WaxIiif::Config] config The configuration object @param [Number] width the desired width of this object in pixels @param [Number] height the desired height of this object in pixels @raise WaxIiif::Error::InvalidImageData

# 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

Public Instance Methods

generate_image_id(id) click to toggle source

Generate a URI for an image

@param [String] id The specific ID for the image @param [String, Number] page_number The page number for this particular image.

@return [<type>] <description>

# 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
height() click to toggle source

Get the image height

@return [Number] The height of the image in pixels

# File lib/wax_iiif/image_variant.rb, line 72
def height
  @image.height
end
mime_type() click to toggle source

Get the MIME Content-Type of the image.

@return [String] the MIME Content-Type (typically 'image/jpeg')

# File lib/wax_iiif/image_variant.rb, line 80
def mime_type
  @image.mime_type
end
width() click to toggle source

Get the image width

@return [Number] The width of the image in pixels

# File lib/wax_iiif/image_variant.rb, line 65
def width
  @image.width
end

Protected Instance Methods

filestring() click to toggle source
# File lib/wax_iiif/image_variant.rb, line 105
def filestring
  "/#{region}/#{width},/0"
end
region() click to toggle source
# File lib/wax_iiif/image_variant.rb, line 97
def region
  'full'
end
resize(width) click to toggle source
# File lib/wax_iiif/image_variant.rb, line 101
def resize(width)
  @image.resize width
end