imaginizer 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de494832c870f1af6fe744725bde17584a1af420
4
- data.tar.gz: fae5df7e05998921daf031b480d5e87865aafcfb
3
+ metadata.gz: fee99c35bfde210d37239c941457949cb2cb24b4
4
+ data.tar.gz: bb7031eadd8e90a375ec200e5fbc8cd7bc2cf025
5
5
  SHA512:
6
- metadata.gz: 69201581fdfa70b2b0b16afd4a3705a0bba1d4e0690f36acd50aad266b54721180486be1dc45167d680c307bb761bd4159239827355f6dedb8db8cf2102af1df
7
- data.tar.gz: ba25e0f37d499d2768a8aef54827fd1e796b9e398a0fb5bd0ea783eed2501d880335205755352871d13cedba0faf9c8558502455053d5267123c0d91c41be826
6
+ metadata.gz: b136a036a217039b2410e851bdf8cbdf02c442051fbcd66795e7f9e3de60a83209b8cec4026dedb85663ae2ed5f5398c517bdbf4e9397315f0db9d8f14098410
7
+ data.tar.gz: 83ca37e1b2a61f82133c6e918e79f017206a40f2ac9a44fc68609d4b1c52df438e5c3cf25841ed4f343c66bf641cfa9140a731dd7e7613c4a47ea25c45aa1ca0
@@ -1,196 +1,196 @@
1
1
  require 'mini_magick'
2
2
 
3
- class Imaginizer::Image
4
- attr_accessor :image_data, :image_path, :image_size
3
+ module Imaginizer
4
+ class Image
5
+ attr_accessor :image_data, :image_path, :image_size
5
6
 
6
- def initialize(source_path = nil)
7
- @image_data = MiniMagick::Image.open source_path || empty_image_path
8
- end
9
-
10
- def self.create(size, image_path)
11
- new.tap do |image|
12
- image.image_size = size
13
- image.image_path = image_path
14
- yield image
15
- image.image_data.write image_path
7
+ def initialize(source_path = nil)
8
+ @image_data = MiniMagick::Image.open source_path || empty_image_path
16
9
  end
17
- end
18
-
19
- def make_jpeg
20
- @image_data.format 'jpg'
21
- end
22
10
 
23
- def resize(w = image_width, h = image_height)
24
- @image_data.resize "#{w}x#{h}!"
25
- end
26
-
27
- def set_background(path_or_url)
28
- v = values_for :background
29
- overlay_image img: path_or_url, blur: '0x15', fill: true, x: v[:x],
30
- y: v[:y], h: v[:h]
31
- end
11
+ def self.create(size, image_path)
12
+ new.tap do |image|
13
+ image.image_size = size
14
+ image.image_path = image_path
15
+ yield image
16
+ image.image_data.write image_path
17
+ end
18
+ end
32
19
 
33
- def set_foreground(path_or_url)
34
- v = values_for :foreground
35
- overlay_image img: path_or_url, h: v[:h], w: v[:w] unless v[:skip]
36
- end
20
+ def make_jpeg
21
+ @image_data.format 'jpg'
22
+ end
37
23
 
38
- def set_title(text)
39
- set_text :title, text do |v|
40
- {style: 'bold', uppercase: true, x: v[:x], y: v[:y], size: v[:size]}
24
+ def resize(w = image_width, h = image_height)
25
+ @image_data.resize "#{w}x#{h}!"
41
26
  end
42
- end
43
27
 
44
- def set_subtitle(text)
45
- set_text :subtitle, text do |v|
46
- {style: 'italic', size: 14, x: v[:x], y: v[:y]}
28
+ def set_background(path_or_url)
29
+ v = values_for :background
30
+ overlay_image img: path_or_url, blur: '0x15', fill: true, x: v[:x],
31
+ y: v[:y], h: v[:h]
47
32
  end
48
- end
49
33
 
50
- def set_footer(text)
51
- overlay_box values_for(:footer)
52
- set_text :footer, text do |v|
53
- {style: 'bold', uppercase: true, x: v[:x], y: v[:y], size: v[:size]}
34
+ def set_foreground(path_or_url)
35
+ v = values_for :foreground
36
+ overlay_image img: path_or_url, h: v[:h], w: v[:w] unless v[:skip]
54
37
  end
55
- end
56
38
 
57
- protected
39
+ def set_title(text)
40
+ set_text :title, text do |v|
41
+ {style: 'bold', uppercase: true, x: v[:x], y: v[:y], size: v[:size]}
42
+ end
43
+ end
58
44
 
59
- def blur(radius_x_sigma)
60
- @image_data.combine_options {|c| c.blur radius_x_sigma} if radius_x_sigma
61
- end
45
+ def set_subtitle(text)
46
+ set_text :subtitle, text do |v|
47
+ {style: 'italic', size: 14, x: v[:x], y: v[:y]}
48
+ end
49
+ end
62
50
 
63
- def crop(options = {})
64
- return if options.none?
65
- w = options[:w] || width
66
- h = options[:h] || height
67
- x_offset = (width - w)/2
68
- y_offset = (height- h)/2
69
- @image_data.crop "#{w}x#{h}+#{x_offset}+#{y_offset}"
70
- end
51
+ def set_footer(text)
52
+ overlay_box values_for(:footer)
53
+ set_text :footer, text do |v|
54
+ {style: 'bold', uppercase: true, x: v[:x], y: v[:y], size: v[:size]}
55
+ end
56
+ end
71
57
 
72
- private
73
-
74
- def values_for(property)
75
- {medium_rectangle: {
76
- width: 300,
77
- height: 250,
78
- background: {y: 125, h: 125},
79
- foreground: {h: 125},
80
- title: {y: 23, second_line_y: 57, size: 40},
81
- subtitle: {y: 84},
82
- footer: {h: 30, size: 25, y: 111}
83
- },
84
- rectangle: {
85
- width: 180,
86
- height: 150,
87
- foreground: {skip: true},
88
- title: {y: -39, second_line_y: -12, size: 34},
89
- subtitle: {y: 13},
90
- footer: {h: 28, size: 16, y: 63}
91
- },
92
- wide_skyscraper: {
93
- width: 160,
94
- height: 600,
95
- background: {y: 250, h: 125},
96
- foreground: {h: 250},
97
- title: {y: -23, second_line_y: 0, size: 30},
98
- subtitle: {y: 33},
99
- footer: {h: 94, size: 26, y: 242, second_line_y: 263}
100
- },
101
- leaderboard: {
102
- width: 728,
103
- height: 90,
104
- background: {x: 228},
105
- foreground: {w: 228},
106
- title: {x: 120, y: -24, size: 42},
107
- subtitle: {x: 120, y: 5},
108
- footer: {w: 500, h: 29, size: 26, x: 120, y: 30}
109
- }
110
- }[@image_size].fetch property, {}
111
- end
58
+ protected
112
59
 
113
- [:height, :width, :format].each do |method|
114
- define_method method do
115
- image_data[method]
60
+ def blur(radius_x_sigma)
61
+ @image_data.combine_options {|c| c.blur radius_x_sigma} if radius_x_sigma
116
62
  end
117
- end
118
63
 
119
- def size
120
- "#{width}x#{height}"
121
- end
64
+ def crop(options = {})
65
+ return if options.none?
66
+ w = options[:w] || width
67
+ h = options[:h] || height
68
+ x_offset = (width - w)/2
69
+ y_offset = (height- h)/2
70
+ @image_data.crop "#{w}x#{h}+#{x_offset}+#{y_offset}"
71
+ end
122
72
 
123
- def image_width
124
- values_for :width
125
- end
73
+ private
74
+
75
+ def values_for(property)
76
+ {medium_rectangle: {
77
+ width: 300,
78
+ height: 250,
79
+ background: {y: 125, h: 125},
80
+ foreground: {h: 125},
81
+ title: {y: 23, second_line_y: 57, size: 40},
82
+ subtitle: {y: 84},
83
+ footer: {h: 30, size: 25, y: 111}
84
+ },
85
+ rectangle: {
86
+ width: 180,
87
+ height: 150,
88
+ foreground: {skip: true},
89
+ title: {y: -39, second_line_y: -12, size: 34},
90
+ subtitle: {y: 13},
91
+ footer: {h: 28, size: 16, y: 63}
92
+ },
93
+ wide_skyscraper: {
94
+ width: 160,
95
+ height: 600,
96
+ background: {y: 250, h: 125},
97
+ foreground: {h: 250},
98
+ title: {y: -23, second_line_y: 0, size: 30},
99
+ subtitle: {y: 33},
100
+ footer: {h: 94, size: 26, y: 242, second_line_y: 263}
101
+ },
102
+ leaderboard: {
103
+ width: 728,
104
+ height: 90,
105
+ background: {x: 228},
106
+ foreground: {w: 228},
107
+ title: {x: 120, y: -24, size: 42},
108
+ subtitle: {x: 120, y: 5},
109
+ footer: {w: 500, h: 29, size: 26, x: 120, y: 30}
110
+ }
111
+ }[@image_size].fetch property, {}
112
+ end
126
113
 
127
- def image_height
128
- values_for :height
129
- end
114
+ [:height, :width, :format].each do |method|
115
+ define_method method do
116
+ image_data[method]
117
+ end
118
+ end
130
119
 
131
- def set_text(size, text)
132
- values = values_for size
133
- options = {text: text}.merge yield(values)
134
- if values[:second_line_y]
135
- line1, line2 = word_wrap options[:text]
136
- overlay_text options.merge(text: line1)
137
- overlay_text options.merge(text: line2, y: values[:second_line_y])
138
- else
139
- overlay_text options
120
+ def size
121
+ "#{width}x#{height}"
140
122
  end
