mini_magick 2.3 → 3.1

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.1
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
  #
@@ -156,27 +168,27 @@ module MiniMagick
156
168
  # Why do I go to the trouble of putting in newlines? Because otherwise animated gifs screw everything up
157
169
  case value.to_s
158
170
  when "format"
159
- run_command("identify", "-format", format_option("%m"), @path).split("\n")[0]
171
+ run_command("identify", "-format", format_option("%m"), escaped_path).split("\n")[0]
160
172
  when "height"
161
- run_command("identify", "-format", format_option("%h"), @path).split("\n")[0].to_i
173
+ run_command("identify", "-format", format_option("%h"), escaped_path).split("\n")[0].to_i
162
174
  when "width"
163
- run_command("identify", "-format", format_option("%w"), @path).split("\n")[0].to_i
175
+ run_command("identify", "-format", format_option("%w"), escaped_path).split("\n")[0].to_i
164
176
  when "dimensions"
165
- run_command("identify", "-format", format_option("%w %h"), @path).split("\n")[0].split.map{|v|v.to_i}
177
+ run_command("identify", "-format", format_option("%w %h"), escaped_path).split("\n")[0].split.map{|v|v.to_i}
166
178
  when "size"
167
179
  File.size(@path) # Do this because calling identify -format "%b" on an animated gif fails!
168
180
  when "original_at"
169
181
  # Get the EXIF original capture as a Time object
170
182
  Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil
171
183
  when /^EXIF\:/i
172
- result = run_command('identify', '-format', "\"%[#{value}]\"", @path).chop
184
+ result = run_command('identify', '-format', "\"%[#{value}]\"", escaped_path).chop
173
185
  if result.include?(",")
174
186
  read_character_data(result)
175
187
  else
176
188
  result
177
189
  end
178
190
  else
179
- run_command('identify', '-format', "\"#{value}\"", @path).split("\n")[0]
191
+ run_command('identify', '-format', "\"#{value}\"", escaped_path).split("\n")[0]
180
192
  end
181
193
  end
182
194
 
@@ -186,7 +198,7 @@ module MiniMagick
186
198
  #
187
199
  # @return [String] Whatever the result from the command line is. May not be terribly useful.
188
200
  def <<(*args)
189
- run_command("mogrify", *args << @path)
201
+ run_command("mogrify", *args << escaped_path)
190
202
  end
191
203
 
192
204
  # This is used to change the format of the image. That is, from "tiff to jpg" or something like that.
@@ -214,7 +226,7 @@ module MiniMagick
214
226
  begin
215
227
  FileUtils.copy_file(@path.sub(".#{format}", "-#{page}.#{format}"), @path)
216
228
  rescue => ex
217
- raise MiniMagickError, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
229
+ raise MiniMagick::Error, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
218
230
  end
219
231
  end
220
232
  ensure
@@ -229,10 +241,25 @@ module MiniMagick
229
241
  run_command("mogrify", "-quality", "100", "#{path}[0]")
230
242
  end
231
243
 
244
+ # Writes the temporary file out to either a file location (by passing in a String) or by
245
+ # passing in a Stream that you can #write(chunk) to repeatedly
246
+ #
247
+ # @param output_to [IOStream, String] Some kind of stream object that needs to be read or a file path as a String
248
+ # @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
249
  # 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
250
+ def write(output_to)
251
+ if output_to.kind_of?(String) || !output_to.respond_to?(:write)
252
+ FileUtils.copy_file @path, output_to
253
+ run_command "identify", output_to # Verify that we have a good image
254
+ else # stream
255
+ File.open(@path, "rb") do |f|
256
+ f.binmode
257
+ while chunk = f.read(8192)
258
+ output_to.write(chunk)
259
+ end
260
+ end
261
+ output_to
262
+ end
236
263
  end
237
264
 
238
265
  # Gives you raw image data back
@@ -255,14 +282,14 @@ module MiniMagick
255
282
 
256
283
  # You can use multiple commands together using this method. Very easy to use!
257
284
  #
258
- # @example
285
+ # @example
259
286
  # image.combine_options do |c|
260
287
  # c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
261
288
  # c.thumbnail "300x500>"
262
289
  # c.background background
263
290
  # end
264
291
  #
265
- # @yieldparam command [CommandBuilder]
292
+ # @yieldparam command [CommandBuilder]
266
293
  def combine_options(&block)
267
294
  c = CommandBuilder.new('mogrify')
268
295
  block.call(c)
@@ -272,7 +299,7 @@ module MiniMagick
272
299
 
273
300
  # Check to see if we are running on win32 -- we need to escape things differently
274
301
  def windows?
