mini_magick 2.3 → 3.2

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.3
1
+ 3.2
data/lib/mini_magick.rb CHANGED
@@ -1,11 +1,24 @@
1
1
  require 'tempfile'
2
2
  require 'subexec'
3
- require 'open-uri'
3
+ require 'pathname'
4
4
 
5
5
  module MiniMagick
6
6
  class << self
7
7
  attr_accessor :processor
8
8
  attr_accessor :timeout
9
+
10
+
11
+ # Experimental method for automatically selecting a processor
12
+ # such as gm. Only works on *nix.
13
+ #
14
+ # TODO: Write tests for this and figure out what platforms it supports
15
+ def choose_processor
16
+ if `type -P mogrify`.size > 0
17
+ return
18
+ elsif `type -P gm`.size > 0
19
+ self.processor = "gm"
20
+ end
21
+ end
9
22
  end
10
23
 
11
24
  MOGRIFY_COMMANDS = %w{adaptive-blur adaptive-resize adaptive-sharpen adjoin affine alpha annotate antialias append authenticate auto-gamma auto-level auto-orient background bench iterations bias black-threshold blue-primary point blue-shift factor blur border bordercolor brightness-contrast caption string cdl filename channel type charcoal radius chop clip clamp clip-mask filename clip-path id clone index clut contrast-stretch coalesce colorize color-matrix colors colorspace type combine comment string compose operator composite compress type contrast convolve coefficients crop cycle amount decipher filename debug events define format:option deconstruct delay delete index density depth despeckle direction type display server dispose method distort type coefficients dither method draw string edge radius emboss radius encipher filename encoding type endian type enhance equalize evaluate operator evaluate-sequence operator extent extract family name fft fill filter type flatten flip floodfill flop font name format string frame function name fuzz distance fx expression gamma gaussian-blur geometry gravity type green-primary point help identify ifft implode amount insert index intent type interlace type interline-spacing interpolate method interword-spacing kerning label string lat layers method level limit type linear-stretch liquid-rescale log format loop iterations mask filename mattecolor median radius modulate monitor monochrome morph morphology method kernel motion-blur negate noise radius normalize opaque ordered-dither NxN orient type page paint radius ping pointsize polaroid angle posterize levels precision preview type print string process image-filter profile filename quality quantizespace quiet radial-blur angle raise random-threshold low,high red-primary point regard-warnings region remap filename render repage resample resize respect-parentheses roll rotate degrees sample sampling-factor scale scene seed segments selective-blur separate sepia-tone threshold set attribute shade degrees shadow sharpen shave shear sigmoidal-contrast size sketch solarize threshold splice spread radius strip stroke strokewidth stretch type style type swap indexes swirl degrees texture filename threshold thumbnail tile filename tile-offset tint transform transparent transparent-color transpose transverse treedepth trim type type undercolor unique-colors units type unsharp verbose version view vignette virtual-pixel method wave weight type white-point point white-threshold write filename}
@@ -55,9 +68,7 @@ module MiniMagick
55
68
  #
56
69
  # Extension is either guessed from the path or you can specify it as a second parameter.
57
70
  #
58
- # If you pass in what looks like a URL, we will see if Kernel#open exists. If it doesn't
59
- # then we require 'open-uri'. That way, if you have a work-alike library, we won't demolish it.
60
- # Open-uri never gets required unless you pass in something with "://" in it.
71
+ # If you pass in what looks like a URL, we require 'open-uri' before opening it.
61
72
  #
62
73
  # @param file_or_url [String] Either a local file path or a URL that open-uri can read
63
74
  # @param ext [String] Specify the extension you want to read it as
@@ -65,9 +76,7 @@ module MiniMagick
65
76
  def open(file_or_url, ext = File.extname(file_or_url))
66
77
  file_or_url = file_or_url.to_s # Force it to be a String... hell or highwater
67
78
  if file_or_url.include?("://")
68
- if !Kernel.respond_to?("open")
69
- require 'open-uri'
70
- end
79
+ require 'open-uri'
71
80
  self.read(Kernel::open(file_or_url), ext)
72
81
  else
73
82
  File.open(file_or_url, "rb") do |f|
@@ -111,7 +120,7 @@ module MiniMagick
111
120
 
112
121
  # Create a new MiniMagick::Image object
113
122
  #
114
- # _DANGER_: The file location passed in here is the *working copy*. That is, it gets *modified*.
123
+ # _DANGER_: The file location passed in here is the *working copy*. That is, it gets *modified*.
115
124
  # you can either copy it yourself or use the MiniMagick::Image.open(path) method which creates a
116
125
  # temporary file for you and protects your original!
117
126
  #
@@ -121,7 +130,10 @@ module MiniMagick
121
130
  @path = input_path
122
131
  @tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected.
123
132
  end
