jf--mini_magick 3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2005 Corey Johnson probablycorey@gmail.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
@@ -0,0 +1,92 @@
1
+ = MiniMagick
2
+
3
+ A ruby wrapper for ImageMagick or GraphicsMagick command line.
4
+
5
+ Tested on both Ruby 1.9.2 and Ruby 1.8.7.
6
+
7
+ == Why?
8
+
9
+ I was using RMagick and loving it, but it was eating up huge amounts
10
+ of memory. A simple script like this...
11
+
12
+ Magick::read("image.jpg") do |f|
13
+ f.write("manipulated.jpg")
14
+ end
15
+
16
+ ...would use over 100 Megs of Ram. On my local machine this wasn't a
17
+ problem, but on my hosting server the ruby apps would crash because of
18
+ their 100 Meg memory limit.
19
+
20
+
21
+ == Solution!
22
+
23
+ Using MiniMagick the ruby processes memory remains small (it spawns
24
+ ImageMagick's command line program mogrify which takes up some memory
25
+ as well, but is much smaller compared to RMagick)
26
+
27
+ MiniMagick gives you access to all the commandline options ImageMagick
28
+ has (Found here http://www.imagemagick.org/script/mogrify.php)
29
+
30
+
31
+ == Examples
32
+
33
+ Want to make a thumbnail from a file...
34
+
35
+ image = MiniMagick::Image.open("input.jpg")
36
+ image.resize "100x100"
37
+ image.write "output.jpg"
38
+
39
+ Want to make a thumbnail from a blob...
40
+
41
+ image = MiniMagick::Image.read(blob)
42
+ image.resize "100x100"
43
+ image.write "output.jpg"
44
+
45
+ Got an incoming IOStream?
46
+
47
+ image = MiniMagick::Image.read(stream)
48
+
49
+ Want to make a thumbnail of a remote image?
50
+
51
+ image = MiniMagick::Image.open("http://www.google.com/images/logos/logo.png")
52
+ image.resize "5x5"
53
+ image.format "gif"
54
+ image.write "localcopy.gif"
55
+
56
+ Need to combine several options?
57
+
58
+ image = MiniMagick::Image.open("input.jpg")
59
+ image.combine_options do |c|
60
+ c.sample "50%"
61
+ c.rotate "-90>"
62
+ end
63
+ image.write "output.jpg"
64
+
65
+ Want to composite two images? Super easy! (Aka, put a watermark on!)
66
+
67
+ image = Image.from_file("original.png")
68
+ result = image.composite(Image.open("watermark.png", "jpg") do |c|
69
+ c.gravity "center"
70
+ end
71
+ result.write "my_output_file.jpg"
72
+
73
+ Want to manipulate an image at its source (You won't have to write it
74
+ out because the transformations are done on that file)
75
+
76
+ image = MiniMagick::Image.new("input.jpg")
77
+ image.resize "100x100"
78
+
79
+ Want to get some meta-information out?
80
+
81
+ image = MiniMagick::Image.open("input.jpg")
82
+ image[:width] # will get the width (you can also use :height and :format)
83
+ image["EXIF:BitsPerSample"] # It also can get all the EXIF tags
84
+ image["%m:%f %wx%h"] # Or you can use one of the many options of the format command
85
+
86
+ For more on the format command see
87
+ http://www.imagemagick.org/script/command-line-options.php#format
88
+
89
+
90
+ == Requirements
91
+
92
+ You must have ImageMagick or GraphicsMagick installed.
@@ -0,0 +1,32 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ $:.unshift(File.dirname(__FILE__) + "/lib")
7
+ require 'mini_magick'
8
+
9
+ desc 'Default: run unit tests.'
10
+ task :default => :test
11
+
12
+ desc 'Test the mini_magick plugin.'
13
+ Rake::TestTask.new(:test) do |t|
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
18
+
19
+ desc 'Generate documentation for the mini_magick plugin.'
20
+ Rake::RDocTask.new(:rdoc) do |rdoc|
21
+ rdoc.rdoc_dir = 'rdoc'
22
+ rdoc.title = 'MiniMagick'
23
+ rdoc.options << '--line-numbers'
24
+ rdoc.options << '--inline-source'
25
+ rdoc.rdoc_files.include('README.rdoc')
26
+ rdoc.rdoc_files.include('lib/**/*.rb')
27
+ end
28
+
29
+ spec = eval(File.read('mini_magick.gemspec'))
30
+ Rake::GemPackageTask.new(spec) do |pkg|
31
+ pkg.gem_spec = spec
32
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.1
@@ -0,0 +1,2 @@
1
+ require 'mini_magick'
2
+ MiniMagick.processor = :gm
@@ -0,0 +1,426 @@
1
+ require 'tempfile'
2
+ require 'subexec'
3
+ require 'pathname'
4
+
5
+ module MiniMagick
6
+ class << self
7
+ attr_accessor :processor
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
22
+ end
23
+
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}
25
+
26
+ class Error < RuntimeError; end
27
+ class Invalid < StandardError; end
28
+
29
+ class Image
30
+ # @return [String] The location of the current working file
31
+ attr :path
32
+
33
+ # Class Methods
34
+ # -------------
35
+ class << self
36
+ # This is the primary loading method used by all of the other class methods.
37
+ #
38
+ # Use this to pass in a stream object. Must respond to Object#read(size) or be a binary string object (BLOBBBB)
39
+ #
40
+ # As a change from the old API, please try and use IOStream objects. They are much, much better and more efficient!
41
+ #
42
+ # Probably easier to use the #open method if you want to open a file or a URL.
43
+ #
44
+ # @param stream [IOStream, String] Some kind of stream object that needs to be read or is a binary String blob!
45
+ # @param ext [String] A manual extension to use for reading the file. Not required, but if you are having issues, give this a try.
46
+ # @return [Image]
47
+ def read(stream, ext = nil)
48
+ if stream.is_a?(String)
49
+ stream = StringIO.new(stream)
50
+ end
51
+
52
+ create(ext) do |f|
53
+ while chunk = stream.read(8192)
54
+ f.write(chunk)
55
+ end
56
+ end
57
+ end
58
+
59
+ # @deprecated Please use Image.read instead!
60
+ def from_blob(blob, ext = nil)
61
+ warn "Warning: MiniMagick::Image.from_blob method is deprecated. Instead, please use Image.read"
62
+ create(ext) { |f| f.write(blob) }
63
+ end
64
+
65
+ # Opens a specific image file either on the local file system or at a URI.
66
+ #
67
+ # Use this if you don't want to overwrite the image file.
68
+ #
69
+ # Extension is either guessed from the path or you can specify it as a second parameter.
70
+ #
71
+ # If you pass in what looks like a URL, we require 'open-uri' before opening it.
72
+ #
73
+ # @param file_or_url [String] Either a local file path or a URL that open-uri can read
74
+ # @param ext [String] Specify the extension you want to read it as
75
+ # @return [Image] The loaded image
76
+ def open(file_or_url, ext = File.extname(file_or_url))
77
+ file_or_url = file_or_url.to_s # Force it to be a String... hell or highwater
78
+ if file_or_url.include?("://")
79
+ require 'open-uri'
80
+ self.read(Kernel::open(file_or_url), ext)
81
+ else
82
+ File.open(file_or_url, "rb") do |f|
83
+ self.read(f, ext)
84
+ end
85
+ end
86
+ end
87
+
88
+ # @deprecated Please use MiniMagick::Image.open(file_or_url) now
89
+ def from_file(file, ext = nil)
90
+ warn "Warning: MiniMagick::Image.from_file is now deprecated. Please use Image.open"
91
+ open(file, ext)
92
+ end
93
+
94
+ # Used to create a new Image object data-copy. Not used to "paint" or that kind of thing.
95
+ #
96
+ # Takes an extension in a block and can be used to build a new Image object. Used
97
+ # by both #open and #read to create a new object! Ensures we have a good tempfile!
98
+ #
99
+ # @param ext [String] Specify the extension you want to read it as
100
+ # @yield [IOStream] You can #write bits to this object to create the new Image
101
+ # @return [Image] The created image
102
+ def create(ext = nil, &block)
103
+ begin
104
+ tempfile = Tempfile.new(['mini_magick', ext.to_s])
105
+ tempfile.binmode
106
+ block.call(tempfile)
107
+ tempfile.close
108
+
109
+ image = self.new(tempfile.path, tempfile)
110
+
111
+ if !image.valid?
112
+ raise MiniMagick::Invalid
113
+ end
114
+ return image
115
+ ensure
116
+ tempfile.close if tempfile
117
+ end
118
+ end
119
+ end
120
+
121
+ # Create a new MiniMagick::Image object
122
+ #
123
+ # _DANGER_: The file location passed in here is the *working copy*. That is, it gets *modified*.
124
+ # you can either copy it yourself or use the MiniMagick::Image.open(path) method which creates a
125
+ # temporary file for you and protects your original!
126
+ #
127
+ # @param input_path [String] The location of an image file
128
+ # @todo Allow this to accept a block that can pass off to Image#combine_options
129
+ def initialize(input_path, tempfile = nil)
130
+ @path = input_path
131
+ @tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected.
132
+ end
133
+
134
+ def escaped_path
135
+ Pathname.new(@path).to_s.gsub(" ", "\\ ")
136
+ end
137
+
138
+ # Checks to make sure that MiniMagick can read the file and understand it.
139
+ #
140
+ # This uses the 'identify' command line utility to check the file. If you are having
141
+ # issues with this, then please work directly with the 'identify' command and see if you
142
+ # can figure out what the issue is.
143
+ #
144
+ # @return [Boolean]
145
+ def valid?
146
+ run_command("identify", @path)
147
+ true
148
+ rescue MiniMagick::Invalid
149
+ false
150
+ end
151
+
152
+ # A rather low-level way to interact with the "identify" command. No nice API here, just
153
+ # the crazy stuff you find in ImageMagick. See the examples listed!
154
+ #
155
+ # @example
156
+ # image["format"] #=> "TIFF"
157
+ # image["height"] #=> 41 (pixels)
158
+ # image["width"] #=> 50 (pixels)
159
+ # image["dimensions"] #=> [50, 41]
160
+ # image["size"] #=> 2050 (bits)
161
+ # image["original_at"] #=> 2005-02-23 23:17:24 +0000 (Read from Exif data)
162
+ # image["EXIF:ExifVersion"] #=> "0220" (Can read anything from Exif)
163
+ #
164
+ # @param format [String] A format for the "identify" command
165
+ # @see For reference see http://www.imagemagick.org/script/command-line-options.php#format
166
+ # @return [String, Numeric, Array, Time, Object] Depends on the method called! Defaults to String for unknown commands
167
+ def [](value)
168
+ # Why do I go to the trouble of putting in newlines? Because otherwise animated gifs screw everything up
169
+ case value.to_s
170
+ when "format"
171
+ run_command("identify", "-format", format_option("%m"), escaped_path).split("\n")[0]
172
+ when "height"
173
+ run_command("identify", "-format", format_option("%h"), escaped_path).split("\n")[0].to_i
174
+ when "width"
175
+ run_command("identify", "-format", format_option("%w"), escaped_path).split("\n")[0].to_i
176
+ when "dimensions"
177
+ run_command("identify", "-format", format_option("%w %h"), escaped_path).split("\n")[0].split.map{|v|v.to_i}
178
+ when "size"
179
+ File.size(@path) # Do this because calling identify -format "%b" on an animated gif fails!
180
+ when "original_at"
181
+ # Get the EXIF original capture as a Time object
182
+ Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil
183
+ when /^EXIF\:/i
184
+ result = run_command('identify', '-format', "\"%[#{value}]\"", escaped_path).chop
185
+ if result.include?(",")
186
+ read_character_data(result)
187
+ else
188
+ result
189
+ end
190
+ else
191
+ run_command('identify', '-format', "\"#{value}\"", escaped_path).split("\n")[0]
192
+ end
193
+ end
194
+
195
+ # Sends raw commands to imagemagick's `mogrify` command. The image path is automatically appended to the command.
196
+ #
197
+ # Remember, we are always acting on this instance of the Image when messing with this.
198
+ #
199
+ # @return [String] Whatever the result from the command line is. May not be terribly useful.
200
+ def <<(*args)
201
+ run_command("mogrify", *args << escaped_path)
202
+ end
203
+
204
+ # This is used to change the format of the image. That is, from "tiff to jpg" or something like that.
205
+ # Once you run it, the instance is pointing to a new file with a new extension!
206
+ #
207
+ # *DANGER*: This renames the file that the instance is pointing to. So, if you manually opened the
208
+ # file with Image.new(file_path)... then that file is DELETED! If you used Image.open(file) then
209
+ # you are ok. The original file will still be there. But, any changes to it might not be...
210
+ #
211
+ # Formatting an animation into a non-animated type will result in ImageMagick creating multiple
212
+ # pages (starting with 0). You can choose which page you want to manipulate. We default to the
213
+ # first page.
214
+ #
215
+ # @param format [String] The target format... like 'jpg', 'gif', 'tiff', etc.
216
+ # @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.
217
+ # @return [nil]
218
+ def format(format, page = 0)
219
+ run_command("mogrify", "-format", format, @path)
220
+
221
+ old_path = @path.dup
222
+ @path.sub!(/(\.\w*)?$/, ".#{format}")
223
+ File.delete(old_path) if old_path != @path
224
+
225
+ unless File.exists?(@path)
226
+ begin
227
+ FileUtils.copy_file(@path.sub(".#{format}", "-#{page}.#{format}"), @path)
228
+ rescue => ex
229
+ raise MiniMagick::Error, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
230
+ end
231
+ end
232
+ ensure
233
+ Dir[@path.sub(/(\.\w+)?$/, "-[0-9]*.#{format}")].each do |fname|
234
+ File.unlink(fname)
235
+ end
236
+ end
237
+
238
+ # Collapse images with sequences to the first frame (ie. animated gifs) and
239
+ # preserve quality
240
+ def collapse!
241
+ run_command("mogrify", "-quality", "100", "#{path}[0]")
242
+ end
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.
249
+ # Writes the temporary image that we are using for processing to the output path
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
263
+ end
264
+
265
+ # Gives you raw image data back
266
+ # @return [String] binary string
267
+ def to_blob
268
+ f = File.new @path
269
+ f.binmode
270
+ f.read
271
+ ensure
272
+ f.close if f
273
+ end
274
+
275
+ # If an unknown method is called then it is sent through the morgrify program
276
+ # Look here to find all the commands (http://www.imagemagick.org/script/mogrify.php)
277
+ def method_missing(symbol, *args)
278
+ combine_options do |c|
279
+ c.method_missing(symbol, *args)
280
+ end
281
+ end
282
+
283
+ # You can use multiple commands together using this method. Very easy to use!
284
+ #
285
+ # @example
286
+ # image.combine_options do |c|
287
+ # c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
288
+ # c.thumbnail "300x500>"
289
+ # c.background background
290
+ # end
291
+ #
292
+ # @yieldparam command [CommandBuilder]
293
+ def combine_options(&block)
294
+ c = CommandBuilder.new('mogrify')
295
+ block.call(c)
296
+ c << @path
297
+ run(c)
298
+ end
299
+
300
+ # Check to see if we are running on win32 -- we need to escape things differently
301
+ def windows?
302
+ !(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil?
303
+ end
304
+
305
+ def composite(other_image, output_extension = 'jpg', &block)
306
+ begin
307
+ second_tempfile = Tempfile.new(output_extension)
308
+ second_tempfile.binmode
309
+ ensure
310
+ second_tempfile.close
311
+ end
312
+
313
+ command = CommandBuilder.new("composite")
314
+ block.call(command) if block
315
+ command.push(other_image.path)
316
+ command.push(self.path)
317
+ command.push(second_tempfile.path)
318
+
319
+ run(command)
320
+ return Image.new(second_tempfile.path, second_tempfile)
321
+ end
322
+
323
+ # Outputs a carriage-return delimited format string for Unix and Windows
324
+ def format_option(format)
325
+ windows? ? "\"#{format}\\n\"" : "\"#{format}\\\\n\""
326
+ end
327
+
328
+ def run_command(command, *args)
329
+ # -ping "efficiently determine image characteristics."
330
+ if command == 'identify'
331
+ args.unshift '-ping'
332
+ end
333
+
334
+ run(CommandBuilder.new(command, *args))
335
+ end
336
+
337
+ def run(command_builder)
338
+ command = command_builder.command
339
+
340
+ sub = Subexec.run(command, :timeout => MiniMagick.timeout)
341
+
342
+ if sub.exitstatus != 0
343
+ # Clean up after ourselves in case of an error
344
+ destroy!
345
+
346
+ # Raise the appropriate error
347
+ if sub.output =~ /no decode delegate/i || sub.output =~ /did not return an image/i
348
+ raise Invalid, sub.output
349
+ else
350
+ # TODO: should we do something different if the command times out ...?
351
+ # its definitely better for logging.. otherwise we dont really know
352
+ raise Error, "Command (#{command.inspect.gsub("\\", "")}) failed: #{{:status_code => sub.exitstatus, :output => sub.output}.inspect}"
353
+ end
354
+ else
355
+ sub.output
356
+ end
357
+ end
358
+
359
+ def destroy!
360
+ return if @tempfile.nil?
361
+ File.unlink(@tempfile.path)
362
+ @tempfile = nil
363
+ end
364
+
365
+ private
366
+ # Sometimes we get back a list of character values
367
+ def read_character_data(list_of_characters)
368
+ chars = list_of_characters.gsub(" ", "").split(",")
369
+ result = ""
370
+ chars.each do |val|
371
+ result << ("%c" % val.to_i)
372
+ end
373
+ result
374
+ end
375
+ end
376
+
377
+ class CommandBuilder
378
+ attr :args
379
+ attr :command
380
+
381
+ def initialize(command, *options)
382
+ @command = command
383
+ @args = []
384
+ options.each { |arg| push(arg) }
385
+ end
386
+
387
+ def command
388
+ "#{MiniMagick.processor} #{@command} #{@args.join(' ')}".strip
389
+ end
390
+
391
+ def method_missing(symbol, *options)
392
+ guessed_command_name = symbol.to_s.gsub('_','-')
393
+ if guessed_command_name == "format"
394
+ raise Error, "You must call 'format' on the image object directly!"
395
+ elsif MOGRIFY_COMMANDS.include?(guessed_command_name)
396
+ add(guessed_command_name, *options)
397
+ self
398
+ else
399
+ super(symbol, *args)
400
+ end
401
+ end
402
+
403
+ def +(*options)
404
+ push(@args.pop.gsub /^-/, '+')
405
+ if options.any?
406
+ options.each do |o|
407
+ push "\"#{ o }\""
408
+ end
409
+ end
410
+ end
411
+
412
+ def add(command, *options)
413
+ push "-#{command}"
414
+ if options.any?
415
+ options.each do |o|
416
+ push "\"#{ o }\""
417
+ end
418
+ end
419
+ end
420
+
421
+ def push(arg)
422
+ @args << arg.to_s.strip
423
+ end
424
+ alias :<< :push
425
+ end
426
+ end
Binary file
Binary file
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.expand_path('../../lib/mini_magick', __FILE__)
4
+
5
+ class CommandBuilderTest < Test::Unit::TestCase
6
+ include MiniMagick
7
+
8
+ def test_basic
9
+ c = CommandBuilder.new("test")
10
+ c.resize "30x40"
11
+ assert_equal "-resize \"30x40\"", c.args.join(" ")
12
+ end
13
+
14
+ def test_complicated
15
+ c = CommandBuilder.new("test")
16
+ c.resize "30x40"
17
+ c.alpha "1 3 4"
18
+ c.resize "mome fingo"
19
+ assert_equal "-resize \"30x40\" -alpha \"1 3 4\" -resize \"mome fingo\"", c.args.join(" ")
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
27
+
28
+ def test_valid_command
29
+ begin
30
+ c = CommandBuilder.new("test", "path")
31
+ c.input 2
32
+ assert false
33
+ rescue NoMethodError
34
+ assert true
35
+ end
36
+ end
37
+
38
+ def test_dashed
39
+ c = CommandBuilder.new("test")
40
+ c.auto_orient
41
+ assert_equal "-auto-orient", c.args.join(" ")
42
+ end
43
+ end
Binary file
@@ -0,0 +1,264 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+ require 'pathname'
5
+ require 'stringio'
6
+ require File.expand_path('../../lib/mini_magick', __FILE__)
7
+
8
+ #MiniMagick.processor = :gm
9
+
10
+ class ImageTest < Test::Unit::TestCase
11
+ include MiniMagick
12
+
13
+ CURRENT_DIR = File.dirname(File.expand_path(__FILE__)) + "/"
14
+
15
+ SIMPLE_IMAGE_PATH = CURRENT_DIR + "simple.gif"
16
+ MINUS_IMAGE_PATH = CURRENT_DIR + "simple-minus.gif"
17
+ TIFF_IMAGE_PATH = CURRENT_DIR + "leaves spaced.tiff"
18
+ NOT_AN_IMAGE_PATH = CURRENT_DIR + "not_an_image.php"
19
+ GIF_WITH_JPG_EXT = CURRENT_DIR + "actually_a_gif.jpg"
20
+ EXIF_IMAGE_PATH = CURRENT_DIR + "trogdor.jpg"
21
+ ANIMATION_PATH = CURRENT_DIR + "animation.gif"
22
+
23
+ def test_image_from_blob
24
+ File.open(SIMPLE_IMAGE_PATH, "rb") do |f|
25
+ image = Image.read(f.read)
26
+ assert image.valid?
27
+ image.destroy!
28
+ end
29
+ end
30
+
31
+ def test_image_open
32
+ image = Image.open(SIMPLE_IMAGE_PATH)
33
+ assert image.valid?
34
+ image.destroy!
35
+ end
36
+
37
+ def test_image_io_reading
38
+ buffer = StringIO.new(File.read(SIMPLE_IMAGE_PATH))
39
+ image = Image.read(buffer)
40
+ image.destroy!
41
+ end
42
+
43
+ def test_image_create
44
+ image = Image.create do |f|
45
+ f.write(File.read(SIMPLE_IMAGE_PATH))
46
+ end
47
+ image.destroy!
48
+ end
49
+
50
+ def test_image_new
51
+ image = Image.new(SIMPLE_IMAGE_PATH)
52
+ image.destroy!
53
+ end
54
+
55
+ def test_remote_image
56
+ image = Image.open("http://www.google.com/images/logos/logo.png")
57
+ image.valid?
58
+ image.destroy!
59
+ end
60
+
61
+ def test_image_write
62
+ output_path = "output.gif"
63
+ begin
64
+ image = Image.new(SIMPLE_IMAGE_PATH)
65
+ image.write output_path
66
+
67
+ assert File.exists?(output_path)
68
+ ensure
69
+ File.delete output_path
70
+ end
71
+ image.destroy!
72
+ end
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
+
83
+ def test_not_an_image
84
+ image = Image.new(NOT_AN_IMAGE_PATH)
85
+ assert_equal false, image.valid?
86
+ image.destroy!
87
+ end
88
+
89
+ def test_throw_on_openining_not_an_image
90
+ assert_raise(MiniMagick::Invalid) do
91
+ image = Image.open(NOT_AN_IMAGE_PATH)
92
+ image.destroy
93
+ end
94
+ end
95
+
96
+ def test_image_meta_info
97
+ image = Image.new(SIMPLE_IMAGE_PATH)
98
+ assert_equal 150, image[:width]
99
+ assert_equal 55, image[:height]
100
+ assert_equal [150, 55], image[:dimensions]
101
+ assert_match(/^gif$/i, image[:format])
102
+ image.destroy!
103
+ end
104
+
105
+ def test_tiff
106
+ image = Image.new(TIFF_IMAGE_PATH)
107
+ assert_equal "tiff", image[:format].downcase
108
+ assert_equal 50, image[:width]
109
+ assert_equal 41, image[:height]
110
+ image.destroy!
111
+ end
112
+
113
+ def test_gif_with_jpg_format
114
+ image = Image.new(GIF_WITH_JPG_EXT)
115
+ assert_equal "gif", image[:format].downcase
116
+ image.destroy!
117
+ end
118
+
119
+ def test_image_resize
120
+ image = Image.open(SIMPLE_IMAGE_PATH)
121
+ image.resize "20x30!"
122
+
123
+ assert_equal 20, image[:width]
124
+ assert_equal 30, image[:height]
125
+ assert_match(/^gif$/i, image[:format])
126
+ image.destroy!
127
+ end
128
+
129
+ def test_image_resize_with_minimum
130
+ image = Image.open(SIMPLE_IMAGE_PATH)
131
+ original_width, original_height = image[:width], image[:height]
132
+ image.resize "#{original_width + 10}x#{original_height + 10}>"
133
+
134
+ assert_equal original_width, image[:width]
135
+ assert_equal original_height, image[:height]
136
+ image.destroy!
137
+ end
138
+
139
+ def test_image_combine_options_resize_blur
140
+ image = Image.open(SIMPLE_IMAGE_PATH)
141
+ image.combine_options do |c|
142
+ c.resize "20x30!"
143
+ c.blur "50"
144
+ end
145
+
146
+ assert_equal 20, image[:width]
147
+ assert_equal 30, image[:height]
148
+ assert_match(/^gif$/i, image[:format])
149
+ image.destroy!
150
+ end
151
+
152
+ def test_image_combine_options_with_filename_with_minusses_in_it
153
+ image = Image.open(SIMPLE_IMAGE_PATH)
154
+ background = "#000000"
155
+ assert_nothing_raised do
156
+ image.combine_options do |c|
157
+ c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
158
+ c.thumbnail "300x500>"
159
+ c.background background
160
+ end
161
+ end
162
+ image.destroy!
163
+ end
164
+
165
+ def test_exif
166
+ image = Image.open(EXIF_IMAGE_PATH)
167
+ assert_equal('0220', image["exif:ExifVersion"])
168
+ image = Image.open(SIMPLE_IMAGE_PATH)
169
+ assert_equal('', image["EXIF:ExifVersion"])
170
+ image.destroy!
171
+ end
172
+
173
+ def test_original_at
174
+ image = Image.open(EXIF_IMAGE_PATH)
175
+ assert_equal(Time.local('2005', '2', '23', '23', '17', '24'), image[:original_at])
176
+ image = Image.open(SIMPLE_IMAGE_PATH)
177
+ assert_nil(image[:original_at])
178
+ image.destroy!
179
+ end
180
+
181
+ def test_tempfile_at_path
182
+ image = Image.open(TIFF_IMAGE_PATH)
183
+ assert_equal image.path, image.instance_eval("@tempfile.path")
184
+ image.destroy!
185
+ end
186
+
187
+ def test_tempfile_at_path_after_format
188
+ image = Image.open(TIFF_IMAGE_PATH)
189
+ image.format('png')
190
+ assert_equal image.path, image.instance_eval("@tempfile.path")
191
+ image.destroy!
192
+ end
193
+
194
+ def test_previous_tempfile_deleted_after_format
195
+ image = Image.open(TIFF_IMAGE_PATH)
196
+ before = image.path.dup
197
+ image.format('png')
198
+ assert !File.exist?(before)
199
+ image.destroy!
200
+ end
201
+
202
+ def test_bad_method_bug
203
+ image = Image.open(TIFF_IMAGE_PATH)
204
+ begin
205
+ image.to_blog
206
+ rescue NoMethodError
207
+ assert true
208
+ end
209
+ image.to_blob
210
+ assert true #we made it this far without error
211
+ image.destroy!
212
+ end
213
+
214
+ def test_simple_composite
215
+ image = Image.open(EXIF_IMAGE_PATH)
216
+ result = image.composite(Image.open(TIFF_IMAGE_PATH)) do |c|
217
+ c.gravity "center"
218
+ end
219
+ assert `diff -s #{result.path} test/composited.jpg`.include?("identical")
220
+ end
221
+
222
+ # http://github.com/probablycorey/mini_magick/issues#issue/8
223
+ def test_issue_8
224
+ image = Image.open(SIMPLE_IMAGE_PATH)
225
+ assert_nothing_raised do
226
+ image.combine_options do |c|
227
+ c.sample "50%"
228
+ c.rotate "-90>"
229
+ end
230
+ end
231
+ image.destroy!
232
+ end
233
+
234
+ # http://github.com/probablycorey/mini_magick/issues#issue/15
235
+ def test_issue_15
236
+ image = Image.open(Pathname.new(SIMPLE_IMAGE_PATH))
237
+ output = Pathname.new("test.gif")
238
+ image.write(output)
239
+ ensure
240
+ FileUtils.rm("test.gif")
241
+ end
242
+
243
+ def test_throw_format_error
244
+ image = Image.open(SIMPLE_IMAGE_PATH)
245
+ assert_raise MiniMagick::Error do
246
+ image.combine_options do |c|
247
+ c.format "png"
248
+ end
249
+ end
250
+ image.destroy!
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
+
264
+ end
Binary file
@@ -0,0 +1 @@
1
+ <?php I am so not an image ?>
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jf--mini_magick
3
+ version: !ruby/object:Gem::Version
4
+ hash: 5
5
+ prerelease: false
6
+ segments:
7
+ - 3
8
+ - 1
9
+ version: "3.1"
10
+ platform: ruby
11
+ authors:
12
+ - Corey Johnson
13
+ - Hampton Catlin
14
+ - Peter Kieltyka
15
+ - Jeffrey 'jf' Lim
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2010-12-26 00:00:00 +08:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: subexec
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ hash: 23
32
+ segments:
33
+ - 0
34
+ - 0
35
+ - 4
36
+ version: 0.0.4
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: mocha
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ hash: 41
48
+ segments:
49
+ - 0
50
+ - 9
51
+ - 9
52
+ version: 0.9.9
53
+ type: :development
54
+ version_requirements: *id002
55
+ description: ""
56
+ email:
57
+ - probablycorey@gmail.com
58
+ - hcatlin@gmail.com
59
+ - peter@nulayer.com
60
+ - jfs.world@gmail.com
61
+ executables: []
62
+
63
+ extensions: []
64
+
65
+ extra_rdoc_files: []
66
+
67
+ files:
68
+ - README.rdoc
69
+ - VERSION
70
+ - MIT-LICENSE
71
+ - Rakefile
72
+ - lib/mini_gmagick.rb
73
+ - lib/mini_magick.rb
74
+ - test/actually_a_gif.jpg
75
+ - test/animation.gif
76
+ - test/command_builder_test.rb
77
+ - test/composited.jpg
78
+ - test/image_test.rb
79
+ - test/leaves spaced.tiff
80
+ - test/not_an_image.php
81
+ - test/simple-minus.gif
82
+ - test/simple.gif
83
+ - test/trogdor.jpg
84
+ has_rdoc: true
85
+ homepage: http://github.com/jf/mini_magick
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.7
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Manipulate images with minimal use of memory via ImageMagick / GraphicsMagick. With jf's fixes and patches.
118
+ test_files:
119
+ - test/actually_a_gif.jpg
120
+ - test/animation.gif
121
+ - test/command_builder_test.rb
122
+ - test/composited.jpg
123
+ - test/image_test.rb
124
+ - test/leaves spaced.tiff
125
+ - test/not_an_image.php
126
+ - test/simple-minus.gif
127
+ - test/simple.gif
128
+ - test/trogdor.jpg