Skip to content

Instantly share code, notes, and snippets.

@khy
Created February 11, 2012 19:08
Show Gist options
  • Save khy/1803660 to your computer and use it in GitHub Desktop.
Save khy/1803660 to your computer and use it in GitHub Desktop.
Image Infrastructure for the Street Art API
require 'mini_magick'
require 'exifr'
module Useless
class StreetArt
class Image
def initialize(raw_io)
@raw_io = raw_io
end
def base
@base ||= minimagick_copy
end
private
def minimagick_copy
# Just instantiate a new instance with the raw image blob
MiniMagick::Image.read(blob)
end
# A binary blob of the original image
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
end
end
end
module Useless
class StreetArt
class Image
def small
@small ||= version('100x100')
end
def medium
@medium ||= version('500x500')
end
def large
@large ||= version('1024x1024')
end
private
def version(dimensions)
# Copy the image
image = minimagick_copy
# resize it to the specified dimensions
image.resize dimensions
# and return
image
end
end
end
end
module Useless
class StreetArt
class Image
def latitude
@latitude ||= exifr.gps.latitude if exifr and exifr.gps
end
def longitude
@longitude ||= exifr.gps.longitude if exifr and exifr.gps
end
def shot_at
@shot_at ||= exifr.date_time if exifr
end
private
def exifr
@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
end
end
end
module Useless
class StreetArt
class Image
def valid?
['JPEG', 'TIFF', 'PNG', 'GIF'].include?(base['format'])
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment