mojo_magick 0.5.5 → 0.6.3
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 +5 -5
- data/.circleci/config.yml +25 -0
- data/.github/dependabot.yml +8 -0
- data/.rubocop-common.yml +99 -0
- data/.rubocop-performance.yml +2 -0
- data/.rubocop-rspec.yml +33 -0
- data/.rubocop.yml +11 -0
- data/.ruby-version +1 -1
- data/Gemfile +6 -1
- data/Gemfile.lock +58 -10
- data/README.md +35 -4
- data/Rakefile +14 -12
- data/examples/animated_gif.rb +11 -14
- data/examples/composite.rb +11 -14
- data/init.rb +1 -3
- data/lib/image_magick/fonts.rb +4 -11
- data/lib/mojo_magick.rb +116 -103
- data/lib/mojo_magick/command_status.rb +3 -2
- data/lib/mojo_magick/commands.rb +32 -0
- data/lib/mojo_magick/errors.rb +1 -1
- data/lib/mojo_magick/font.rb +4 -7
- data/lib/mojo_magick/opt_builder.rb +24 -35
- data/lib/mojo_magick/util/font_parser.rb +43 -0
- data/lib/mojo_magick/util/parser.rb +11 -60
- data/lib/mojo_magick/version.rb +1 -1
- data/mojo_magick.gemspec +20 -15
- data/test/fixtures/roll with it.jpg +0 -0
- data/test/font_parser_test.rb +29 -0
- data/test/font_test.rb +22 -28
- data/test/mojo_magick_test.rb +288 -283
- data/test/opt_builder_test.rb +105 -89
- data/test/test_helper.rb +7 -5
- metadata +78 -18
- data/lib/image_magick/resource_limits.rb +0 -96
- data/lib/initializers/hash.rb +0 -8
- data/test/fonts_test.rb +0 -12
- data/test/parser_test.rb +0 -30
- data/test/resource_limits_test.rb +0 -51
@@ -0,0 +1,43 @@
|
|
1
|
+
# rubocop:disable Lint/AssignmentInCondition
|
2
|
+
module MojoMagick
|
3
|
+
module Util
|
4
|
+
class FontParser
|
5
|
+
attr_reader :raw_fonts
|
6
|
+
|
7
|
+
def initialize(raw_fonts)
|
8
|
+
@raw_fonts = raw_fonts
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse
|
12
|
+
fonts = {}
|
13
|
+
enumerator = raw_fonts.split(/\n/).each
|
14
|
+
name = nil
|
15
|
+
while begin; line = enumerator.next; rescue StopIteration; line = nil; end
|
16
|
+
line.chomp!
|
17
|
+
line = enumerator.next if line_is_empty(line)
|
18
|
+
m = /^\s*Font:\s+(.*)$/.match(line)
|
19
|
+
if m
|
20
|
+
name = m[1].strip
|
21
|
+
fonts[name] = { name: name }
|
22
|
+
else
|
23
|
+
k, v = extract_key_value(line)
|
24
|
+
fonts[name][k] = v if k && name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
fonts.values.map { |f| MojoMagick::Font.new f }
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def extract_key_value(line)
|
33
|
+
key_val = line.split(/:/).map(&:strip)
|
34
|
+
[key_val[0].downcase.to_sym, key_val[1]]
|
35
|
+
end
|
36
|
+
|
37
|
+
def line_is_empty(line)
|
38
|
+
line.nil? || line.empty? || (/^\s+$/ =~ line)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
# rubocop:enable Lint/AssignmentInCondition
|
@@ -1,69 +1,20 @@
|
|
1
|
+
require_relative "./font_parser"
|
1
2
|
module MojoMagick
|
2
3
|
module Util
|
3
4
|
class Parser
|
4
|
-
|
5
|
+
attr_reader :raw_fonts
|
5
6
|
|
6
|
-
def
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
name = nil
|
11
|
-
while (begin; line = enumerator.next; rescue StopIteration; line=nil; end) do
|
12
|
-
line.chomp!
|
13
|
-
line = enumerator.next if line.nil? || line.empty? || (/^\s+$/ =~ line)
|
14
|
-
if m = /^\s*Font:\s+(.*)$/.match(line)
|
15
|
-
name = m[1].strip
|
16
|
-
fonts[name] = {:name => name}
|
17
|
-
else
|
18
|
-
key_val = line.split(/:/).map(&:strip)
|
19
|
-
k = key_val[0].downcase.to_sym
|
20
|
-
v = key_val[1]
|
21
|
-
if k && name
|
22
|
-
fonts[name][k] = key_val[1]
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
fonts.values.map{|f| MojoMagick::Font.new f}
|
7
|
+
def initialize
|
8
|
+
warn "DEPRECATION WARNING: This class has been deprecated and will be removed with"\
|
9
|
+
" the next minor version release."\
|
10
|
+
" Please use `MojoMagick::Util::FontParser` instead"
|
27
11
|
end
|
28
12
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
resources = header.strip.split
|
35
|
-
limits = data.strip.split
|
36
|
-
|
37
|
-
actual_values = {}
|
38
|
-
readable_values = {}
|
39
|
-
|
40
|
-
resources.each_index do |i|
|
41
|
-
resource = resources[i].downcase.to_sym
|
42
|
-
scale = limits[i].match(%r{[a-z]+$}) || []
|
43
|
-
value = limits[i].match(%r{^[0-9]+})
|
44
|
-
unscaled_value = value ? value[0].to_i : -1
|
45
|
-
case scale[0]
|
46
|
-
when 'eb'
|
47
|
-
scaled_value = unscaled_value * (2 ** 60)
|
48
|
-
when 'pb'
|
49
|
-
scaled_value = unscaled_value * (2 ** 50)
|
50
|
-
when 'tb'
|
51
|
-
scaled_value = unscaled_value * (2 ** 40)
|
52
|
-
when 'gb'
|
53
|
-
scaled_value = unscaled_value * (2 ** 30)
|
54
|
-
when 'mb'
|
55
|
-
scaled_value = unscaled_value * (2 ** 20)
|
56
|
-
when 'kb'
|
57
|
-
scaled_value = unscaled_value * (2 ** 10)
|
58
|
-
when 'b'
|
59
|
-
scaled_value = unscaled_value
|
60
|
-
else
|
61
|
-
scaled_value = unscaled_value
|
62
|
-
end
|
63
|
-
actual_values[resource] = scaled_value
|
64
|
-
readable_values[resource] = limits[i]
|
65
|
-
end
|
66
|
-
[actual_values, readable_values]
|
13
|
+
def parse_fonts(fonts)
|
14
|
+
warn "DEPRECATION WARNING: #{__method__} has been deprecated and will be removed with"\
|
15
|
+
" the next minor version release."\
|
16
|
+
" Please use `MojoMagick::Util::FontParser#parse` instead"
|
17
|
+
FontParser.new(fonts).parse
|
67
18
|
end
|
68
19
|
end
|
69
20
|
end
|
data/lib/mojo_magick/version.rb
CHANGED
data/mojo_magick.gemspec
CHANGED
@@ -1,37 +1,42 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.push File.expand_path("lib", __dir__)
|
2
2
|
require "mojo_magick/version"
|
3
3
|
|
4
|
-
post_install_message =
|
4
|
+
post_install_message = <<~EOPOST_INSTALL
|
5
5
|
|
6
|
-
Thanks for installing MojoMagick - keepin it simple!
|
6
|
+
Thanks for installing MojoMagick - keepin it simple!
|
7
7
|
|
8
|
-
*** To make this gem work, you need a few binaries!
|
9
|
-
Make sure you've got ImageMagick available. http://imagemagick.org
|
10
|
-
If you plan to build images with text (using the "label" method) you'll need freetype and ghostscript as well.
|
11
|
-
Check out http://www.freetype.org and http://ghostscript.com respectively for installation info.
|
8
|
+
*** To make this gem work, you need a few binaries!
|
9
|
+
Make sure you've got ImageMagick available. http://imagemagick.org
|
10
|
+
If you plan to build images with text (using the "label" method) you'll need freetype and ghostscript as well.
|
11
|
+
Check out http://www.freetype.org and http://ghostscript.com respectively for installation info.
|
12
12
|
|
13
|
-
|
13
|
+
EOPOST_INSTALL
|
14
14
|
|
15
15
|
Gem::Specification.new do |s|
|
16
|
-
s.name
|
17
|
-
s.license
|
16
|
+
s.name = "mojo_magick"
|
17
|
+
s.license = "MIT"
|
18
18
|
s.version = MojoMagick::VERSION
|
19
19
|
s.platform = Gem::Platform::RUBY
|
20
20
|
s.authors = ["Steve Midgley", "Elliot Nelson", "Jon Rogers"]
|
21
21
|
s.email = ["science@misuse.org", "jon@rcode5.com"]
|
22
22
|
s.homepage = "http://github.com/rcode5/mojo_magick"
|
23
23
|
s.summary = "mojo_magick-#{MojoMagick::VERSION}"
|
24
|
-
s.description =
|
24
|
+
s.description = "Simple Ruby stateless module interface to imagemagick."
|
25
|
+
s.required_ruby_version = ">= 2.6.0"
|
25
26
|
|
26
27
|
s.rubyforge_project = "mojo_magick"
|
27
28
|
|
28
29
|
s.files = `git ls-files`.split("\n")
|
29
30
|
s.test_files = `git ls-files -- {test,features}/*`.split("\n")
|
30
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
31
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
31
32
|
s.require_paths = ["lib"]
|
32
|
-
s.add_development_dependency(
|
33
|
-
s.add_development_dependency(
|
34
|
-
s.add_development_dependency(
|
33
|
+
s.add_development_dependency("bundle-audit")
|
34
|
+
s.add_development_dependency("bundler")
|
35
|
+
s.add_development_dependency("minitest")
|
36
|
+
s.add_development_dependency("rake")
|
37
|
+
s.add_development_dependency("rspec-expectations")
|
38
|
+
s.add_development_dependency("rubocop")
|
39
|
+
s.add_development_dependency("rubocop-performance")
|
35
40
|
|
36
41
|
s.post_install_message = post_install_message
|
37
42
|
end
|
Binary file
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
IDENTIFY_FONT_RESPONSE = <<~EOFONT
|
4
|
+
|
5
|
+
Font: Zapf-Dingbats
|
6
|
+
family: Zapf Dingbats
|
7
|
+
style: Normal
|
8
|
+
stretch: Normal
|
9
|
+
weight: 400
|
10
|
+
glyphs: /System/Library/Fonts/ZapfDingbats.ttf
|
11
|
+
|
12
|
+
Font: Zapfino
|
13
|
+
family: Zapfino
|
14
|
+
style: Italic
|
15
|
+
stretch: Normal
|
16
|
+
weight: 400
|
17
|
+
glyphs: /Library/Fonts/Zapfino.ttf
|
18
|
+
|
19
|
+
|
20
|
+
EOFONT
|
21
|
+
|
22
|
+
class FontParserTest < MiniTest::Test
|
23
|
+
def test_parse_fonts
|
24
|
+
parser = MojoMagick::Util::FontParser.new(IDENTIFY_FONT_RESPONSE)
|
25
|
+
parsed_fonts = parser.parse
|
26
|
+
assert_equal parsed_fonts.length, 2
|
27
|
+
assert_equal parsed_fonts[1].style, "Italic"
|
28
|
+
end
|
29
|
+
end
|
data/test/font_test.rb
CHANGED
@@ -1,35 +1,29 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
class FontTest < MiniTest::Unit::TestCase
|
20
|
-
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
IDENTIFY_FONT_RESPONSE = <<~EO_FONTS
|
4
|
+
Font: Zapf-Dingbats
|
5
|
+
family: Zapf Dingbats
|
6
|
+
style: Normal
|
7
|
+
stretch: Normal
|
8
|
+
weight: 400
|
9
|
+
glyphs: /System/Library/Fonts/ZapfDingbats.ttf
|
10
|
+
Font: Zapfino
|
11
|
+
family: Zapfino
|
12
|
+
style: Italic
|
13
|
+
stretch: Normal
|
14
|
+
weight: 400
|
15
|
+
glyphs: /Library/Fonts/Zapfino.ttf
|
16
|
+
EO_FONTS
|
17
|
+
|
18
|
+
class FontTest < MiniTest::Test
|
21
19
|
def test_font
|
22
20
|
f = MojoMagick::Font.new
|
23
|
-
|
21
|
+
assert_nil f.name
|
24
22
|
assert_equal f.valid?, false
|
25
23
|
|
26
|
-
f = MojoMagick::Font.new(:
|
27
|
-
assert_equal f.name,
|
24
|
+
f = MojoMagick::Font.new(name: "Zapfino", weight: 400)
|
25
|
+
assert_equal f.name, "Zapfino"
|
28
26
|
assert_equal f.valid?, true
|
29
|
-
assert_equal f.weight,
|
30
|
-
|
27
|
+
assert_equal f.weight, 400
|
31
28
|
end
|
32
|
-
|
33
29
|
end
|
34
|
-
|
35
|
-
|
data/test/mojo_magick_test.rb
CHANGED
@@ -1,283 +1,288 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class MojoMagickTest < MiniTest::
|
4
|
-
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
@
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
assert_equal
|
28
|
-
assert_equal
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
orig_image_size = File
|
35
|
-
size_test_temp = Tempfile
|
36
|
-
size_test = size_test_temp.path
|
37
|
-
retval = MojoMagick
|
38
|
-
assert_equal size_test, retval
|
39
|
-
assert_equal orig_image_size, File
|
40
|
-
assert_equal retval, size_test
|
41
|
-
new_dimensions = MojoMagick
|
42
|
-
assert_equal 100, new_dimensions[:height]
|
43
|
-
assert_equal 67, new_dimensions[:width]
|
44
|
-
|
45
|
-
# we should be able to resize image right over itself
|
46
|
-
retval = MojoMagick
|
47
|
-
assert_equal @test_image, retval
|
48
|
-
refute_equal orig_image_size, File
|
49
|
-
new_dimensions = MojoMagick
|
50
|
-
assert_equal
|
51
|
-
assert_equal
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_image_resize_with_percentage
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
size_test
|
70
|
-
|
71
|
-
assert_equal
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
assert_equal
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
File.
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
c.
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
out_image = File
|
176
|
-
|
177
|
-
MojoMagick
|
178
|
-
c.label '
|
179
|
-
c.file out_image
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
def
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
c.crop
|
249
|
-
c.
|
250
|
-
end
|
251
|
-
|
252
|
-
|
253
|
-
assert_equal
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
r
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class MojoMagickTest < MiniTest::Test
|
4
|
+
# we keep a fixtures path and a working path so that we can easily test image
|
5
|
+
# manipulation routines without tainting the original images
|
6
|
+
def setup
|
7
|
+
@fixtures_path = File.expand_path(File.join(File.dirname(__FILE__), "fixtures"))
|
8
|
+
@working_path = File.join(@fixtures_path, "tmp")
|
9
|
+
|
10
|
+
reset_images
|
11
|
+
|
12
|
+
@test_image = File.join(@working_path, "5742.jpg")
|
13
|
+
@out_image = File.join(@working_path, "out1.jpg")
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset_images
|
17
|
+
FileUtils.rm_r(@working_path) if File.exist?(@working_path)
|
18
|
+
FileUtils.mkdir(@working_path)
|
19
|
+
Dir.glob(File.join(@fixtures_path, "*")).each do |file|
|
20
|
+
FileUtils.cp(file, @working_path) if File.file?(file)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_get_image_size
|
25
|
+
orig_image_size = File.size(@test_image)
|
26
|
+
retval = MojoMagick.get_image_size(@test_image)
|
27
|
+
assert_equal orig_image_size, File.size(@test_image)
|
28
|
+
assert_equal 500, retval[:height]
|
29
|
+
assert_equal 333, retval[:width]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_image_resize
|
33
|
+
# test basic resizing
|
34
|
+
orig_image_size = File.size(@test_image)
|
35
|
+
size_test_temp = Tempfile.new("mojo_test")
|
36
|
+
size_test = size_test_temp.path
|
37
|
+
retval = MojoMagick.resize(@test_image, size_test, { width: 100, height: 100 })
|
38
|
+
assert_equal size_test, retval
|
39
|
+
assert_equal orig_image_size, File.size(@test_image)
|
40
|
+
assert_equal retval, size_test
|
41
|
+
new_dimensions = MojoMagick.get_image_size(size_test)
|
42
|
+
assert_equal 100, new_dimensions[:height]
|
43
|
+
assert_equal 67, new_dimensions[:width]
|
44
|
+
|
45
|
+
# we should be able to resize image right over itself
|
46
|
+
retval = MojoMagick.resize(@test_image, @test_image, { width: 150, height: 150 })
|
47
|
+
assert_equal @test_image, retval
|
48
|
+
refute_equal orig_image_size, File.size(@test_image)
|
49
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
50
|
+
assert_equal 150, new_dimensions[:height]
|
51
|
+
assert_equal 100, new_dimensions[:width]
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_image_resize_with_percentage
|
55
|
+
original_size = MojoMagick.get_image_size(@test_image)
|
56
|
+
retval = MojoMagick.resize(@test_image, @test_image, { percent: 50 })
|
57
|
+
assert_equal @test_image, retval
|
58
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
59
|
+
%i[height width].each do |dim|
|
60
|
+
assert_equal (original_size[dim] / 2.0).ceil, new_dimensions[dim]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_shrink_with_big_dimensions
|
65
|
+
# image shouldn't resize if we specify very large dimensions and specify "shrink_only"
|
66
|
+
size_test_temp = Tempfile.new("mojo_test")
|
67
|
+
size_test = size_test_temp.path
|
68
|
+
retval = MojoMagick.shrink(@test_image, size_test, { width: 1000, height: 1000 })
|
69
|
+
assert_equal size_test, retval
|
70
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
71
|
+
assert_equal 500, new_dimensions[:height]
|
72
|
+
assert_equal 333, new_dimensions[:width]
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_shrink
|
76
|
+
# image should resize if we specify small dimensions and shrink_only
|
77
|
+
retval = MojoMagick.shrink(@test_image, @test_image, { width: 1000, height: 100 })
|
78
|
+
assert_equal @test_image, retval
|
79
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
80
|
+
assert_equal 100, new_dimensions[:height]
|
81
|
+
assert_equal 67, new_dimensions[:width]
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_resize_with_shrink_only_options
|
85
|
+
# image should resize if we specify small dimensions and shrink_only
|
86
|
+
retval = MojoMagick.resize(@test_image, @test_image, { shrink_only: true, width: 400, height: 400 })
|
87
|
+
assert_equal @test_image, retval
|
88
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
89
|
+
assert_equal 400, new_dimensions[:height]
|
90
|
+
assert_equal 266, new_dimensions[:width]
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_expand_with_small_dim
|
94
|
+
# image shouldn't resize if we specify small dimensions and expand_only
|
95
|
+
_orig_image_size = File.size(@test_image)
|
96
|
+
retval = MojoMagick.expand(@test_image, @test_image, { width: 10, height: 10 })
|
97
|
+
assert_equal @test_image, retval
|
98
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
99
|
+
assert_equal 500, new_dimensions[:height]
|
100
|
+
assert_equal 333, new_dimensions[:width]
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_expand
|
104
|
+
# image should resize if we specify large dimensions and expand_only
|
105
|
+
retval = MojoMagick.expand(@test_image, @test_image, { width: 1000, height: 1000 })
|
106
|
+
assert_equal @test_image, retval
|
107
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
108
|
+
assert_equal 1000, new_dimensions[:height]
|
109
|
+
assert_equal 666, new_dimensions[:width]
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_invalid_images
|
113
|
+
# test bad images
|
114
|
+
bad_image = File.join(@working_path, "not_an_image.jpg")
|
115
|
+
zero_image = File.join(@working_path, "zero_byte_image.jpg")
|
116
|
+
assert_raises(MojoMagick::MojoFailed) { MojoMagick.get_image_size(bad_image) }
|
117
|
+
assert_raises(MojoMagick::MojoFailed) { MojoMagick.get_image_size(zero_image) }
|
118
|
+
assert_raises(MojoMagick::MojoFailed) do
|
119
|
+
MojoMagick.get_image_size("/file_does_not_exist_here_ok.jpg")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_resize_with_fill
|
124
|
+
@test_image = File.join(@working_path, "5742.jpg")
|
125
|
+
MojoMagick.resize(@test_image, @test_image, { fill: true, width: 100, height: 100 })
|
126
|
+
dim = MojoMagick.get_image_size(@test_image)
|
127
|
+
assert_equal 100, dim[:width]
|
128
|
+
assert_equal 150, dim[:height]
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_resize_with_fill_and_crop
|
132
|
+
@test_image = File.join(@working_path, "5742.jpg")
|
133
|
+
MojoMagick.resize(@test_image, @test_image, { fill: true, crop: true, width: 150, height: 120 })
|
134
|
+
dim = MojoMagick.get_image_size(@test_image)
|
135
|
+
assert_equal 150, dim[:width]
|
136
|
+
assert_equal 120, dim[:height]
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_tempfile
|
140
|
+
# Create a tempfile and return the path
|
141
|
+
filename = MojoMagick.tempfile("binary data")
|
142
|
+
File.open(filename, "rb") do |f|
|
143
|
+
assert_equal f.read, "binary data"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_label
|
148
|
+
out_image = File.join(@working_path, "label_test.png")
|
149
|
+
|
150
|
+
MojoMagick.convert do |c|
|
151
|
+
c.label "rock the house"
|
152
|
+
c.file out_image
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_label_with_quote
|
157
|
+
out_image = File.join(@working_path, "label_test.png")
|
158
|
+
|
159
|
+
MojoMagick.convert do |c|
|
160
|
+
c.label 'rock "the house'
|
161
|
+
c.file out_image
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_label_with_apostrophe
|
166
|
+
out_image = File.join(@working_path, "label_test.png")
|
167
|
+
|
168
|
+
MojoMagick.convert do |c|
|
169
|
+
c.label "rock 'the house"
|
170
|
+
c.file out_image
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_label_with_quotes
|
175
|
+
out_image = File.join(@working_path, "label_test.png")
|
176
|
+
|
177
|
+
MojoMagick.convert do |c|
|
178
|
+
c.label 'this is "it!"'
|
179
|
+
c.file out_image
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_bad_command
|
184
|
+
MojoMagick.convert do |c|
|
185
|
+
c.unknown_option "fail"
|
186
|
+
c.file "boogabooga.whatever"
|
187
|
+
end
|
188
|
+
rescue MojoMagick::MojoFailed => e
|
189
|
+
assert e.message.include?("unrecognized option"),
|
190
|
+
"Unable to find ImageMagick commandline error in the message"
|
191
|
+
assert e.message.include?("convert.c/ConvertImageCommand"),
|
192
|
+
"Unable to find ImageMagick commandline error in the message"
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_blob_rgb
|
196
|
+
data = (Array.new(16) { [rand > 0.5 ? 0 : 255] * 3 }).flatten
|
197
|
+
bdata = data.pack "C" * data.size
|
198
|
+
out = "out.png"
|
199
|
+
MojoMagick.convert(nil, "png:#{out}") do |c|
|
200
|
+
c.blob bdata, format: :rgb, depth: 8, size: "4x4"
|
201
|
+
end
|
202
|
+
r = MojoMagick.get_image_size(out)
|
203
|
+
assert r[:height] == 4
|
204
|
+
assert r[:width] == 4
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_convert
|
208
|
+
MojoMagick.convert do |c|
|
209
|
+
c.file @test_image
|
210
|
+
c.crop "92x64+0+0"
|
211
|
+
c.repage!
|
212
|
+
c.file @out_image
|
213
|
+
end
|
214
|
+
retval = MojoMagick.get_image_size(@out_image)
|
215
|
+
assert_equal 92, retval[:width]
|
216
|
+
assert_equal 64, retval[:height]
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_mogrify
|
220
|
+
MojoMagick.convert do |c|
|
221
|
+
c.file @test_image
|
222
|
+
c.file @out_image
|
223
|
+
end
|
224
|
+
# Simple mogrify test
|
225
|
+
MojoMagick.mogrify do |m|
|
226
|
+
m.crop "32x32+0+0"
|
227
|
+
m.repage!
|
228
|
+
m.file @out_image
|
229
|
+
end
|
230
|
+
retval = MojoMagick.get_image_size(@out_image)
|
231
|
+
assert_equal 32, retval[:width]
|
232
|
+
assert_equal 32, retval[:height]
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_convert_crop_and_repage
|
236
|
+
MojoMagick.convert(@test_image, @out_image) do |c|
|
237
|
+
c.crop "100x100+0+0"
|
238
|
+
c.repage!
|
239
|
+
end
|
240
|
+
retval = MojoMagick.get_image_size(@out_image)
|
241
|
+
assert_equal 100, retval[:width]
|
242
|
+
assert_equal 100, retval[:height]
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_mogrify_with_shave_and_repage
|
246
|
+
MojoMagick.convert do |c|
|
247
|
+
c.file @test_image
|
248
|
+
c.crop "100x100+0+0"
|
249
|
+
c.file @out_image
|
250
|
+
end
|
251
|
+
MojoMagick.mogrify(@out_image) { |m| m.shave("25x25").repage! }
|
252
|
+
retval = MojoMagick.get_image_size(@out_image)
|
253
|
+
assert_equal 50, retval[:width]
|
254
|
+
assert_equal 50, retval[:height]
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_convert_rgb
|
258
|
+
bdata = "aaaaaabbbbbbccc"
|
259
|
+
out = "out.png"
|
260
|
+
MojoMagick.convert do |c|
|
261
|
+
c.blob bdata, format: :rgb, depth: 8, size: "5x1"
|
262
|
+
c.file out
|
263
|
+
end
|
264
|
+
r = MojoMagick.get_image_size(out)
|
265
|
+
assert r[:height] == 1
|
266
|
+
assert r[:width] == 5
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_convert_rgba
|
270
|
+
bdata = "1111222233334444"
|
271
|
+
out = "out.png"
|
272
|
+
MojoMagick.convert do |c|
|
273
|
+
c.blob bdata, format: :rgba, depth: 8, size: "4x1"
|
274
|
+
c.file out
|
275
|
+
end
|
276
|
+
r = MojoMagick.get_image_size(out)
|
277
|
+
assert r[:height] == 1
|
278
|
+
assert r[:width] == 4
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_available_fonts
|
282
|
+
fonts = MojoMagick.available_fonts
|
283
|
+
assert fonts.is_a? Array
|
284
|
+
assert fonts.length > 1
|
285
|
+
assert fonts.first.name
|
286
|
+
assert(fonts.first.name.is_a?(String))
|
287
|
+
end
|
288
|
+
end
|