mojo_magick 0.5.2 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/Gemfile.lock +19 -10
- data/LICENSE.txt +1 -1
- data/README.md +34 -0
- data/Rakefile +6 -12
- data/examples/animated_gif.rb +1 -2
- data/examples/composite.rb +1 -2
- data/init.rb +1 -3
- data/lib/image_magick/fonts.rb +4 -4
- data/lib/image_magick/resource_limits.rb +43 -45
- data/lib/initializers/hash.rb +5 -1
- data/lib/mojo_magick.rb +190 -186
- data/lib/mojo_magick/command_status.rb +11 -0
- data/lib/mojo_magick/errors.rb +5 -0
- data/lib/mojo_magick/font.rb +4 -6
- data/lib/mojo_magick/opt_builder.rb +21 -9
- data/lib/mojo_magick/util/parser.rb +25 -28
- data/lib/mojo_magick/version.rb +1 -1
- data/mojo_magick.gemspec +19 -17
- data/test/font_test.rb +19 -25
- data/test/fonts_test.rb +4 -5
- data/test/mojo_magick_test.rb +277 -256
- data/test/opt_builder_test.rb +31 -14
- data/test/parser_test.rb +15 -16
- data/test/resource_limits_test.rb +33 -21
- data/test/test_helper.rb +3 -3
- metadata +36 -20
- data/lib/image_resources.rb +0 -0
data/lib/mojo_magick/font.rb
CHANGED
@@ -1,23 +1,21 @@
|
|
1
1
|
module MojoMagick
|
2
|
-
|
3
2
|
class Font
|
4
|
-
|
5
3
|
attr_accessor :name, :family, :style, :stretch, :weight, :glyphs
|
6
4
|
|
7
5
|
def valid?
|
8
|
-
!
|
6
|
+
!name.nil?
|
9
7
|
end
|
10
8
|
|
11
9
|
def initialize(property_hash = {})
|
12
10
|
property_hash.symbolize_keys!
|
13
|
-
[
|
11
|
+
%i[name family style stretch weight glyphs].each do |f|
|
14
12
|
setter = "#{f}="
|
15
|
-
|
13
|
+
send(setter, property_hash[f])
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
17
|
def self.all
|
20
|
-
ImageMagick::Font.all.map{|font_info| Font.new(font_info)}
|
18
|
+
ImageMagick::Font.all.map { |font_info| Font.new(font_info) }
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
@@ -28,6 +28,16 @@ module MojoMagick
|
|
28
28
|
@opts << "label:#{quoted_arg(args.join)}"
|
29
29
|
end
|
30
30
|
|
31
|
+
# annotate takes non-standard args
|
32
|
+
def annotate(*args)
|
33
|
+
@opts << '-annotate'
|
34
|
+
arguments = args.join.split
|
35
|
+
arguments.unshift '0' if arguments.length == 1
|
36
|
+
arguments.each do |arg|
|
37
|
+
add_formatted arg
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
31
41
|
# Create a temporary file for the given image and add to command line
|
32
42
|
def format(*args)
|
33
43
|
@opts << '-format'
|
@@ -39,10 +49,10 @@ module MojoMagick
|
|
39
49
|
def blob(*args)
|
40
50
|
data = args[0]
|
41
51
|
opts = args[1] || {}
|
42
|
-
opts.each do |k,v|
|
43
|
-
send(k.to_s,v.to_s)
|
52
|
+
opts.each do |k, v|
|
53
|
+
send(k.to_s, v.to_s)
|
44
54
|
end
|
45
|
-
tmpfile = MojoMagick
|
55
|
+
tmpfile = MojoMagick.tempfile(data, opts)
|
46
56
|
file tmpfile
|
47
57
|
end
|
48
58
|
|
@@ -55,11 +65,11 @@ module MojoMagick
|
|
55
65
|
|
56
66
|
# Generic commands. Arguments will be formatted if necessary
|
57
67
|
def method_missing(command, *args)
|
58
|
-
if command.to_s[-1, 1] == '!'
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
68
|
+
@opts << if command.to_s[-1, 1] == '!'
|
69
|
+
"+#{command.to_s.chop}"
|
70
|
+
else
|
71
|
+
"-#{command}"
|
72
|
+
end
|
63
73
|
args.each do |arg|
|
64
74
|
add_formatted arg
|
65
75
|
end
|
@@ -71,6 +81,7 @@ module MojoMagick
|
|
71
81
|
end
|
72
82
|
|
73
83
|
protected
|
84
|
+
|
74
85
|
def add_formatted(arg)
|
75
86
|
# Quote anything that would cause problems on *nix or windows
|
76
87
|
@opts << quoted_arg(arg)
|
@@ -78,7 +89,8 @@ module MojoMagick
|
|
78
89
|
|
79
90
|
def quoted_arg(arg)
|
80
91
|
return arg unless arg =~ /[#'<>^|&();` ]/
|
81
|
-
|
92
|
+
|
93
|
+
['"', arg.gsub('"', '\"').gsub("'", "\'"), '"'].join
|
82
94
|
end
|
83
95
|
end
|
84
96
|
end
|
@@ -4,30 +4,26 @@ module MojoMagick
|
|
4
4
|
# handle parsing outputs from ImageMagick commands
|
5
5
|
|
6
6
|
def parse_fonts(raw_fonts)
|
7
|
-
font = nil
|
8
7
|
fonts = {}
|
9
8
|
enumerator = raw_fonts.split(/\n/).each
|
10
9
|
name = nil
|
11
|
-
while
|
10
|
+
while begin; line = enumerator.next; rescue StopIteration; line = nil; end
|
12
11
|
line.chomp!
|
13
12
|
line = enumerator.next if line.nil? || line.empty? || (/^\s+$/ =~ line)
|
14
13
|
if m = /^\s*Font:\s+(.*)$/.match(line)
|
15
14
|
name = m[1].strip
|
16
|
-
fonts[name] = {:
|
15
|
+
fonts[name] = {name: name}
|
17
16
|
else
|
18
17
|
key_val = line.split(/:/).map(&:strip)
|
19
18
|
k = key_val[0].downcase.to_sym
|
20
19
|
v = key_val[1]
|
21
|
-
if k && name
|
22
|
-
fonts[name][k] = key_val[1]
|
23
|
-
end
|
20
|
+
fonts[name][k] = v if k && name
|
24
21
|
end
|
25
22
|
end
|
26
|
-
fonts.values.map{|f| MojoMagick::Font.new f}
|
23
|
+
fonts.values.map { |f| MojoMagick::Font.new f}
|
27
24
|
end
|
28
25
|
|
29
26
|
def parse_limits(raw_limits)
|
30
|
-
|
31
27
|
row_limits = raw_limits.split("\n")
|
32
28
|
header = row_limits[0].chomp
|
33
29
|
data = row_limits[2].chomp
|
@@ -36,29 +32,30 @@ module MojoMagick
|
|
36
32
|
|
37
33
|
actual_values = {}
|
38
34
|
readable_values = {}
|
35
|
+
|
39
36
|
resources.each_index do |i|
|
40
37
|
resource = resources[i].downcase.to_sym
|
41
|
-
scale = limits[i].match(
|
42
|
-
value = limits[i].match(
|
38
|
+
scale = limits[i].match(/[a-z]+$/) || []
|
39
|
+
value = limits[i].match(/^[0-9]+/)
|
43
40
|
unscaled_value = value ? value[0].to_i : -1
|
44
|
-
case scale[0]
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
41
|
+
scaled_value = case scale[0]
|
42
|
+
when 'eb'
|
43
|
+
unscaled_value * (2**60)
|
44
|
+
when 'pb'
|
45
|
+
unscaled_value * (2**50)
|
46
|
+
when 'tb'
|
47
|
+
unscaled_value * (2**40)
|
48
|
+
when 'gb'
|
49
|
+
unscaled_value * (2**30)
|
50
|
+
when 'mb'
|
51
|
+
unscaled_value * (2**20)
|
52
|
+
when 'kb'
|
53
|
+
unscaled_value * (2**10)
|
54
|
+
when 'b'
|
55
|
+
unscaled_value
|
56
|
+
else
|
57
|
+
unscaled_value
|
58
|
+
end
|
62
59
|
actual_values[resource] = scaled_value
|
63
60
|
readable_values[resource] = limits[i]
|
64
61
|
end
|
data/lib/mojo_magick/version.rb
CHANGED
data/mojo_magick.gemspec
CHANGED
@@ -1,36 +1,38 @@
|
|
1
|
-
$:.push File.expand_path(
|
2
|
-
require
|
1
|
+
$:.push File.expand_path('lib', __dir__)
|
2
|
+
require 'mojo_magick/version'
|
3
3
|
|
4
|
-
post_install_message =
|
4
|
+
post_install_message = <<~EOF
|
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
|
EOF
|
14
14
|
|
15
15
|
Gem::Specification.new do |s|
|
16
|
-
s.name
|
16
|
+
s.name = 'mojo_magick'
|
17
|
+
s.license = 'MIT'
|
17
18
|
s.version = MojoMagick::VERSION
|
18
19
|
s.platform = Gem::Platform::RUBY
|
19
|
-
s.authors = [
|
20
|
-
s.email = [
|
21
|
-
s.homepage =
|
20
|
+
s.authors = ['Steve Midgley', 'Elliot Nelson', 'Jon Rogers']
|
21
|
+
s.email = ['science@misuse.org', 'jon@rcode5.com']
|
22
|
+
s.homepage = 'http://github.com/rcode5/mojo_magick'
|
22
23
|
s.summary = "mojo_magick-#{MojoMagick::VERSION}"
|
23
|
-
s.description =
|
24
|
+
s.description = 'Simple Ruby stateless module interface to imagemagick.'
|
24
25
|
|
25
|
-
s.rubyforge_project =
|
26
|
+
s.rubyforge_project = 'mojo_magick'
|
26
27
|
|
27
28
|
s.files = `git ls-files`.split("\n")
|
28
29
|
s.test_files = `git ls-files -- {test,features}/*`.split("\n")
|
29
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
30
|
-
s.require_paths = [
|
30
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
31
|
+
s.require_paths = ['lib']
|
32
|
+
s.add_development_dependency('minitest')
|
31
33
|
s.add_development_dependency('rake')
|
32
|
-
s.add_development_dependency('simplecov')
|
33
34
|
s.add_development_dependency('rspec-expectations')
|
35
|
+
s.add_development_dependency('simplecov')
|
34
36
|
|
35
37
|
s.post_install_message = post_install_message
|
36
38
|
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
|
-
glyphs: /Library/Fonts/Zapfino.ttf
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
IDENTIFY_FONT_RESPONSE = <<~EOF.freeze
|
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
|
17
16
|
EOF
|
18
17
|
|
19
|
-
class FontTest < Test
|
20
|
-
|
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(:
|
24
|
+
f = MojoMagick::Font.new(name: 'Zapfino', weight: 400)
|
27
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/fonts_test.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
require File
|
2
|
-
|
3
|
-
class FontsTest < Test::Unit::TestCase
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
4
2
|
|
3
|
+
class FontsTest < MiniTest::Test
|
5
4
|
def test_get_fonts
|
6
|
-
fonts = MojoMagick
|
5
|
+
fonts = MojoMagick.get_fonts
|
7
6
|
assert fonts.is_a? Array
|
8
7
|
assert fonts.length > 1
|
9
8
|
assert fonts.first.name
|
10
|
-
assert
|
9
|
+
assert(fonts.first.name.is_a?(String))
|
11
10
|
end
|
12
11
|
end
|
data/test/mojo_magick_test.rb
CHANGED
@@ -1,256 +1,277 @@
|
|
1
|
-
require File
|
2
|
-
|
3
|
-
class MojoMagickTest < Test
|
4
|
-
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
@
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
FileUtils
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
orig_image_size
|
25
|
-
|
26
|
-
assert_equal
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
size_test
|
37
|
-
|
38
|
-
assert_equal
|
39
|
-
|
40
|
-
assert_equal
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
assert_equal
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
assert_equal
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
assert_equal
|
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
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
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
|
-
c.
|
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
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
1
|
+
require File.join(File.dirname(__FILE__), '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
|
+
end
|
10
|
+
|
11
|
+
def reset_images
|
12
|
+
FileUtils.rm_r(@working_path) if File.exist?(@working_path)
|
13
|
+
FileUtils.mkdir(@working_path)
|
14
|
+
Dir.glob(File.join(@fixtures_path, '*')).each do |file|
|
15
|
+
FileUtils.cp(file, @working_path) if File.file?(file)
|
16
|
+
end
|
17
|
+
@test_image = File.join(@working_path, '5742.jpg')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_get_image_size
|
21
|
+
reset_images
|
22
|
+
orig_image_size = File.size(@test_image)
|
23
|
+
retval = MojoMagick.get_image_size(@test_image)
|
24
|
+
assert_equal orig_image_size, File.size(@test_image)
|
25
|
+
assert_equal 500, retval[:height]
|
26
|
+
assert_equal 333, retval[:width]
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_image_resize
|
30
|
+
# test basic resizing
|
31
|
+
reset_images
|
32
|
+
orig_image_size = File.size(@test_image)
|
33
|
+
size_test_temp = Tempfile.new('mojo_test')
|
34
|
+
size_test = size_test_temp.path
|
35
|
+
retval = MojoMagick.resize(@test_image, size_test, {width: 100, height: 100})
|
36
|
+
assert_equal size_test, retval
|
37
|
+
assert_equal orig_image_size, File.size(@test_image)
|
38
|
+
assert_equal retval, size_test
|
39
|
+
new_dimensions = MojoMagick.get_image_size(size_test)
|
40
|
+
assert_equal 100, new_dimensions[:height]
|
41
|
+
assert_equal 67, new_dimensions[:width]
|
42
|
+
|
43
|
+
# we should be able to resize image right over itself
|
44
|
+
retval = MojoMagick.resize(@test_image, @test_image, {width: 100, height: 100})
|
45
|
+
assert_equal @test_image, retval
|
46
|
+
refute_equal orig_image_size, File.size(@test_image)
|
47
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
48
|
+
assert_equal 100, new_dimensions[:height]
|
49
|
+
assert_equal 67, new_dimensions[:width]
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_image_resize_with_percentage
|
53
|
+
reset_images
|
54
|
+
original_size = MojoMagick.get_image_size(@test_image)
|
55
|
+
retval = MojoMagick.resize(@test_image, @test_image, {percent: 50})
|
56
|
+
assert_equal @test_image, retval
|
57
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
58
|
+
%i[height width].each do |dim|
|
59
|
+
assert_equal (original_size[dim] / 2.0).ceil, new_dimensions[dim]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_shrink_with_big_dimensions
|
64
|
+
# image shouldn't resize if we specify very large dimensions and specify "shrink_only"
|
65
|
+
reset_images
|
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
|
+
reset_images
|
77
|
+
# image should resize if we specify small dimensions and shrink_only
|
78
|
+
retval = MojoMagick.shrink(@test_image, @test_image, {width: 1000, height: 100})
|
79
|
+
assert_equal @test_image, retval
|
80
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
81
|
+
assert_equal 100, new_dimensions[:height]
|
82
|
+
assert_equal 67, new_dimensions[:width]
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_resize_with_shrink_only_options
|
86
|
+
reset_images
|
87
|
+
# image should resize if we specify small dimensions and shrink_only
|
88
|
+
retval = MojoMagick.resize(@test_image, @test_image, {shrink_only: true, width: 400, height: 400})
|
89
|
+
assert_equal @test_image, retval
|
90
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
91
|
+
assert_equal 400, new_dimensions[:height]
|
92
|
+
assert_equal 266, new_dimensions[:width]
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_expand_with_small_dim
|
96
|
+
# image shouldn't resize if we specify small dimensions and expand_only
|
97
|
+
reset_images
|
98
|
+
retval = MojoMagick.expand(@test_image, @test_image, {width: 10, height: 10})
|
99
|
+
assert_equal @test_image, retval
|
100
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
101
|
+
assert_equal 500, new_dimensions[:height]
|
102
|
+
assert_equal 333, new_dimensions[:width]
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_expand
|
106
|
+
reset_images
|
107
|
+
# image should resize if we specify large dimensions and expand_only
|
108
|
+
retval = MojoMagick.expand(@test_image, @test_image, {width: 1000, height: 1000})
|
109
|
+
assert_equal @test_image, retval
|
110
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
111
|
+
assert_equal 1000, new_dimensions[:height]
|
112
|
+
assert_equal 666, new_dimensions[:width]
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_invalid_images
|
116
|
+
# test bad images
|
117
|
+
bad_image = File.join(@working_path, 'not_an_image.jpg')
|
118
|
+
zero_image = File.join(@working_path, 'zero_byte_image.jpg')
|
119
|
+
assert_raises(MojoMagick::MojoFailed) {MojoMagick.get_image_size(bad_image)}
|
120
|
+
assert_raises(MojoMagick::MojoFailed) {MojoMagick.get_image_size(zero_image)}
|
121
|
+
assert_raises(MojoMagick::MojoFailed) {MojoMagick.get_image_size('/file_does_not_exist_here_ok.jpg')}
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_resize_with_fill
|
125
|
+
reset_images
|
126
|
+
@test_image = File.join(@working_path, '5742.jpg')
|
127
|
+
MojoMagick.resize(@test_image, @test_image, {fill: true, width: 100, height: 100})
|
128
|
+
dim = MojoMagick.get_image_size(@test_image)
|
129
|
+
assert_equal 100, dim[:width]
|
130
|
+
assert_equal 150, dim[:height]
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_resize_with_fill_and_crop
|
134
|
+
reset_images
|
135
|
+
@test_image = File.join(@working_path, '5742.jpg')
|
136
|
+
MojoMagick.resize(@test_image, @test_image, {fill: true, crop: true, width: 150, height: 120})
|
137
|
+
dim = MojoMagick.get_image_size(@test_image)
|
138
|
+
assert_equal 150, dim[:width]
|
139
|
+
assert_equal 120, dim[:height]
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_tempfile
|
143
|
+
# Create a tempfile and return the path
|
144
|
+
filename = MojoMagick.tempfile('binary data')
|
145
|
+
File.open(filename, 'rb') do |f|
|
146
|
+
assert_equal f.read, 'binary data'
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_label
|
151
|
+
reset_images
|
152
|
+
out_image = File.join(@working_path, 'label_test.png')
|
153
|
+
|
154
|
+
MojoMagick.convert do |c|
|
155
|
+
c.label 'rock the house'
|
156
|
+
c.file out_image
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_label_with_quote
|
161
|
+
reset_images
|
162
|
+
out_image = File.join(@working_path, 'label_test.png')
|
163
|
+
|
164
|
+
MojoMagick.convert do |c|
|
165
|
+
c.label 'rock "the house'
|
166
|
+
c.file out_image
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_label_with_apostrophe
|
171
|
+
reset_images
|
172
|
+
out_image = File.join(@working_path, 'label_test.png')
|
173
|
+
|
174
|
+
MojoMagick.convert do |c|
|
175
|
+
c.label 'rock \'the house'
|
176
|
+
c.file out_image
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_label_with_quotes
|
181
|
+
reset_images
|
182
|
+
out_image = File.join(@working_path, 'label_test.png')
|
183
|
+
|
184
|
+
MojoMagick.convert do |c|
|
185
|
+
c.label 'this is "it!"'
|
186
|
+
c.file out_image
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_bad_command
|
191
|
+
MojoMagick.convert do |c|
|
192
|
+
c.unknown_option 'fail'
|
193
|
+
c.file 'boogabooga.whatever'
|
194
|
+
end
|
195
|
+
rescue MojoMagick::MojoFailed => e
|
196
|
+
assert e.message.include?('unrecognized option'), 'Unable to find ImageMagick commandline error in the message'
|
197
|
+
assert e.message.include?('convert.c/ConvertImageCommand'), 'Unable to find ImageMagick commandline error in the message'
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_blob
|
201
|
+
reset_images
|
202
|
+
|
203
|
+
# RGB8 test
|
204
|
+
data = (16.times.map { [rand > 0.5 ? 0 : 255] * 3 }).flatten
|
205
|
+
bdata = data.pack 'C' * data.size
|
206
|
+
out = 'out.png'
|
207
|
+
MojoMagick.convert(nil, "png:#{out}") do |c|
|
208
|
+
c.blob bdata, format: :rgb, depth: 8, size: '4x4'
|
209
|
+
end
|
210
|
+
r = MojoMagick.get_image_size(out)
|
211
|
+
assert r[:height] == 4
|
212
|
+
assert r[:width] == 4
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_command_helpers
|
216
|
+
reset_images
|
217
|
+
test_image = File.join(@working_path, '5742.jpg')
|
218
|
+
out_image = File.join(@working_path, 'out1.jpg')
|
219
|
+
|
220
|
+
# Simple convert test
|
221
|
+
MojoMagick.convert do |c|
|
222
|
+
c.file test_image
|
223
|
+
c.crop '92x64+0+0'
|
224
|
+
c.repage!
|
225
|
+
c.file out_image
|
226
|
+
end
|
227
|
+
retval = MojoMagick.get_image_size(out_image)
|
228
|
+
assert_equal 92, retval[:width]
|
229
|
+
assert_equal 64, retval[:height]
|
230
|
+
|
231
|
+
# Simple mogrify test
|
232
|
+
MojoMagick.mogrify do |m|
|
233
|
+
m.crop '32x32+0+0'
|
234
|
+
m.repage!
|
235
|
+
m.file out_image
|
236
|
+
end
|
237
|
+
retval = MojoMagick.get_image_size(out_image)
|
238
|
+
assert_equal 32, retval[:width]
|
239
|
+
assert_equal 32, retval[:height]
|
240
|
+
|
241
|
+
# Convert test, using file shortcuts
|
242
|
+
MojoMagick.convert(test_image, out_image) do |c|
|
243
|
+
c.crop '100x100+0+0'
|
244
|
+
c.repage!
|
245
|
+
end
|
246
|
+
retval = MojoMagick.get_image_size(out_image)
|
247
|
+
assert_equal 100, retval[:width]
|
248
|
+
assert_equal 100, retval[:height]
|
249
|
+
|
250
|
+
# Mogrify test, using file shortcut
|
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
|
+
|
256
|
+
# RGB8 test
|
257
|
+
bdata = 'aaaaaabbbbbbccc'
|
258
|
+
out = 'out.png'
|
259
|
+
MojoMagick.convert do |c|
|
260
|
+
c.blob bdata, format: :rgb, depth: 8, size: '5x1'
|
261
|
+
c.file out
|
262
|
+
end
|
263
|
+
r = MojoMagick.get_image_size(out)
|
264
|
+
assert r[:height] == 1
|
265
|
+
assert r[:width] == 5
|
266
|
+
|
267
|
+
bdata = '1111222233334444'
|
268
|
+
out = 'out.png'
|
269
|
+
MojoMagick.convert do |c|
|
270
|
+
c.blob bdata, format: :rgba, depth: 8, size: '4x1'
|
271
|
+
c.file out
|
272
|
+
end
|
273
|
+
r = MojoMagick.get_image_size(out)
|
274
|
+
assert r[:height] == 1
|
275
|
+
assert r[:width] == 4
|
276
|
+
end
|
277
|
+
end
|