class FlickIt::Collage

Public Class Methods

new() click to toggle source
# File lib/collage.rb, line 3
def initialize
  @image = Magick::Image
  @image_list = Magick::ImageList.new
end

Public Instance Methods

create(photos) click to toggle source

Collates all of the images and creates collage

# File lib/collage.rb, line 9
def create(photos)
  added_local_image_files = add_local_image_files(photos)
  add_to_images_list(added_local_image_files)
  montage_it.write(ENV['HOME'] + '/Desktop/flickr_collage.jpg')
  puts "CREATED COLLAGE"
end
delete_images() click to toggle source

Removes images from image folder

# File lib/collage.rb, line 16
def delete_images
  FileUtils.rm_rf(Dir.glob('./lib/images/*'))
end

Private Instance Methods

add_local_image_files(photos) click to toggle source

Creates local images from data in api

# File lib/collage.rb, line 24
def add_local_image_files(photos)
  photos.map do |photo|
    path = "./lib/images/#{photo['id']}.jpg"
    open(path, 'wb') do |file|
      file << open(create_photo_url(photo)).read
    end
    photo.merge!({'local_path' => open(path)})
  end
end
add_to_images_list(photos) click to toggle source

Adds images to image list to use to create montage

# File lib/collage.rb, line 45
def add_to_images_list(photos)
  photos.each do |photo|
    img =  @image.read(photo['local_path']).first
    img.border!(30, 30, "#ffffff")
    @image_list << img
  end
  @image_list
end
create_photo_url(photo) click to toggle source

Creates image url to get photo from

# File lib/collage.rb, line 35
def create_photo_url(photo)
  "http://farm#{photo['farm']}.static.flickr.com/#{photo['server']}/#{photo['id']}_#{photo['secret']}.jpg"
end
crop(photo) click to toggle source

Crops photo

# File lib/collage.rb, line 40
def crop(photo)
  @image.read(photo['local_path']).first.crop!(300, 400, 300, 400)
end
montage_it(image_list = @image_list) click to toggle source

Creates montage and forms an image

# File lib/collage.rb, line 55
def montage_it(image_list = @image_list)
  photo_width = 250
  photo_height = 250
  padding_between_column = 0
  padding_between_row = 0
  montaged_image = image_list.montage do
    self.tile = "2x5"
    self.geometry = "#{photo_width}x#{photo_height}+#{padding_between_column}+#{padding_between_row}"
  end
  montaged_image.flatten_images
end