image_mosaic 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16c5f7ada72b064ecf1bef0bd89f7dc50dc28a28
4
- data.tar.gz: 5da2b55388a4897099f432885488fe124601790b
3
+ metadata.gz: 984c3b5a6fbb11cb7b4576882355a63cb3889d68
4
+ data.tar.gz: eb396d1d438bed75f3da82e68ae150f61ac5e5c6
5
5
  SHA512:
6
- metadata.gz: df1829264594f547382597ca356919679b6a58ba9bd605a1da517e4bb29f219ea70dd03eca958ed637525086b4b59dd378a29a6348abc6b09a1080e565873973
7
- data.tar.gz: 240a137ef6e4c0453dcb2df2d7e607cd8160b605c47ac1ce7fdc96c307a794f0d199d7faf979b8a093481f5b07dfefabdc54e18abe24c3f694077236658bed46
6
+ metadata.gz: 8dad330e1d2acc3fe0e8c663469a20970ca81fc9d43de0936dddbfe812c52c43c33366b108eb066aea52877dd607b473b29d98663e381ddee0a41d6f7d20f257
7
+ data.tar.gz: 844f8b7463bb528be4ac4cf0eca87882ab63c638f15eec941df83f775789f22d2d6ca3b1bbd2d967cf81bd23bf42bd0ad3f15eadda2019b45a689f19420cfcb6
data/lib/image_mosaic.rb CHANGED
@@ -3,21 +3,21 @@ require 'mini_magick'
3
3
  require 'tempfile'
4
4
  require 'securerandom'
5
5
  require 'image_mosaic/data/background'
6
- require 'image_mosaic/data/setting'
7
6
  require 'image_mosaic/data/validator'
8
7
  require 'image_mosaic/image/child'
9
8
  require 'image_mosaic/image/mosaic'
10
9
  require 'image_mosaic/image/parent'
10
+ require 'image_mosaic/image/layout/grid'
11
11
  require 'image_mosaic/operations/compositor'
12
12
  require 'image_mosaic/operations/construct'
13
13
  require 'image_mosaic/railtie' if defined?(Rails)
14
14
 
15
15
  module ImageMosaic
16
- HEIGHT = 1000
17
16
  WIDTH = 1000
18
17
  COLOUR = '#ffffff'.freeze
18
+ LAYOUT = 'grid'.freeze
19
19
 
20
- def self.create(items, colour: COLOUR)
21
- Operations::Construct.new(items, colour: colour).parent
20
+ def self.create(items, layout: LAYOUT, colour: COLOUR, width: WIDTH)
21
+ Operations::Construct.new(items, layout: layout, colour: colour, width: width).parent
22
22
  end
23
23
  end
@@ -6,15 +6,20 @@ module ImageMosaic
6
6
  end
7
7
 
8
8
  def items
9
- @items ||= create_images.compact
9
+ @items ||= validate_items.compact
10
10
  end
11
11
 
12
12
  private
13
13
 
14
- def create_images
14
+ def validate_items
15
15
  @urls.map do |url|
16
- child = Image::Child.new(url)
17
- child if child.valid?
16
+ image = Image::Child.new(url)
17
+ next unless image.valid?
18
+ {
19
+ url: url,
20
+ h: image.height,
21
+ w: image.width
22
+ }
18
23
  end
19
24
  end
20
25
  end
@@ -12,6 +12,20 @@ module ImageMosaic
12
12
  false
13
13
  end
14
14
 
15
+ def width
16
+ dimensions[0]
17
+ end
18
+
19
+ def height
20
+ dimensions[1]
21
+ end
22
+
23
+ private
24
+
25
+ def dimensions
26
+ @dimensions ||= image.dimensions
27
+ end
28
+
15
29
  def image
16
30
  @image ||= MiniMagick::Image.open(@url)
17
31
  end
@@ -0,0 +1,46 @@
1
+ module ImageMosaic
2
+ module Image
3
+ module Layout
4
+ # Takes an array of URLs, returns a sliced array of Child images with sizes based on columns
5
+ class Grid
6
+ STARTING_Y = 0
7
+
8
+ def initialize(items, width: WIDTH)
9
+ @items = items
10
+ @width = width
11
+ end
12
+
13
+ def items(y: STARTING_Y)
14
+ [].tap do |images|
15
+ rows.each do |row|
16
+ x = 0
17
+ row.each do |cell|
18
+ images << cell.merge(y: y, x: x, h: child_dimension, w: child_dimension)
19
+ x += child_dimension
20
+ end
21
+ y += child_dimension
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def rows
29
+ @rows ||= valid_items.each_slice(columns).to_a
30
+ end
31
+
32
+ def columns
33
+ Math.sqrt(valid_items.count).floor
34
+ end
35
+
36
+ def child_dimension
37
+ @width / columns
38
+ end
39
+
40
+ def valid_items
41
+ @valid_items ||= Data::Validator.new(@items).items
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -2,35 +2,27 @@ module ImageMosaic
2
2
  module Image
3
3
  #Takes an array of images and metrics and then returns a single image of composited images.