124
-
133
+
134
+ def escaped_path
135
+ Pathname.new(@path).to_s.gsub(" ", "\\ ")
136
+ end
125
137
 
126
138
  # Checks to make sure that MiniMagick can read the file and understand it.
127
139
  #
@@ -144,6 +156,7 @@ module MiniMagick
144
156
  # image["format"] #=> "TIFF"
145
157
  # image["height"] #=> 41 (pixels)
146
158
  # image["width"] #=> 50 (pixels)
159
+ # image["colorspace"] #=> "DirectClassRGB"
147
160
  # image["dimensions"] #=> [50, 41]
148
161
  # image["size"] #=> 2050 (bits)
149
162
  # image["original_at"] #=> 2005-02-23 23:17:24 +0000 (Read from Exif data)
@@ -155,28 +168,30 @@ module MiniMagick
155
168
  def [](value)
156
169
  # Why do I go to the trouble of putting in newlines? Because otherwise animated gifs screw everything up
157
170
  case value.to_s
171
+ when "colorspace"
172
+ run_command("identify", "-format", format_option("%r"), escaped_path).split("\n")[0]
158
173
  when "format"
159
- run_command("identify", "-format", format_option("%m"), @path).split("\n")[0]
174
+ run_command("identify", "-format", format_option("%m"), escaped_path).split("\n")[0]
160
175
  when "height"
161
- run_command("identify", "-format", format_option("%h"), @path).split("\n")[0].to_i
176
+ run_command("identify", "-format", format_option("%h"), escaped_path).split("\n")[0].to_i
162
177
  when "width"
163
- run_command("identify", "-format", format_option("%w"), @path).split("\n")[0].to_i
178
+ run_command("identify", "-format", format_option("%w"), escaped_path).split("\n")[0].to_i
164
179
  when "dimensions"
165
- run_command("identify", "-format", format_option("%w %h"), @path).split("\n")[0].split.map{|v|v.to_i}
180
+ run_command("identify", "-format", format_option("%w %h"), escaped_path).split("\n")[0].split.map{|v|v.to_i}
166
181
  when "size"
167
182
  File.size(@path) # Do this because calling identify -format "%b" on an animated gif fails!
168
183
  when "original_at"
169
184
  # Get the EXIF original capture as a Time object
170
185
  Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil
171
186
  when /^EXIF\:/i
172
- result = run_command('identify', '-format', "\"%[#{value}]\"", @path).chop
187
+ result = run_command('identify', '-format', "\"%[#{value}]\"", escaped_path).chop
173
188
  if result.include?(",")
174
189
  read_character_data(result)
175
190
  else
176
191
  result
177
192
  end
178
193
  else
179
- run_command('identify', '-format', "\"#{value}\"", @path).split("\n")[0]
194
+ run_command('identify', '-format', "\"#{value}\"", escaped_path).split("\n")[0]
180
195
  end
181
196
  end
182
197
 
@@ -186,7 +201,7 @@ module MiniMagick
186
201
  #
187
202
  # @return [String] Whatever the result from the command line is. May not be terribly useful.
188
203
  def <<(*args)
189
- run_command("mogrify", *args << @path)
204
+ run_command("mogrify", *args << escaped_path)
190
205
  end
191
206
 
192
207
  # This is used to change the format of the image. That is, from "tiff to jpg" or something like that.
@@ -204,7 +219,10 @@ module MiniMagick
204
219
  # @param page [Integer] If this is an animated gif, say which 'page' you want with an integer. Leave as default if you don't care.
205
220
  # @return [nil]
206
221
  def format(format, page = 0)
207
- run_command("mogrify", "-format", format, @path)
222
+ c = CommandBuilder.new('mogrify', '-format', format)
223
+ yield c if block_given?
224
+ c << @path
225
+ run(c)
208
226
 
209
227
  old_path = @path.dup
210
228
  @path.sub!(/(\.\w*)?$/, ".#{format}")
@@ -214,7 +232,7 @@ module MiniMagick
214
232
  begin
215
233
  FileUtils.copy_file(@path.sub(".#{format}", "-#{page}.#{format}"), @path)
216
234
  rescue => ex
217
- raise MiniMagickError, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
235
+ raise MiniMagick::Error, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
218
236
  end
219
237
  end
220
238
  ensure
@@ -229,10 +247,27 @@ module MiniMagick
229
247
  run_command("mogrify", "-quality", "100", "#{path}[0]")
230
248
  end
231
249
 
250
+ # Writes the temporary file out to either a file location (by passing in a String) or by
251
+ # passing in a Stream that you can #write(chunk) to repeatedly
252
+ #
253
+ # @param output_to [IOStream, String] Some kind of stream object that needs to be read or a file path as a String
254
+ # @return [IOStream, Boolean] If you pass in a file location [String] then you get a success boolean. If its a stream, you get it back.
232
255
  # Writes the temporary image that we are using for processing to the output path
