mojo_magick 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module MojoMagick
2
+ CommandStatus = Struct.new(:command, :return_value, :error, :system_status) do
3
+ def success?
4
+ system_status.success?
5
+ end
6
+
7
+ def exit_code
8
+ system_status.exitstatus || "unknown"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module MojoMagick
2
+ class MojoMagickException < StandardError; end
3
+ class MojoError < MojoMagickException; end
4
+ class MojoFailed < MojoMagickException; end
5
+ end
@@ -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
- !(name.nil?)
6
+ !name.nil?
9
7
  end
10
8
 
11
9
  def initialize(property_hash = {})
12
10
  property_hash.symbolize_keys!
13
- [:name, :family, :style, :stretch, :weight, :glyphs].each do |f|
11
+ %i[name family style stretch weight glyphs].each do |f|
14
12
  setter = "#{f}="
15
- self.send(setter, property_hash[f])
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
- args.each do |arg|
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 << '-annotate'
31
+ @opts << "-annotate"
34
32
  arguments = args.join.split
35
- if arguments.length == 1
36
- arguments.unshift '0'
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 << '-format'
46
- args.each do |arg|
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::tempfile(data, opts)
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
- @opts << "+#{command.to_s.chop}"
72
- else
73
- @opts << "-#{command}"
74
- end
75
- args.each do |arg|
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
- @opts.join ' '
72
+ to_a.join " "
83
73
  end
84
74
 
