mojo_magick 0.5.3 → 0.6.0
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 +24 -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 +10 -0
- data/.ruby-version +1 -1
- data/Gemfile +6 -1
- data/Gemfile.lock +58 -10
- data/LICENSE.txt +1 -1
- data/README.md +50 -2
- data/Rakefile +15 -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 +6 -6
- data/lib/initializers/hash.rb +5 -1
- data/lib/mojo_magick.rb +191 -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 +23 -32
- data/lib/mojo_magick/util/parser.rb +7 -48
- data/lib/mojo_magick/version.rb +1 -1
- data/mojo_magick.gemspec +21 -16
- data/test/fixtures/roll with it.jpg +0 -0
- data/test/font_test.rb +22 -28
- data/test/fonts_test.rb +4 -5
- data/test/mojo_magick_test.rb +282 -256
- data/test/opt_builder_test.rb +46 -49
- data/test/parser_test.rb +17 -18
- data/test/test_helper.rb +7 -5
- metadata +87 -25
- data/lib/image_magick/resource_limits.rb +0 -94
- data/lib/image_resources.rb +0 -0
- data/test/resource_limits_test.rb +0 -51
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
|
@@ -17,9 +17,7 @@ module MojoMagick
|
|
17
17
|
|
18
18
|
# Add files to command line, formatted if necessary
|
19
19
|
def file(*args)
|
20
|
-
|
21
|
-
add_formatted arg
|
22
|
-
end
|
20
|
+
@opts << args
|
23
21
|
self
|
24
22
|
end
|
25
23
|
alias files file
|
@@ -30,31 +28,25 @@ module MojoMagick
|
|
30
28
|
|
31
29
|
# annotate takes non-standard args
|
32
30
|
def annotate(*args)
|
33
|
-
@opts <<
|
31
|
+
@opts << "-annotate"
|
34
32
|
arguments = args.join.split
|
35
|
-
if arguments.length == 1
|
36
|
-
|
37
|
-
end
|
38
|
-
arguments.each do |arg|
|
39
|
-
add_formatted arg
|
40
|
-
end
|
33
|
+
arguments.unshift "0" if arguments.length == 1
|
34
|
+
@opts << arguments
|
41
35
|
end
|
42
36
|
|
43
37
|
# Create a temporary file for the given image and add to command line
|
44
38
|
def format(*args)
|
45
|
-
@opts <<
|
46
|
-
|
47
|
-
add_formatted arg
|
48
|
-
end
|
39
|
+
@opts << "-format"
|
40
|
+
@opts << args
|
49
41
|
end
|
50
42
|
|
51
43
|
def blob(*args)
|
52
44
|
data = args[0]
|
53
45
|
opts = args[1] || {}
|
54
|
-
opts.each do |k,v|
|
55
|
-
send(k.to_s,v.to_s)
|
46
|
+
opts.each do |k, v|
|
47
|
+
send(k.to_s, v.to_s)
|
56
48
|
end
|
57
|
-
tmpfile = MojoMagick
|
49
|
+
tmpfile = MojoMagick.tempfile(data, opts)
|
58
50
|
file tmpfile
|
59
51
|
end
|
60
52
|
|
@@ -67,30 +59,29 @@ module MojoMagick
|
|
67
59
|
|
68
60
|
# Generic commands. Arguments will be formatted if necessary
|
69
61
|
def method_missing(command, *args)
|
70
|
-
if command.to_s[-1, 1] ==
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
add_formatted arg
|
77
|
-
end
|
62
|
+
@opts << if command.to_s[-1, 1] == "!"
|
63
|
+
"+#{command.to_s.chop}"
|
64
|
+
else
|
65
|
+
"-#{command}"
|
66
|
+
end
|
67
|
+
@opts << args
|
78
68
|
self
|
79
69
|
end
|
80
70
|
|
81
71
|
def to_s
|
82
|
-
|
72
|
+
to_a.join " "
|
83
73
|
end
|
84
74
|
|
85
|
-
|
86
|
-
|
87
|
-
# Quote anything that would cause problems on *nix or windows
|
88
|
-
@opts << quoted_arg(arg)
|
75
|
+
def to_a
|
76
|
+
@opts.flatten
|
89
77
|
end
|
90
78
|
|
79
|
+
protected
|
80
|
+
|
91
81
|
def quoted_arg(arg)
|
92
|
-
return arg unless
|
93
|
-
|
82
|
+
return arg unless /[#'<>^|&();` ]/.match?(arg)
|
83
|
+
|
84
|
+
['"', arg.gsub('"', '\"').tr("'", "\'"), '"'].join
|
94
85
|
end
|
95
86
|
end
|
96
87
|
end
|
@@ -1,69 +1,28 @@
|
|
1
|
+
# rubocop:disable Lint/AssignmentInCondition
|
1
2
|
module MojoMagick
|
2
3
|
module Util
|
3
4
|
class Parser
|
4
5
|
# handle parsing outputs from ImageMagick commands
|
5
6
|
|
6
7
|
def parse_fonts(raw_fonts)
|
7
|
-
font = nil
|
8
8
|
fonts = {}
|
9
9
|
enumerator = raw_fonts.split(/\n/).each
|
10
10
|
name = nil
|
11
|
-
while
|
11
|
+
while begin; line = enumerator.next; rescue StopIteration; line = nil; end
|
12
12
|
line.chomp!
|
13
13
|
line = enumerator.next if line.nil? || line.empty? || (/^\s+$/ =~ line)
|
14
|
-
|
14
|
+
m = /^\s*Font:\s+(.*)$/.match(line)
|
15
|
+
if m
|
15
16
|
name = m[1].strip
|
16
|
-
fonts[name] = {:name
|
17
|
+
fonts[name] = { name: name }
|
17
18
|
else
|
18
19
|
key_val = line.split(/:/).map(&:strip)
|
19
20
|
k = key_val[0].downcase.to_sym
|
20
21
|
v = key_val[1]
|
21
|
-
if k && name
|
22
|
-
fonts[name][k] = key_val[1]
|
23
|
-
end
|
22
|
+
fonts[name][k] = v if k && name
|
24
23
|
end
|
25
24
|
end
|
26
|
-
fonts.values.map{|f| MojoMagick::Font.new f}
|
27
|
-
end
|
28
|
-
|
29
|
-
def parse_limits(raw_limits)
|
30
|
-
|
31
|
-
row_limits = raw_limits.split("\n")
|
32
|
-
header = row_limits[0].chomp
|
33
|
-
data = row_limits[2].chomp
|
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]
|
25
|
+
fonts.values.map { |f| MojoMagick::Font.new f }
|
67
26
|
end
|
68
27
|
end
|
69
28
|
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
|
-
s.email = ["
|
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("bundler")
|
34
|
+
s.add_development_dependency("bundle-audit")
|
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
|
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/fonts_test.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class FontsTest < MiniTest::Unit::TestCase
|
1
|
+
require_relative "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,282 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class MojoMagickTest < MiniTest::
|
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
|
-
assert_raises(MojoMagick::MojoFailed)
|
123
|
-
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_resize_with_fill
|
128
|
-
reset_images
|
129
|
-
@test_image = File
|
130
|
-
MojoMagick
|
131
|
-
dim = MojoMagick
|
132
|
-
assert_equal 100, dim[:width]
|
133
|
-
assert_equal 150, dim[:height]
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_resize_with_fill_and_crop
|
137
|
-
reset_images
|
138
|
-
@test_image = File
|
139
|
-
MojoMagick
|
140
|
-
dim = MojoMagick
|
141
|
-
assert_equal 150, dim[:width]
|
142
|
-
assert_equal 120, dim[:height]
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_tempfile
|
146
|
-
# Create a tempfile and return the path
|
147
|
-
filename = MojoMagick
|
148
|
-
File.open(filename,
|
149
|
-
assert_equal f.read,
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_label
|
154
|
-
reset_images
|
155
|
-
out_image = File
|
156
|
-
|
157
|
-
MojoMagick
|
158
|
-
c.label
|
159
|
-
c.file out_image
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_label_with_quote
|
164
|
-
reset_images
|
165
|
-
out_image = File
|
166
|
-
|
167
|
-
MojoMagick
|
168
|
-
c.label 'rock "the house'
|
169
|
-
c.file out_image
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
def test_label_with_apostrophe
|
174
|
-
reset_images
|
175
|
-
out_image = File
|
176
|
-
|
177
|
-
MojoMagick
|
178
|
-
c.label
|
179
|
-
c.file out_image
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_label_with_quotes
|
184
|
-
reset_images
|
185
|
-
out_image = File
|
186
|
-
|
187
|
-
MojoMagick
|
188
|
-
c.label 'this is "it!"'
|
189
|
-
c.file out_image
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
def
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
end
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
MojoMagick
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
MojoMagick
|
248
|
-
c.
|
249
|
-
c.
|
250
|
-
end
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
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
|
+
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
|
+
_orig_image_size = File.size(@test_image)
|
99
|
+
retval = MojoMagick.expand(@test_image, @test_image, { width: 10, height: 10 })
|
100
|
+
assert_equal @test_image, retval
|
101
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
102
|
+
assert_equal 500, new_dimensions[:height]
|
103
|
+
assert_equal 333, new_dimensions[:width]
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_expand
|
107
|
+
reset_images
|
108
|
+
# image should resize if we specify large dimensions and expand_only
|
109
|
+
retval = MojoMagick.expand(@test_image, @test_image, { width: 1000, height: 1000 })
|
110
|
+
assert_equal @test_image, retval
|
111
|
+
new_dimensions = MojoMagick.get_image_size(@test_image)
|
112
|
+
assert_equal 1000, new_dimensions[:height]
|
113
|
+
assert_equal 666, new_dimensions[:width]
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_invalid_images
|
117
|
+
# test bad images
|
118
|
+
bad_image = File.join(@working_path, "not_an_image.jpg")
|
119
|
+
zero_image = File.join(@working_path, "zero_byte_image.jpg")
|
120
|
+
assert_raises(MojoMagick::MojoFailed) { MojoMagick.get_image_size(bad_image) }
|
121
|
+
assert_raises(MojoMagick::MojoFailed) { MojoMagick.get_image_size(zero_image) }
|
122
|
+
assert_raises(MojoMagick::MojoFailed) do
|
123
|
+
MojoMagick.get_image_size("/file_does_not_exist_here_ok.jpg")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_resize_with_fill
|
128
|
+
reset_images
|
129
|
+
@test_image = File.join(@working_path, "5742.jpg")
|
130
|
+
MojoMagick.resize(@test_image, @test_image, { fill: true, width: 100, height: 100 })
|
131
|
+
dim = MojoMagick.get_image_size(@test_image)
|
132
|
+
assert_equal 100, dim[:width]
|
133
|
+
assert_equal 150, dim[:height]
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_resize_with_fill_and_crop
|
137
|
+
reset_images
|
138
|
+
@test_image = File.join(@working_path, "5742.jpg")
|
139
|
+
MojoMagick.resize(@test_image, @test_image, { fill: true, crop: true, width: 150, height: 120 })
|
140
|
+
dim = MojoMagick.get_image_size(@test_image)
|
141
|
+
assert_equal 150, dim[:width]
|
142
|
+
assert_equal 120, dim[:height]
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_tempfile
|
146
|
+
# Create a tempfile and return the path
|
147
|
+
filename = MojoMagick.tempfile("binary data")
|
148
|
+
File.open(filename, "rb") do |f|
|
149
|
+
assert_equal f.read, "binary data"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_label
|
154
|
+
reset_images
|
155
|
+
out_image = File.join(@working_path, "label_test.png")
|
156
|
+
|
157
|
+
MojoMagick.convert do |c|
|
158
|
+
c.label "rock the house"
|
159
|
+
c.file out_image
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_label_with_quote
|
164
|
+
reset_images
|
165
|
+
out_image = File.join(@working_path, "label_test.png")
|
166
|
+
|
167
|
+
MojoMagick.convert do |c|
|
168
|
+
c.label 'rock "the house'
|
169
|
+
c.file out_image
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_label_with_apostrophe
|
174
|
+
reset_images
|
175
|
+
out_image = File.join(@working_path, "label_test.png")
|
176
|
+
|
177
|
+
MojoMagick.convert do |c|
|
178
|
+
c.label "rock 'the house"
|
179
|
+
c.file out_image
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_label_with_quotes
|
184
|
+
reset_images
|
185
|
+
out_image = File.join(@working_path, "label_test.png")
|
186
|
+
|
187
|
+
MojoMagick.convert do |c|
|
188
|
+
c.label 'this is "it!"'
|
189
|
+
c.file out_image
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_bad_command
|
194
|
+
MojoMagick.convert do |c|
|
195
|
+
c.unknown_option "fail"
|
196
|
+
c.file "boogabooga.whatever"
|
197
|
+
end
|
198
|
+
rescue MojoMagick::MojoFailed => e
|
199
|
+
assert e.message.include?("unrecognized option"),
|
200
|
+
"Unable to find ImageMagick commandline error in the message"
|
201
|
+
assert e.message.include?("convert.c/ConvertImageCommand"),
|
202
|
+
"Unable to find ImageMagick commandline error in the message"
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_blob
|
206
|
+
reset_images
|
207
|
+
|
208
|
+
# RGB8 test
|
209
|
+
data = (Array.new(16) { [rand > 0.5 ? 0 : 255] * 3 }).flatten
|
210
|
+
bdata = data.pack "C" * data.size
|
211
|
+
out = "out.png"
|
212
|
+
MojoMagick.convert(nil, "png:#{out}") do |c|
|
213
|
+
c.blob bdata, format: :rgb, depth: 8, size: "4x4"
|
214
|
+
end
|
215
|
+
r = MojoMagick.get_image_size(out)
|
216
|
+
assert r[:height] == 4
|
217
|
+
assert r[:width] == 4
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_command_helpers
|
221
|
+
reset_images
|
222
|
+
test_image = File.join(@working_path, "5742.jpg")
|
223
|
+
out_image = File.join(@working_path, "out1.jpg")
|
224
|
+
|
225
|
+
# Simple convert test
|
226
|
+
MojoMagick.convert do |c|
|
227
|
+
c.file test_image
|
228
|
+
c.crop "92x64+0+0"
|
229
|
+
c.repage!
|
230
|
+
c.file out_image
|
231
|
+
end
|
232
|
+
retval = MojoMagick.get_image_size(out_image)
|
233
|
+
assert_equal 92, retval[:width]
|
234
|
+
assert_equal 64, retval[:height]
|
235
|
+
|
236
|
+
# Simple mogrify test
|
237
|
+
MojoMagick.mogrify do |m|
|
238
|
+
m.crop "32x32+0+0"
|
239
|
+
m.repage!
|
240
|
+
m.file out_image
|
241
|
+
end
|
242
|
+
retval = MojoMagick.get_image_size(out_image)
|
243
|
+
assert_equal 32, retval[:width]
|
244
|
+
assert_equal 32, retval[:height]
|
245
|
+
|
246
|
+
# Convert test, using file shortcuts
|
247
|
+
MojoMagick.convert(test_image, out_image) do |c|
|
248
|
+
c.crop "100x100+0+0"
|
249
|
+
c.repage!
|
250
|
+
end
|
251
|
+
retval = MojoMagick.get_image_size(out_image)
|
252
|
+
assert_equal 100, retval[:width]
|
253
|
+
assert_equal 100, retval[:height]
|
254
|
+
|
255
|
+
# Mogrify test, using file shortcut
|
256
|
+
MojoMagick.mogrify(out_image) { |m| m.shave("25x25").repage! }
|
257
|
+
retval = MojoMagick.get_image_size(out_image)
|
258
|
+
assert_equal 50, retval[:width]
|
259
|
+
assert_equal 50, retval[:height]
|
260
|
+
|
261
|
+
# RGB8 test
|
262
|
+
bdata = "aaaaaabbbbbbccc"
|
263
|
+
out = "out.png"
|
264
|
+
MojoMagick.convert do |c|
|
265
|
+
c.blob bdata, format: :rgb, depth: 8, size: "5x1"
|
266
|
+
c.file out
|
267
|
+
end
|
268
|
+
r = MojoMagick.get_image_size(out)
|
269
|
+
assert r[:height] == 1
|
270
|
+
assert r[:width] == 5
|
271
|
+
|
272
|
+
bdata = "1111222233334444"
|
273
|
+
out = "out.png"
|
274
|
+
MojoMagick.convert do |c|
|
275
|
+
c.blob bdata, format: :rgba, depth: 8, size: "4x1"
|
276
|
+
c.file out
|
277
|
+
end
|
278
|
+
r = MojoMagick.get_image_size(out)
|
279
|
+
assert r[:height] == 1
|
280
|
+
assert r[:width] == 4
|
281
|
+
end
|
282
|
+
end
|