233
- def write(output_path)
234
- FileUtils.copy_file @path, output_path
235
- run_command "identify", output_path # Verify that we have a good image
256
+ def write(output_to)
257
+ if output_to.kind_of?(String) || !output_to.respond_to?(:write)
258
+ FileUtils.copy_file @path, output_to
259
+ # We need to escape the output path if it contains a space
260
+ escaped_output_to = output_to.to_s.gsub(' ', '\\ ')
261
+ run_command "identify", escaped_output_to # Verify that we have a good image
262
+ else # stream
263
+ File.open(@path, "rb") do |f|
264
+ f.binmode
265
+ while chunk = f.read(8192)
266
+ output_to.write(chunk)
267
+ end
268
+ end
269
+ output_to
270
+ end
236
271
  end
237
272
 
238
273
  # Gives you raw image data back
@@ -255,14 +290,14 @@ module MiniMagick
255
290
 
256
291
  # You can use multiple commands together using this method. Very easy to use!
257
292
  #
258
- # @example
293
+ # @example
259
294
  # image.combine_options do |c|
260
295
  # c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
261
296
  # c.thumbnail "300x500>"
262
297
  # c.background background
263
298
  # end
264
299
  #
265
- # @yieldparam command [CommandBuilder]
300
+ # @yieldparam command [CommandBuilder]
266
301
  def combine_options(&block)
267
302
  c = CommandBuilder.new('mogrify')
268
303
  block.call(c)
@@ -272,7 +307,7 @@ module MiniMagick
272
307
 
273
308
  # Check to see if we are running on win32 -- we need to escape things differently
274
309
  def windows?
