class Useless::Museum::Image

Museum::Image is a utility class that provides two types of functionality:

* image resizing based upon the Useless::Musuem specifications
* access to the following image metadata: latitude, longitude, shot_at

It takes an IO object upon initialization. The resize methods return instances of MiniMagick::Image.

Public Class Methods

new(raw_io) click to toggle source
# File lib/useless/museum/image.rb, line 13
def initialize(raw_io)
  @raw_io = raw_io
end

Public Instance Methods

base() click to toggle source

MiniMagick::Image for the original image

# File lib/useless/museum/image.rb, line 18
def base
  @base ||= minimagick_copy
end
large() click to toggle source

and 'large' is 1024 pixels

# File lib/useless/museum/image.rb, line 38
def large
  @large ||= version('1024x1024')
end
latitude() click to toggle source

Just grab latitude

# File lib/useless/museum/image.rb, line 43
def latitude
  @latitude ||= exifr.gps.latitude if exifr and exifr.gps
end
longitude() click to toggle source

and longitude from EXIFR

# File lib/useless/museum/image.rb, line 48
def longitude
  @longitude ||= exifr.gps.longitude if exifr and exifr.gps
end
medium() click to toggle source

'medium' is 500 pixels

# File lib/useless/museum/image.rb, line 33
def medium
  @medium ||= version('500x500')
end
shot_at() click to toggle source

Shot at is just the date_time provide from EXIFR

# File lib/useless/museum/image.rb, line 53
def shot_at
  @shot_at ||= exifr.date_time if exifr
end
small() click to toggle source

A 'small' image's longest side is 100 pixels,

# File lib/useless/museum/image.rb, line 28
def small
  @small ||= version('100x100')
end
valid?() click to toggle source

Only JPEGs are considered valid

# File lib/useless/museum/image.rb, line 23
def valid?
  base and ['JPEG', 'TIFF', 'PNG', 'GIF'].include?(base['format'])
end

Private Instance Methods

blob() click to toggle source

A binary blob of the original image

# File lib/useless/museum/image.rb, line 94
def blob
  @blob ||= begin
    # Make sure the pointer is up front,
    @raw_io.rewind

    # read the bytes,
    blob = @raw_io.read

    # make sure they're un-encoded (encodings can cause multiple bytes to
    # be interpreted as one, which is no good for images),
    blob.force_encoding('BINARY')

    # and return
    blob
  end
end
exifr() click to toggle source

An EXIFR::JPEG instance, which provides access to the image metadata.

# File lib/useless/museum/image.rb, line 60
def exifr
  return nil unless base

  @exifr ||= case base['format']
    when 'JPEG' then EXIFR::JPEG.new(StringIO.new(blob))
    when 'TIFF' then EXIFR::TIFF.new(StringIO.new(blob))
    else nil
    end
end
minimagick_copy() click to toggle source
# File lib/useless/museum/image.rb, line 82
def minimagick_copy
  # Just instantiate a new instance with the raw image blob.
  MiniMagick::Image.read(blob)

  # If the 'image' cannot be parsed,
rescue MiniMagick::Invalid

  # just return nil
  nil
end
version(dimensions) click to toggle source
# File lib/useless/museum/image.rb, line 70
def version(dimensions)
  # Copy the image if possible,
  if image = minimagick_copy

    # resize it to the specified dimensions
    image.resize dimensions

    # and return
    image
  end
end