4
4
  class Parent
5
- def initialize(items, colour:, columns:, dimension:)
5
+ def initialize(items, colour:, width:)
6
6
  @items = items
7
7
  @colour = colour
8
- @columns = columns
9
- @dimension = dimension
8
+ @width = width
10
9
  end
11
10
 
12
11
  def create
13
- image.run_command(:convert, '-size', "#{WIDTH}x#{HEIGHT}", "xc:#{@colour}", image.path)
14
- y = 0
15
- sliced_grid.each do |row|
16
- x = 0
17
- row.each do |cell|
18
- @image = add_image(cell, x, y)
19
- x += @dimension
20
- end
21
- y += @dimension
22
- end
12
+ image.run_command(:convert, '-size', dimensions, "xc:#{@colour}", image.path)
13
+ @items.each { |item| @image = add_image(item) }
23
14
  Mosaic.new(image)
24
15
  end
25
16
 
26
17
  private
27
18
 
28
- def sliced_grid
29
- @items.each_slice(@columns).to_a
19
+ def dimensions
20
+ # Parent image is always square for time being.
21
+ "#{@width}x#{@width}"
30
22
  end
31
23
 
32
- def add_image(child_image, x, y)
33
- Operations::Compositor.new(@image, child_image).save(x, y, type: 'Over', dimension: @dimension)
24
+ def add_image(image)
25
+ Operations::Compositor.new(@image, image).save
34
26
  end
35
27
 
36
28
  def temp_file
@@ -8,22 +8,17 @@ module ImageMosaic
8
8
  @child = child
9
9
  end
10
10
 
11
- def save(x, y, type: 'Over', dimension: 200)
12
- resize_image_to(dimension)
13
- @composite_image.composite(child_image) do |composite|
11
+ def save(type: 'Over')
12
+ @composite_image.composite(image) do |composite|
14
13
  composite.compose type
15
- composite.geometry "+#{x}+#{y}"
14
+ composite.geometry "+#{@child[:x]}+#{@child[:y]}"
16
15
  end
17
16
  end
18
17
 
19
18
  private
20
19
 
21
- def resize_image_to(dimension)
22
- child_image.resize "#{dimension}x#{dimension}^"
23
- end
24
-
25
- def child_image
26
- @child.image
20
+ def image
21
+ @image ||= MiniMagick::Image.open(@child[:url]).resize "#{@child[:w]}x#{@child[:h]}^"
27
22
  end
28
23
  end
29
24
  end
@@ -1,27 +1,29 @@
1
1
  module ImageMosaic
2
2
  module Operations
3
3
  class Construct
4
- def initialize(items, colour: COLOUR)
4
+ def initialize(items, layout: LAYOUT, colour: COLOUR, width: WIDTH)
5
5
  @items = items
6
+ @layout = layout
6
7
  @colour = colour
8
+ @width = width
7
9
  end
8
10
 
9
11
  def parent
10
- @parent ||= Image::Parent.new(valid_items, colour: @colour, columns: setting.columns, dimension: setting.dimension).create
12
+ @parent ||= Image::Parent.new(layout_items, colour: @colour, width: @width).create
11
13
  end
12
14
 
13
15
  private
14
16
 
15
- def setting
16
- @setting ||= Data::Setting.new(valid_items.count)
17
+ def background
18
+ @background ||= Data::Background.new(@colour).hex
17
19
  end
18
20
 
19
- def valid_items
20
- @valid_items ||= Data::Validator.new(@items).items
21
+ def layout_items
22
+ @layout_items ||= layout_klass.new(@items, width: @width).items
21
23
  end
22
24
 
23
- def background
24
- @background ||= Data::Background.new(@colour).hex
25
+ def layout_klass
26
+ Kernel.const_get("ImageMosaic::Image::Layout::#{@layout.split('_').collect(&:capitalize).join}")
25
27
  end
26
28
  end
27
29
  end
@@ -1,3 +1,3 @@
1
1
  module ImageMosaic
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_mosaic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Goodwin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-27 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,9 +86,9 @@ files:
86
86
  - image_mosaic.gemspec
87
87
  - lib/image_mosaic.rb
88
88
  - lib/image_mosaic/data/background.rb
89
- - lib/image_mosaic/data/setting.rb
90
89
  - lib/image_mosaic/data/validator.rb
91
90
  - lib/image_mosaic/image/child.rb
91
+ - lib/image_mosaic/image/layout/grid.rb
92
92
  - lib/image_mosaic/image/mosaic.rb
93
93
  - lib/image_mosaic/image/parent.rb
94
94
  - lib/image_mosaic/operations/compositor.rb
@@ -1,18 +0,0 @@
1
- module ImageMosaic
2
- module Data
3
- class Setting
4
- def initialize(count, width: WIDTH)
5
- @count = count
6
- @width = width
7
- end
8
-
9
- def columns
10
- Math.sqrt(@count).ceil
11
- end
12
-
13
- def dimension
14
- @width / columns
15
- end
16
- end
17
- end
18
- end