module ImageMosaic module Image #Takes an array of images and metrics and then returns a single image of composited images. class Parent def initialize(items, colour: 'white', columns: 5, dimension: 200) @items = items @colour = colour @columns = columns @dimension = dimension end def create image.run_command(:convert, '-size', "#{WIDTH}x#{HEIGHT}", "xc:#{@colour}", image.path) y = 0 sliced_grid.each do |row| x = 0 row.each do |cell| @image = add_image(cell, x, y) x += @dimension end y += @dimension end image end private def sliced_grid @items.each_slice(@columns).to_a end def add_image(child_image, x, y) Operations::Compositor.new(@image, child_image).save(x, y, type: 'Over', dimension: @dimension) end def temp_file @temp_file ||= Tempfile.new(['image_mosaic', '.png']) end def image @image ||= MiniMagick::Image.new(temp_file.path) end end end end