275
- !(RUBY_PLATFORM =~ /win32/).nil?
302
+ !(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil?
276
303
  end
277
304
 
278
305
  def composite(other_image, output_extension = 'jpg', &block)
data/test/image_test.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
- require 'stringio'
3
+ require 'mocha'
4
4
  require 'pathname'
5
+ require 'stringio'
5
6
  require File.expand_path('../../lib/mini_magick', __FILE__)
6
7
 
7
8
  #MiniMagick.processor = :gm
@@ -13,7 +14,7 @@ class ImageTest < Test::Unit::TestCase
13
14
 
14
15
  SIMPLE_IMAGE_PATH = CURRENT_DIR + "simple.gif"
15
16
  MINUS_IMAGE_PATH = CURRENT_DIR + "simple-minus.gif"
16
- TIFF_IMAGE_PATH = CURRENT_DIR + "leaves.tiff"
17
+ TIFF_IMAGE_PATH = CURRENT_DIR + "leaves spaced.tiff"
17
18
  NOT_AN_IMAGE_PATH = CURRENT_DIR + "not_an_image.php"
18
19
  GIF_WITH_JPG_EXT = CURRENT_DIR + "actually_a_gif.jpg"
19
20
  EXIF_IMAGE_PATH = CURRENT_DIR + "trogdor.jpg"
@@ -50,7 +51,7 @@ class ImageTest < Test::Unit::TestCase
50
51
  image = Image.new(SIMPLE_IMAGE_PATH)
51
52
  image.destroy!
52
53
  end
53
-
54
+
54
55
  def test_remote_image
55
56
  image = Image.open("http://www.google.com/images/logos/logo.png")
56
57
  image.valid?
@@ -70,6 +71,15 @@ class ImageTest < Test::Unit::TestCase
70
71
  image.destroy!
71
72
  end
72
73
 
74
+ def test_image_write_with_stream
75
+ stream = StringIO.new
76
+ image = Image.open(SIMPLE_IMAGE_PATH)
77
+ image.write("/tmp/foo.gif")
78
+ image.write(stream)
79
+ # assert Image.read(stream.string).valid?
80
+ image.destroy!
81
+ end
82
+
73
83
  def test_not_an_image
74
84
  image = Image.new(NOT_AN_IMAGE_PATH)
75
85
  assert_equal false, image.valid?
@@ -220,7 +230,7 @@ class ImageTest < Test::Unit::TestCase
220
230
  end
221
231
  image.destroy!
222
232
  end
223
-
233
+
224
234
  # http://github.com/probablycorey/mini_magick/issues#issue/15
225
235
  def test_issue_15
226
236
  image = Image.open(Pathname.new(SIMPLE_IMAGE_PATH))
@@ -239,4 +249,16 @@ class ImageTest < Test::Unit::TestCase
239
249
  end
240
250
  image.destroy!
241
251
  end
252
+
253
+ # testing that if copying files formatted from an animation fails,
254
+ # it raises an appropriate error
255
+ def test_throw_animation_copy_after_format_error
256
+ image = Image.open(ANIMATION_PATH)
257
+ FileUtils.stubs(:copy_file).raises(Errno::ENOENT)
258
+ assert_raises MiniMagick::Error do
259
+ image.format('png')
260
+ end
261
+ end
262
+
263
+
242
264
  end
metadata CHANGED
@@ -3,9 +3,9 @@ name: mini_magick
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 2
7
6
  - 3
8
- version: "2.3"
7
+ - 1
8
+ version: "3.1"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Corey Johnson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-12 00:00:00 +01:00
18
+ date: 2010-11-09 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -33,6 +33,21 @@ dependencies:
33
33
  version: 0.0.4
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: mocha
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 0
46
+ - 9
47
+ - 9
48
+ version: 0.9.9
49
+ type: :development
50
+ version_requirements: *id002
36
51
  description: ""
37
52
  email:
38
53
  - probablycorey@gmail.com
@@ -56,7 +71,7 @@ files:
56
71
  - test/command_builder_test.rb
57
72
  - test/composited.jpg
58
73
  - test/image_test.rb
59
- - test/leaves.tiff
74
+ - test/leaves spaced.tiff
60
75
  - test/not_an_image.php
61
76
  - test/simple-minus.gif
62
77
  - test/simple.gif
@@ -99,7 +114,7 @@ test_files:
99
114
  - test/command_builder_test.rb
100
115
  - test/composited.jpg
101
116
  - test/image_test.rb
102
- - test/leaves.tiff
117
+ - test/leaves spaced.tiff
103
118
  - test/not_an_image.php
104
119
  - test/simple-minus.gif
105
120
  - test/simple.gif
File without changes