module ImageMosaic module Image #Takes an array of images and metrics and then returns a single image of compisited images. class Parent def initialize(items, width: 1000, height: 1000, colour: 'white', columns: 5, cell_dimension: 200) @items = items @width = width @height = height @colour = colour @columns = columns @cell_dimension = cell_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 += @cell_dimension end y += @cell_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: @cell_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