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.
@@ -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
@@ -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::tempfile(data, opts)
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
- @opts << "+#{command.to_s.chop}"
60
- else
61
- @opts << "-#{command}"
62
- end
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
- [ '"', arg.gsub('"', '\"').gsub("'", "\'"), '"'].join
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 (begin; line = enumerator.next; rescue StopIteration; line=nil; end) do
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] = {:name => 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(%r{[a-z]+$}) || []
42
- value = limits[i].match(%r{^[0-9]+})
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
- when 'eb'
46
- scaled_value = unscaled_value * (2 ** 60)
47
- when 'pb'
48
- scaled_value = unscaled_value * (2 ** 50)
49
- when 'tb'
50
- scaled_value = unscaled_value * (2 ** 40)
51
- when 'gb'
52
- scaled_value = unscaled_value * (2 ** 30)
53
- when 'mb'
54
- scaled_value = unscaled_value * (2 ** 20)
55
- when 'kb'
56
- scaled_value = unscaled_value * (2 ** 10)
57
- when 'b'
58
- scaled_value = unscaled_value
59
- else
60
- scaled_value = unscaled_value
61
- end
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
@@ -1,3 +1,3 @@
1
1
  module MojoMagick
2
- VERSION = '0.5.2'
2
+ VERSION = '0.5.7'.freeze
3
3
  end
@@ -1,36 +1,38 @@
1
- $:.push File.expand_path("../lib", __FILE__)
2
- require "mojo_magick/version"
1
+ $:.push File.expand_path('lib', __dir__)
2
+ require 'mojo_magick/version'
3
3
 
4
- post_install_message = <<EOF
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 = "mojo_magick"
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 = ["Steve Midgley", "Elliot Nelson", "Jon Rogers"]
20
- s.email = ["public@misuse.org", "jon@rcode5.com"]
21
- s.homepage = "http://github.com/rcode5/mojo_magick"
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 = %q{Simple Ruby stateless module interface to imagemagick.}
24
+ s.description = 'Simple Ruby stateless module interface to imagemagick.'
24
25
 
25
- s.rubyforge_project = "mojo_magick"
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 = ["lib"]
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
@@ -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
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::Unit::TestCase
20
-
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)
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, 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 < 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::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,277 @@
1
- require File::join(File::dirname(__FILE__), 'test_helper')
2
-
3
- class MojoMagickTest < Test::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
- assert_not_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_raise(MojoMagick::MojoFailed) {MojoMagick::get_image_size(bad_image)}
123
- assert_raise(MojoMagick::MojoFailed) {MojoMagick::get_image_size(zero_image)}
124
- assert_raise(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 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