275
- !(RUBY_PLATFORM =~ /win32/).nil?
310
+ !(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil?
276
311
  end
277
312
 
278
313
  def composite(other_image, output_extension = 'jpg', &block)
@@ -299,6 +334,11 @@ module MiniMagick
299
334
  end
300
335
 
301
336
  def run_command(command, *args)
337
+ # -ping "efficiently determine image characteristics."
338
+ if command == 'identify'
339
+ args.unshift '-ping'
340
+ end
341
+
302
342
  run(CommandBuilder.new(command, *args))
303
343
  end
304
344
 
@@ -362,15 +402,27 @@ module MiniMagick
362
402
  raise Error, "You must call 'format' on the image object directly!"
363
403
  elsif MOGRIFY_COMMANDS.include?(guessed_command_name)
364
404
  add(guessed_command_name, *options)
405
+ self
365
406
  else
366
407
  super(symbol, *args)
367
408
  end
368
409
  end
369
410
 
411
+ def +(*options)
412
+ push(@args.pop.gsub /^-/, '+')
413
+ if options.any?
414
+ options.each do |o|
415
+ push "\"#{ o }\""
416
+ end
417
+ end
418
+ end
419
+
370
420
  def add(command, *options)
371
421
  push "-#{command}"
372
422
  if options.any?
373
- push "\"#{options.join(" ")}\""
423
+ options.each do |o|
424
+ push "\"#{ o }\""
425
+ end
374
426
  end
375
427
  end
376
428
 
@@ -378,11 +430,5 @@ module MiniMagick
378
430
  @args << arg.to_s.strip
379
431
  end
380
432
  alias :<< :push
381
-
382
- # @deprecated Please don't use the + method its has been deprecated
383
- def +(value)
384
- warn "Warning: The MiniMagick::ComandBuilder#+ command has been deprecated. Please use c << '+#{value}' instead"
385
- push "+#{value}"
386
- end
387
433
  end
388
434
  end
@@ -14,10 +14,16 @@ class CommandBuilderTest < Test::Unit::TestCase
14
14
  def test_complicated
15
15
  c = CommandBuilder.new("test")
16
16
  c.resize "30x40"
17
- c.alpha 1, 3, 4
17
+ c.alpha "1 3 4"
18
18
  c.resize "mome fingo"
19
19
  assert_equal "-resize \"30x40\" -alpha \"1 3 4\" -resize \"mome fingo\"", c.args.join(" ")
20
20
  end
21
+
22
+ def test_plus_modifier_and_multiple_options
23
+ c = CommandBuilder.new("test")
24
+ c.distort.+ 'srt', '0.6 20'
25
+ assert_equal "+distort \"srt\" \"0.6 20\"", c.args.join(" ")
26
+ end
21
27
 
22
28
  def test_valid_command
23
29
  begin
data/test/image_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
- require 'stringio'
4
3
  require 'pathname'
4
+ require 'stringio'
5
5
  require File.expand_path('../../lib/mini_magick', __FILE__)
6
6
 
7
7
  #MiniMagick.processor = :gm
@@ -13,7 +13,7 @@ class ImageTest < Test::Unit::TestCase
13
13
 
14
14
  SIMPLE_IMAGE_PATH = CURRENT_DIR + "simple.gif"
15
15
  MINUS_IMAGE_PATH = CURRENT_DIR + "simple-minus.gif"
16
- TIFF_IMAGE_PATH = CURRENT_DIR + "leaves.tiff"
16
+ TIFF_IMAGE_PATH = CURRENT_DIR + "leaves spaced.tiff"
17
17
  NOT_AN_IMAGE_PATH = CURRENT_DIR + "not_an_image.php"
18
18
  GIF_WITH_JPG_EXT = CURRENT_DIR + "actually_a_gif.jpg"
19
19
  EXIF_IMAGE_PATH = CURRENT_DIR + "trogdor.jpg"
@@ -50,7 +50,7 @@ class ImageTest < Test::Unit::TestCase
50
50
  image = Image.new(SIMPLE_IMAGE_PATH)
51
51
  image.destroy!
52
52
  end
53
-
53
+
54
54
  def test_remote_image
55
55
  image = Image.open("http://www.google.com/images/logos/logo.png")
56
56
  image.valid?
@@ -70,6 +70,28 @@ class ImageTest < Test::Unit::TestCase
70
70
  image.destroy!
71
71
  end
72
72
 
73
+ def test_image_write_with_space_in_output_path
74
+ output_path = "test output.gif"
75
+ begin
76
+ image = Image.new(SIMPLE_IMAGE_PATH)
77
+ image.write output_path
78
+
79
+ assert File.exists?(output_path)
80
+ ensure
81
+ File.delete output_path
82
+ end
83
+ image.destroy!
84
+ end
85
+
86
+ def test_image_write_with_stream
87
+ stream = StringIO.new
88
+ image = Image.open(SIMPLE_IMAGE_PATH)
89
+ image.write("/tmp/foo.gif")
90
+ image.write(stream)
91
+ # assert Image.read(stream.string).valid?
92
+ image.destroy!
93
+ end
94
+
73
95
  def test_not_an_image
74
96
  image = Image.new(NOT_AN_IMAGE_PATH)
75
97
  assert_equal false, image.valid?
@@ -88,6 +110,7 @@ class ImageTest < Test::Unit::TestCase
88
110
  assert_equal 150, image[:width]
89
111
  assert_equal 55, image[:height]
90
112
  assert_equal [150, 55], image[:dimensions]
113
+ assert_equal 'PseudoClassRGB', image[:colorspace]
91
114
  assert_match(/^gif$/i, image[:format])
92
115
  image.destroy!
93
116
  end
@@ -220,7 +243,7 @@ class ImageTest < Test::Unit::TestCase
220
243
  end
221
244
  image.destroy!
222
245
  end
223
-
246
+
224
247
  # http://github.com/probablycorey/mini_magick/issues#issue/15
225
248
  def test_issue_15
226
249
  image = Image.open(Pathname.new(SIMPLE_IMAGE_PATH))
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_magick
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 3
4
5
  prerelease: false
5
6
  segments:
6
- - 2
7
7
  - 3
8
- version: "2.3"
8
+ - 2
9
+ version: "3.2"
9
10
  platform: ruby
10
11
  authors:
11
12
  - Corey Johnson
@@ -15,7 +16,7 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-10-12 00:00:00 +01:00
19
+ date: 2011-01-11 00:00:00 +00:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
@@ -26,6 +27,7 @@ dependencies:
26
27
  requirements:
27
28
  - - ~>
28
29
  - !ruby/object:Gem::Version
30
+ hash: 23
29
31
  segments:
30
32
  - 0
31
33
  - 0
@@ -56,7 +58,7 @@ files:
56
58
  - test/command_builder_test.rb
57
59
  - test/composited.jpg
58
60
  - test/image_test.rb
59
- - test/leaves.tiff
61
+ - test/leaves spaced.tiff
60
62
  - test/not_an_image.php
61
63
  - test/simple-minus.gif
62
64
  - test/simple.gif
@@ -75,6 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
77
  requirements:
76
78
  - - ">="
77
79
  - !ruby/object:Gem::Version
80
+ hash: 3
78
81
  segments:
79
82
  - 0
80
83
  version: "0"
@@ -83,6 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
86
  requirements:
84
87
  - - ">="
85
88
  - !ruby/object:Gem::Version
89
+ hash: 3
86
90
  segments:
87
91
  - 0
88
92
  version: "0"
@@ -99,7 +103,7 @@ test_files:
99
103
  - test/command_builder_test.rb
100
104
  - test/composited.jpg
101
105
  - test/image_test.rb
102
- - test/leaves.tiff
106
+ - test/leaves spaced.tiff
103
107
  - test/not_an_image.php
104
108
  - test/simple-minus.gif
105
109
  - test/simple.gif
File without changes