85
- protected
86
- def add_formatted(arg)
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 arg =~ /[#'<>^|&();` ]/
93
- [ '"', arg.gsub('"', '\"').gsub("'", "\'"), '"'].join
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 (begin; line = enumerator.next; rescue StopIteration; line=nil; end) do
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
- if m = /^\s*Font:\s+(.*)$/.match(line)
14
+ m = /^\s*Font:\s+(.*)$/.match(line)
15
+ if m
15
16
  name = m[1].strip
16
- fonts[name] = {: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
@@ -1,3 +1,3 @@
1
1
  module MojoMagick
2
- VERSION = '0.5.3'
2
+ VERSION = "0.6.0"
3
3
  end
@@ -1,37 +1,42 @@
1
- $:.push File.expand_path("../lib", __FILE__)
1
+ $LOAD_PATH.push File.expand_path("lib", __dir__)
2
2
  require "mojo_magick/version"
3
3
 
4
- post_install_message = <<EOF
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
- EOF
13
+ EOPOST_INSTALL
14
14
 
15
15
  Gem::Specification.new do |s|
16
- s.name = "mojo_magick"
17
- s.license = 'MIT'
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 = ["public@misuse.org", "jon@rcode5.com"]
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 = %q{Simple Ruby stateless module interface to imagemagick.}
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('rake')
33
- s.add_development_dependency('simplecov')
34
- s.add_development_dependency('rspec-expectations')
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
@@ -1,35 +1,29 @@
1
-
2
- require File::join(File::dirname(__FILE__), 'test_helper')
3
-
4
- IDENTIFY_FONT_RESPONSE =<<EOF
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
- Font: Zapfino
12
- family: Zapfino
13
- style: Italic
14
- stretch: Normal
15
- weight: 400
16
- glyphs: /Library/Fonts/Zapfino.ttf
17
- EOF
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
- assert_equal f.name, nil
21
+ assert_nil f.name
24
22
  assert_equal f.valid?, false
25
23
 
26
- f = MojoMagick::Font.new(:name => "Zapfino", :weight => 400)
27
- assert_equal f.name, 'Zapfino'
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, 400
30
-
27
+ assert_equal f.weight, 400
31
28
  end
32
-
33
29
  end
34
-
35
-
@@ -1,12 +1,11 @@
1
- require File::join(File::dirname(__FILE__), 'test_helper')
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::get_fonts
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 (fonts.first.name.is_a? String)
9
+ assert(fonts.first.name.is_a?(String))
11
10
  end
12
11
  end
@@ -1,256 +1,282 @@
1
- require File::join(File::dirname(__FILE__), 'test_helper')
2
-
3
- class MojoMagickTest < MiniTest::Unit::TestCase
4
-
5
- # we keep a fixtures path and a working path so that we can easily test image
6
- # manipulation routines without tainting the original images
7
- def setup
8
- @fixtures_path = File.expand_path(File::join(File.dirname(__FILE__), 'fixtures'))
9
- @working_path = File::join(@fixtures_path, 'tmp')
10
- end
11
-
12
- def reset_images
13
- FileUtils::rm_r(@working_path) if File::exists?(@working_path)
14
- FileUtils::mkdir(@working_path)
15
- Dir::glob(File::join(@fixtures_path, '*')).each do |file|
16
- FileUtils::cp(file, @working_path) if File::file?(file)
17
- end
18
- @test_image = File::join(@working_path, '5742.jpg')
19
-
20
- end
21
-
22
- def test_get_image_size
23
- reset_images
24
- orig_image_size = File::size(@test_image)
25
- retval = MojoMagick::get_image_size(@test_image)
26
- assert_equal orig_image_size, File::size(@test_image)
27
- assert_equal 500, retval[:height]
28
- assert_equal 333, retval[:width]
29
- end
30
-
31
- def test_image_resize
32
- # test basic resizing
33
- reset_images
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=>100, :height=>100})
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 100, new_dimensions[:height]
51
- assert_equal 67, new_dimensions[:width]
52
- end
53
-
54
- def test_image_resize_with_percentage
55
- reset_images
56
- original_size = MojoMagick::get_image_size(@test_image)
57
- retval = MojoMagick::resize(@test_image, @test_image, {:percent => 50})
58
- assert_equal @test_image, retval
59
- new_dimensions = MojoMagick::get_image_size(@test_image)
60
- [:height, :width].each do |dim|
61
- assert_equal (original_size[dim]/2.0).ceil, new_dimensions[dim]
62
- end
63
- end
64
-
65
- def test_shrink_with_big_dimensions
66
- # image shouldn't resize if we specify very large dimensions and specify "shrink_only"
67
- reset_images
68
- size_test_temp = Tempfile::new('mojo_test')
69
- size_test = size_test_temp.path
70
- retval = MojoMagick::shrink(@test_image, size_test, {:width=>1000, :height=>1000})
71
- assert_equal size_test, retval
72
- new_dimensions = MojoMagick::get_image_size(@test_image)
73
- assert_equal 500, new_dimensions[:height]
74
- assert_equal 333, new_dimensions[:width]
75
- end
76
-
77
- def test_shrink
78
- reset_images
79
- # image should resize if we specify small dimensions and shrink_only
80
- retval = MojoMagick::shrink(@test_image, @test_image, {:width=>1000, :height=>100})
81
- assert_equal @test_image, retval
82
- new_dimensions = MojoMagick::get_image_size(@test_image)
83
- assert_equal 100, new_dimensions[:height]
84
- assert_equal 67, new_dimensions[:width]
85
- end
86
-
87
- def test_resize_with_shrink_only_options
88
- reset_images
89
- # image should resize if we specify small dimensions and shrink_only
90
- retval = MojoMagick::resize(@test_image, @test_image, {:shrink_only => true, :width=>400, :height=>400})
91
- assert_equal @test_image, retval
92
- new_dimensions = MojoMagick::get_image_size(@test_image)
93
- assert_equal 400, new_dimensions[:height]
94
- assert_equal 266, new_dimensions[:width]
95
- end
96
-
97
- def test_expand_with_small_dim
98
- # image shouldn't resize if we specify small dimensions and expand_only
99
- reset_images
100
- orig_image_size = File::size(@test_image)
101
- retval = MojoMagick::expand(@test_image, @test_image, {:width=>10, :height=>10})
102
- assert_equal @test_image, retval
103
- new_dimensions = MojoMagick::get_image_size(@test_image)
104
- assert_equal 500, new_dimensions[:height]
105
- assert_equal 333, new_dimensions[:width]
106
- end
107
-
108
- def test_expand
109
- reset_images
110
- # image should resize if we specify large dimensions and expand_only
111
- retval = MojoMagick::expand(@test_image, @test_image, {:width=>1000, :height=>1000})
112
- assert_equal @test_image, retval
113
- new_dimensions = MojoMagick::get_image_size(@test_image)
114
- assert_equal 1000, new_dimensions[:height]
115
- assert_equal 666, new_dimensions[:width]
116
- end
117
-
118
- def test_invalid_images
119
- # test bad images
120
- bad_image = File::join(@working_path, 'not_an_image.jpg')
121
- zero_image = File::join(@working_path, 'zero_byte_image.jpg')
122
- assert_raises(MojoMagick::MojoFailed) {MojoMagick::get_image_size(bad_image)}
123
- assert_raises(MojoMagick::MojoFailed) {MojoMagick::get_image_size(zero_image)}
124
- assert_raises(MojoMagick::MojoFailed) {MojoMagick::get_image_size('/file_does_not_exist_here_ok.jpg')}
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_command_helpers
194
- reset_images
195
- test_image = File::join(@working_path, '5742.jpg')
196
- out_image = File::join(@working_path, 'out1.jpg')
197
-
198
- # Simple convert test
199
- MojoMagick::convert do |c|
200
- c.file test_image
201
- c.crop '92x64+0+0'
202
- c.repage!
203
- c.file out_image
204
- end
205
- retval = MojoMagick::get_image_size(out_image)
206
- assert_equal 92, retval[:width]
207
- assert_equal 64, retval[:height]
208
-
209
- # Simple mogrify test
210
- MojoMagick::mogrify do |m|
211
- m.crop '32x32+0+0'
212
- m.repage!
213
- m.file out_image
214
- end
215
- retval = MojoMagick::get_image_size(out_image)
216
- assert_equal 32, retval[:width]
217
- assert_equal 32, retval[:height]
218
-
219
- # Convert test, using file shortcuts
220
- MojoMagick::convert(test_image, out_image) do |c|
221
- c.crop '100x100+0+0'
222
- c.repage!
223
- end
224
- retval = MojoMagick::get_image_size(out_image)
225
- assert_equal 100, retval[:width]
226
- assert_equal 100, retval[:height]
227
-
228
- # Mogrify test, using file shortcut
229
- MojoMagick::mogrify(out_image) { |m| m.shave('25x25').repage! }
230
- retval = MojoMagick::get_image_size(out_image)
231
- assert_equal 50, retval[:width]
232
- assert_equal 50, retval[:height]
233
-
234
- # RGB8 test
235
- bdata = 'aaaaaabbbbbbccc'
236
- out = 'out.png'
237
- MojoMagick::convert do |c|
238
- c.blob bdata, :format => :rgb, :depth => 8, :size => '5x1'
239
- c.file out
240
- end
241
- r = MojoMagick::get_image_size(out)
242
- assert r[:height] == 1
243
- assert r[:width] == 5
244
-
245
- bdata = '1111222233334444'
246
- out = 'out.png'
247
- MojoMagick::convert do |c|
248
- c.blob bdata, :format => :rgba, :depth => 8, :size => '4x1'
249
- c.file out
250
- end
251
- r = MojoMagick::get_image_size(out)
252
- assert r[:height] == 1
253
- assert r[:width] == 4
254
-
255
- end
256
- end
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