141
- end
142
123
 
143
- def word_wrap(text)
144
- text.delete("\n").sub(/(.{1,9})(\s+|$)/, "\\1\n").strip.split("\n")
145
- end
124
+ def image_width
125
+ values_for :width
126
+ end
146
127
 
147
- def overlay_text(options = {})
148
- return unless text = options[:text]
149
- text = text.upcase if options[:uppercase]
150
- @image_data.combine_options do |c|
151
- c.fill 'white'
152
- c.font font_path(options[:style])
153
- c.gravity 'center'
154
- c.pointsize options[:size] if options[:size]
155
- c.draw "text #{options[:x] || 0},#{options[:y] || 0} '#{options[:text]}'"
128
+ def image_height
129
+ values_for :height
156
130
  end
157
- end
158
131
 
159
- def overlay_box(options = {})
160
- return if options.none?
161
- x2 = width
162
- y2 = height
163
- x1 = x2 - (options[:w] || x2)
164
- y1 = y2 - (options[:h] || y2)
165
- @image_data.combine_options do |c|
166
- c.fill 'graya(0%, 0.5)'
167
- c.draw "rectangle #{x1}, #{y1}, #{x2}, #{y2}"
132
+ def set_text(size, text)
133
+ values = values_for size
134
+ options = {text: text}.merge yield(values)
135
+ if values[:second_line_y]
136
+ line1, line2 = word_wrap options[:text]
137
+ overlay_text options.merge(text: line1)
138
+ overlay_text options.merge(text: line2, y: values[:second_line_y])
139
+ else
140
+ overlay_text options
141
+ end
168
142
  end
169
- end
170
143
 
171
- def overlay_image(options = {})
172
- return unless options[:img]
173
- image = self.class.new options[:img]
174
- image.blur options[:blur]
144
+ def word_wrap(text)
145
+ text.delete("\n").sub(/(.{1,9})(\s+|$)/, "\\1\n").strip.split "\n"
146
+ end
175
147
 
176
- image.crop options
177
- image.resize image_width, image_height if options[:fill]
178
- overlay_x = options[:x] || 0
179
- overlay_y = options[:y] || 0
180
- @image_data = @image_data.composite image.image_data do |c|
181
- c.geometry "#{image.width}x#{image.height}+#{overlay_x}+#{overlay_y}"
148
+ def overlay_text(options = {})
149
+ return unless text = options[:text]
150
+ text = text.upcase if options[:uppercase]
151
+ @image_data.combine_options do |c|
152
+ c.fill 'white'
153
+ c.font font_path(options[:style])
154
+ c.gravity 'center'
155
+ c.pointsize options[:size] if options[:size]
156
+ c.draw "text #{options[:x] || 0},#{options[:y] || 0} '#{text}'"
157
+ end
182
158
  end
183
- end
184
159
 
160
+ def overlay_box(options = {})
161
+ return if options.none?
162
+ x2 = width
163
+ y2 = height
164
+ x1 = x2 - (options[:w] || x2)
165
+ y1 = y2 - (options[:h] || y2)
166
+ @image_data.combine_options do |c|
167
+ c.fill 'graya(0%, 0.5)'
168
+ c.draw "rectangle #{x1}, #{y1}, #{x2}, #{y2}"
169
+ end
170
+ end
185
171
 
172
+ def overlay_image(options = {})
173
+ return unless options[:img]
174
+ image = self.class.new options[:img]
175
+ image.blur options[:blur]
176
+
177
+ image.crop options
178
+ image.resize image_width, image_height if options[:fill]
179
+ overlay_x = options[:x] || 0
180
+ overlay_y = options[:y] || 0
181
+ @image_data = @image_data.composite image.image_data do |c|
182
+ c.geometry "#{image.width}x#{image.height}+#{overlay_x}+#{overlay_y}"
183
+ end
184
+ end
186
185
 
187
- def empty_image_path
188
- File.expand_path '../../assets/1x1.jpg', __FILE__
189
- end
186
+ def empty_image_path
187
+ File.expand_path '../../assets/1x1.jpg', __FILE__
188
+ end
190
189
 
191
- def font_path(style)
192
- style ||= 'regular'
193
- font_file = "../../assets/RobotoCondensed-#{style.capitalize}.ttf"
194
- File.expand_path font_file, __FILE__
190
+ def font_path(style)
191
+ style ||= 'regular'
192
+ font_file = "../../assets/RobotoCondensed-#{style.capitalize}.ttf"
193
+ File.expand_path font_file, __FILE__
194
+ end
195
195
  end
196
196
  end
@@ -1,3 +1,3 @@
1
1
  module Imaginizer
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imaginizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Baccigalupo