mini_magick 2.0.0 → 3.0
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/README.rdoc +19 -8
- data/VERSION +1 -1
- data/lib/mini_magick.rb +213 -79
- data/test/command_builder_test.rb +2 -10
- data/test/image_test.rb +106 -47
- data/test/leaves.tiff +0 -0
- metadata +18 -18
data/README.rdoc
CHANGED
|
@@ -32,24 +32,35 @@ has (Found here http://www.imagemagick.org/script/mogrify.php)
|
|
|
32
32
|
|
|
33
33
|
Want to make a thumbnail from a file...
|
|
34
34
|
|
|
35
|
-
image = MiniMagick::Image.
|
|
35
|
+
image = MiniMagick::Image.open("input.jpg")
|
|
36
36
|
image.resize "100x100"
|
|
37
|
-
image.write
|
|
37
|
+
image.write "output.jpg"
|
|
38
38
|
|
|
39
39
|
Want to make a thumbnail from a blob...
|
|
40
40
|
|
|
41
|
-
image = MiniMagick::Image.
|
|
41
|
+
image = MiniMagick::Image.read(blob)
|
|
42
42
|
image.resize "100x100"
|
|
43
|
-
image.write
|
|
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"
|
|
44
55
|
|
|
45
56
|
Need to combine several options?
|
|
46
57
|
|
|
47
|
-
image = MiniMagick::Image.
|
|
58
|
+
image = MiniMagick::Image.open("input.jpg")
|
|
48
59
|
image.combine_options do |c|
|
|
49
60
|
c.sample "50%"
|
|
50
61
|
c.rotate "-90>"
|
|
51
62
|
end
|
|
52
|
-
image.write
|
|
63
|
+
image.write "output.jpg"
|
|
53
64
|
|
|
54
65
|
Want to composite two images? Super easy! (Aka, put a watermark on!)
|
|
55
66
|
|
|
@@ -57,7 +68,7 @@ Want to composite two images? Super easy! (Aka, put a watermark on!)
|
|
|
57
68
|
result = image.composite(Image.open("watermark.png", "jpg") do |c|
|
|
58
69
|
c.gravity "center"
|
|
59
70
|
end
|
|
60
|
-
result.write
|
|
71
|
+
result.write "my_output_file.jpg"
|
|
61
72
|
|
|
62
73
|
Want to manipulate an image at its source (You won't have to write it
|
|
63
74
|
out because the transformations are done on that file)
|
|
@@ -67,7 +78,7 @@ out because the transformations are done on that file)
|
|
|
67
78
|
|
|
68
79
|
Want to get some meta-information out?
|
|
69
80
|
|
|
70
|
-
image = MiniMagick::Image.
|
|
81
|
+
image = MiniMagick::Image.open("input.jpg")
|
|
71
82
|
image[:width] # will get the width (you can also use :height and :format)
|
|
72
83
|
image["EXIF:BitsPerSample"] # It also can get all the EXIF tags
|
|
73
84
|
image["%m:%f %wx%h"] # Or you can use one of the many options of the format command
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
3.0
|
data/lib/mini_magick.rb
CHANGED
|
@@ -5,53 +5,139 @@ module MiniMagick
|
|
|
5
5
|
class << self
|
|
6
6
|
attr_accessor :processor
|
|
7
7
|
attr_accessor :timeout
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Experimental method for automatically selecting a processor
|
|
11
|
+
# such as gm. Only works on *nix.
|
|
12
|
+
#
|
|
13
|
+
# TODO: Write tests for this and figure out what platforms it supports
|
|
14
|
+
def choose_processor
|
|
15
|
+
if `type -P mogrify`.size > 0
|
|
16
|
+
return
|
|
17
|
+
elsif `type -P gm`.size > 0
|
|
18
|
+
self.processor = "gm"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
8
21
|
end
|
|
9
|
-
|
|
22
|
+
|
|
10
23
|
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}
|
|
11
24
|
|
|
12
25
|
class Error < RuntimeError; end
|
|
13
26
|
class Invalid < StandardError; end
|
|
14
27
|
|
|
15
28
|
class Image
|
|
29
|
+
# @return [String] The location of the current working file
|
|
16
30
|
attr :path
|
|
17
|
-
attr :tempfile
|
|
18
|
-
attr :output
|
|
19
31
|
|
|
20
32
|
# Class Methods
|
|
21
33
|
# -------------
|
|
22
34
|
class << self
|
|
35
|
+
# This is the primary loading method used by all of the other class methods.
|
|
36
|
+
#
|
|
37
|
+
# Use this to pass in a stream object. Must respond to Object#read(size) or be a binary string object (BLOBBBB)
|
|
38
|
+
#
|
|
39
|
+
# As a change from the old API, please try and use IOStream objects. They are much, much better and more efficient!
|
|
40
|
+
#
|
|
41
|
+
# Probably easier to use the #open method if you want to open a file or a URL.
|
|
42
|
+
#
|
|
43
|
+
# @param stream [IOStream, String] Some kind of stream object that needs to be read or is a binary String blob!
|
|
44
|
+
# @param ext [String] A manual extension to use for reading the file. Not required, but if you are having issues, give this a try.
|
|
45
|
+
# @return [Image]
|
|
46
|
+
def read(stream, ext = nil)
|
|
47
|
+
if stream.is_a?(String)
|
|
48
|
+
stream = StringIO.new(stream)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
create(ext) do |f|
|
|
52
|
+
while chunk = stream.read(8192)
|
|
53
|
+
f.write(chunk)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# @deprecated Please use Image.read instead!
|
|
23
59
|
def from_blob(blob, ext = nil)
|
|
60
|
+
warn "Warning: MiniMagick::Image.from_blob method is deprecated. Instead, please use Image.read"
|
|
61
|
+
create(ext) { |f| f.write(blob) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Opens a specific image file either on the local file system or at a URI.
|
|
65
|
+
#
|
|
66
|
+
# Use this if you don't want to overwrite the image file.
|
|
67
|
+
#
|
|
68
|
+
# Extension is either guessed from the path or you can specify it as a second parameter.
|
|
69
|
+
#
|
|
70
|
+
# If you pass in what looks like a URL, we require 'open-uri' before opening it.
|
|
71
|
+
#
|
|
72
|
+
# @param file_or_url [String] Either a local file path or a URL that open-uri can read
|
|
73
|
+
# @param ext [String] Specify the extension you want to read it as
|
|
74
|
+
# @return [Image] The loaded image
|
|
75
|
+
def open(file_or_url, ext = File.extname(file_or_url))
|
|
76
|
+
file_or_url = file_or_url.to_s # Force it to be a String... hell or highwater
|
|
77
|
+
if file_or_url.include?("://")
|
|
78
|
+
require 'open-uri'
|
|
79
|
+
self.read(Kernel::open(file_or_url), ext)
|
|
80
|
+
else
|
|
81
|
+
File.open(file_or_url, "rb") do |f|
|
|
82
|
+
self.read(f, ext)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @deprecated Please use MiniMagick::Image.open(file_or_url) now
|
|
88
|
+
def from_file(file, ext = nil)
|
|
89
|
+
warn "Warning: MiniMagick::Image.from_file is now deprecated. Please use Image.open"
|
|
90
|
+
open(file, ext)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Used to create a new Image object data-copy. Not used to "paint" or that kind of thing.
|
|
94
|
+
#
|
|
95
|
+
# Takes an extension in a block and can be used to build a new Image object. Used
|
|
96
|
+
# by both #open and #read to create a new object! Ensures we have a good tempfile!
|
|
97
|
+
#
|
|
98
|
+
# @param ext [String] Specify the extension you want to read it as
|
|
99
|
+
# @yield [IOStream] You can #write bits to this object to create the new Image
|
|
100
|
+
# @return [Image] The created image
|
|
101
|
+
def create(ext = nil, &block)
|
|
24
102
|
begin
|
|
25
103
|
tempfile = Tempfile.new(['mini_magick', ext.to_s])
|
|
26
104
|
tempfile.binmode
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
tempfile.close if tempfile
|
|
30
|
-
end
|
|
105
|
+
block.call(tempfile)
|
|
106
|
+
tempfile.close
|
|
31
107
|
|
|
32
|
-
|
|
33
|
-
if !image.valid?
|
|
34
|
-
raise MiniMagick::Invalid
|
|
35
|
-
end
|
|
36
|
-
image
|
|
37
|
-
end
|
|
108
|
+
image = self.new(tempfile.path, tempfile)
|
|
38
109
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
110
|
+
if !image.valid?
|
|
111
|
+
raise MiniMagick::Invalid
|
|
112
|
+
end
|
|
113
|
+
return image
|
|
114
|
+
ensure
|
|
115
|
+
tempfile.close if tempfile
|
|
43
116
|
end
|
|
44
117
|
end
|
|
45
|
-
alias_method :from_file, :open
|
|
46
118
|
end
|
|
47
119
|
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
|
|
120
|
+
# Create a new MiniMagick::Image object
|
|
121
|
+
#
|
|
122
|
+
# _DANGER_: The file location passed in here is the *working copy*. That is, it gets *modified*.
|
|
123
|
+
# you can either copy it yourself or use the MiniMagick::Image.open(path) method which creates a
|
|
124
|
+
# temporary file for you and protects your original!
|
|
125
|
+
#
|
|
126
|
+
# @param input_path [String] The location of an image file
|
|
127
|
+
# @todo Allow this to accept a block that can pass off to Image#combine_options
|
|
128
|
+
def initialize(input_path, tempfile = nil)
|
|
51
129
|
@path = input_path
|
|
52
130
|
@tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected.
|
|
53
131
|
end
|
|
54
|
-
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
# Checks to make sure that MiniMagick can read the file and understand it.
|
|
135
|
+
#
|
|
136
|
+
# This uses the 'identify' command line utility to check the file. If you are having
|
|
137
|
+
# issues with this, then please work directly with the 'identify' command and see if you
|
|
138
|
+
# can figure out what the issue is.
|
|
139
|
+
#
|
|
140
|
+
# @return [Boolean]
|
|
55
141
|
def valid?
|
|
56
142
|
run_command("identify", @path)
|
|
57
143
|
true
|
|
@@ -59,7 +145,21 @@ module MiniMagick
|
|
|
59
145
|
false
|
|
60
146
|
end
|
|
61
147
|
|
|
62
|
-
#
|
|
148
|
+
# A rather low-level way to interact with the "identify" command. No nice API here, just
|
|
149
|
+
# the crazy stuff you find in ImageMagick. See the examples listed!
|
|
150
|
+
#
|
|
151
|
+
# @example
|
|
152
|
+
# image["format"] #=> "TIFF"
|
|
153
|
+
# image["height"] #=> 41 (pixels)
|
|
154
|
+
# image["width"] #=> 50 (pixels)
|
|
155
|
+
# image["dimensions"] #=> [50, 41]
|
|
156
|
+
# image["size"] #=> 2050 (bits)
|
|
157
|
+
# image["original_at"] #=> 2005-02-23 23:17:24 +0000 (Read from Exif data)
|
|
158
|
+
# image["EXIF:ExifVersion"] #=> "0220" (Can read anything from Exif)
|
|
159
|
+
#
|
|
160
|
+
# @param format [String] A format for the "identify" command
|
|
161
|
+
# @see For reference see http://www.imagemagick.org/script/command-line-options.php#format
|
|
162
|
+
# @return [String, Numeric, Array, Time, Object] Depends on the method called! Defaults to String for unknown commands
|
|
63
163
|
def [](value)
|
|
64
164
|
# Why do I go to the trouble of putting in newlines? Because otherwise animated gifs screw everything up
|
|
65
165
|
case value.to_s
|
|
@@ -88,27 +188,41 @@ module MiniMagick
|
|
|
88
188
|
end
|
|
89
189
|
end
|
|
90
190
|
|
|
91
|
-
# Sends raw commands to imagemagick's mogrify command. The image path is automatically appended to the command
|
|
191
|
+
# Sends raw commands to imagemagick's `mogrify` command. The image path is automatically appended to the command.
|
|
192
|
+
#
|
|
193
|
+
# Remember, we are always acting on this instance of the Image when messing with this.
|
|
194
|
+
#
|
|
195
|
+
# @return [String] Whatever the result from the command line is. May not be terribly useful.
|
|
92
196
|
def <<(*args)
|
|
93
197
|
run_command("mogrify", *args << @path)
|
|
94
198
|
end
|
|
95
199
|
|
|
96
|
-
# This is
|
|
200
|
+
# This is used to change the format of the image. That is, from "tiff to jpg" or something like that.
|
|
201
|
+
# Once you run it, the instance is pointing to a new file with a new extension!
|
|
202
|
+
#
|
|
203
|
+
# *DANGER*: This renames the file that the instance is pointing to. So, if you manually opened the
|
|
204
|
+
# file with Image.new(file_path)... then that file is DELETED! If you used Image.open(file) then
|
|
205
|
+
# you are ok. The original file will still be there. But, any changes to it might not be...
|
|
206
|
+
#
|
|
97
207
|
# Formatting an animation into a non-animated type will result in ImageMagick creating multiple
|
|
98
208
|
# pages (starting with 0). You can choose which page you want to manipulate. We default to the
|
|
99
209
|
# first page.
|
|
100
|
-
|
|
210
|
+
#
|
|
211
|
+
# @param format [String] The target format... like 'jpg', 'gif', 'tiff', etc.
|
|
212
|
+
# @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.
|
|
213
|
+
# @return [nil]
|
|
214
|
+
def format(format, page = 0)
|
|
101
215
|
run_command("mogrify", "-format", format, @path)
|
|
102
216
|
|
|
103
217
|
old_path = @path.dup
|
|
104
218
|
@path.sub!(/(\.\w*)?$/, ".#{format}")
|
|
105
|
-
File.delete(old_path)
|
|
219
|
+
File.delete(old_path) if old_path != @path
|
|
106
220
|
|
|
107
221
|
unless File.exists?(@path)
|
|
108
222
|
begin
|
|
109
223
|
FileUtils.copy_file(@path.sub(".#{format}", "-#{page}.#{format}"), @path)
|
|
110
224
|
rescue => ex
|
|
111
|
-
raise
|
|
225
|
+
raise MiniMagick::Error, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
|
|
112
226
|
end
|
|
113
227
|
end
|
|
114
228
|
ensure
|
|
@@ -116,20 +230,36 @@ module MiniMagick
|
|
|
116
230
|
File.unlink(fname)
|
|
117
231
|
end
|
|
118
232
|
end
|
|
119
|
-
|
|
233
|
+
|
|
120
234
|
# Collapse images with sequences to the first frame (ie. animated gifs) and
|
|
121
235
|
# preserve quality
|
|
122
236
|
def collapse!
|
|
123
237
|
run_command("mogrify", "-quality", "100", "#{path}[0]")
|
|
124
238
|
end
|
|
125
239
|
|
|
240
|
+
# Writes the temporary file out to either a file location (by passing in a String) or by
|
|
241
|
+
# passing in a Stream that you can #write(chunk) to repeatedly
|
|
242
|
+
#
|
|
243
|
+
# @param output_to [IOStream, String] Some kind of stream object that needs to be read or a file path as a String
|
|
244
|
+
# @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.
|
|
126
245
|
# Writes the temporary image that we are using for processing to the output path
|
|
127
|
-
def write(
|
|
128
|
-
|
|
129
|
-
|
|
246
|
+
def write(output_to)
|
|
247
|
+
if output_to.kind_of?(String) || !output_to.respond_to?(:write)
|
|
248
|
+
FileUtils.copy_file @path, output_to
|
|
249
|
+
run_command "identify", output_to # Verify that we have a good image
|
|
250
|
+
else # stream
|
|
251
|
+
File.open(@path, "rb") do |f|
|
|
252
|
+
f.binmode
|
|
253
|
+
while chunk = f.read(8192)
|
|
254
|
+
output_to.write(chunk)
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
output_to
|
|
258
|
+
end
|
|
130
259
|
end
|
|
131
260
|
|
|
132
|
-
#
|
|
261
|
+
# Gives you raw image data back
|
|
262
|
+
# @return [String] binary string
|
|
133
263
|
def to_blob
|
|
134
264
|
f = File.new @path
|
|
135
265
|
f.binmode
|
|
@@ -141,57 +271,60 @@ module MiniMagick
|
|
|
141
271
|
# If an unknown method is called then it is sent through the morgrify program
|
|
142
272
|
# Look here to find all the commands (http://www.imagemagick.org/script/mogrify.php)
|
|
143
273
|
def method_missing(symbol, *args)
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if MOGRIFY_COMMANDS.include?(guessed_command_name)
|
|
147
|
-
args.push(@path) # push the path onto the end
|
|
148
|
-
run_command("mogrify", "-#{guessed_command_name}", *args)
|
|
149
|
-
self
|
|
150
|
-
else
|
|
151
|
-
super(symbol, *args)
|
|
274
|
+
combine_options do |c|
|
|
275
|
+
c.method_missing(symbol, *args)
|
|
152
276
|
end
|
|
153
277
|
end
|
|
154
278
|
|
|
155
|
-
# You can use multiple commands together using this method
|
|
279
|
+
# You can use multiple commands together using this method. Very easy to use!
|
|
280
|
+
#
|
|
281
|
+
# @example
|
|
282
|
+
# image.combine_options do |c|
|
|
283
|
+
# c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
|
|
284
|
+
# c.thumbnail "300x500>"
|
|
285
|
+
# c.background background
|
|
286
|
+
# end
|
|
287
|
+
#
|
|
288
|
+
# @yieldparam command [CommandBuilder]
|
|
156
289
|
def combine_options(&block)
|
|
157
290
|
c = CommandBuilder.new('mogrify')
|
|
158
|
-
block.call
|
|
291
|
+
block.call(c)
|
|
159
292
|
c << @path
|
|
160
293
|
run(c)
|
|
161
294
|
end
|
|
162
295
|
|
|
163
296
|
# Check to see if we are running on win32 -- we need to escape things differently
|
|
164
297
|
def windows?
|
|
165
|
-
!(RUBY_PLATFORM =~ /win32/).nil?
|
|
298
|
+
!(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil?
|
|
166
299
|
end
|
|
167
|
-
|
|
300
|
+
|
|
168
301
|
def composite(other_image, output_extension = 'jpg', &block)
|
|
169
302
|
begin
|
|
170
|
-
|
|
171
|
-
|
|
303
|
+
second_tempfile = Tempfile.new(output_extension)
|
|
304
|
+
second_tempfile.binmode
|
|
172
305
|
ensure
|
|
173
|
-
|
|
306
|
+
second_tempfile.close
|
|
174
307
|
end
|
|
175
|
-
|
|
308
|
+
|
|
176
309
|
command = CommandBuilder.new("composite")
|
|
177
310
|
block.call(command) if block
|
|
178
311
|
command.push(other_image.path)
|
|
179
312
|
command.push(self.path)
|
|
180
|
-
command.push(
|
|
181
|
-
|
|
313
|
+
command.push(second_tempfile.path)
|
|
314
|
+
|
|
182
315
|
run(command)
|
|
183
|
-
return Image.new(
|
|
316
|
+
return Image.new(second_tempfile.path, second_tempfile)
|
|
184
317
|
end
|
|
185
318
|
|
|
186
319
|
# Outputs a carriage-return delimited format string for Unix and Windows
|
|
187
320
|
def format_option(format)
|
|
188
|
-
windows? ? "#{format}\\n" : "#{format}\\\\n"
|
|
321
|
+
windows? ? "\"#{format}\\n\"" : "\"#{format}\\\\n\""
|
|
189
322
|
end
|
|
190
323
|
|
|
191
324
|
def run_command(command, *args)
|
|
192
325
|
run(CommandBuilder.new(command, *args))
|
|
193
326
|
end
|
|
194
|
-
|
|
327
|
+
|
|
195
328
|
def run(command_builder)
|
|
196
329
|
command = command_builder.command
|
|
197
330
|
|
|
@@ -200,26 +333,26 @@ module MiniMagick
|
|
|
200
333
|
if sub.exitstatus != 0
|
|
201
334
|
# Clean up after ourselves in case of an error
|
|
202
335
|
destroy!
|
|
203
|
-
|
|
336
|
+
|
|
204
337
|
# Raise the appropriate error
|
|
205
338
|
if sub.output =~ /no decode delegate/i || sub.output =~ /did not return an image/i
|
|
206
339
|
raise Invalid, sub.output
|
|
207
340
|
else
|
|
208
341
|
# TODO: should we do something different if the command times out ...?
|
|
209
342
|
# its definitely better for logging.. otherwise we dont really know
|
|
210
|
-
raise Error, "Command (#{command.inspect}) failed: #{{:status_code => sub.exitstatus, :output => sub.output}.inspect}"
|
|
343
|
+
raise Error, "Command (#{command.inspect.gsub("\\", "")}) failed: #{{:status_code => sub.exitstatus, :output => sub.output}.inspect}"
|
|
211
344
|
end
|
|
212
345
|
else
|
|
213
346
|
sub.output
|
|
214
347
|
end
|
|
215
348
|
end
|
|
216
|
-
|
|
349
|
+
|
|
217
350
|
def destroy!
|
|
218
|
-
return if tempfile.nil?
|
|
219
|
-
File.unlink(tempfile.path)
|
|
351
|
+
return if @tempfile.nil?
|
|
352
|
+
File.unlink(@tempfile.path)
|
|
220
353
|
@tempfile = nil
|
|
221
354
|
end
|
|
222
|
-
|
|
355
|
+
|
|
223
356
|
private
|
|
224
357
|
# Sometimes we get back a list of character values
|
|
225
358
|
def read_character_data(list_of_characters)
|
|
@@ -239,38 +372,39 @@ module MiniMagick
|
|
|
239
372
|
def initialize(command, *options)
|
|
240
373
|
@command = command
|
|
241
374
|
@args = []
|
|
242
|
-
|
|
243
|
-
options.each { |val| push(val) }
|
|
375
|
+
options.each { |arg| push(arg) }
|
|
244
376
|
end
|
|
245
|
-
|
|
377
|
+
|
|
246
378
|
def command
|
|
247
379
|
"#{MiniMagick.processor} #{@command} #{@args.join(' ')}".strip
|
|
248
380
|
end
|
|
249
|
-
|
|
250
|
-
def method_missing(symbol, *
|
|
381
|
+
|
|
382
|
+
def method_missing(symbol, *options)
|
|
251
383
|
guessed_command_name = symbol.to_s.gsub('_','-')
|
|
252
|
-
if
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
push("-#{guessed_command_name}")
|
|
257
|
-
push(args.join(" "))
|
|
258
|
-
else
|
|
259
|
-
push("-#{guessed_command_name} #{args.join(" ")}")
|
|
260
|
-
end
|
|
384
|
+
if guessed_command_name == "format"
|
|
385
|
+
raise Error, "You must call 'format' on the image object directly!"
|
|
386
|
+
elsif MOGRIFY_COMMANDS.include?(guessed_command_name)
|
|
387
|
+
add(guessed_command_name, *options)
|
|
261
388
|
else
|
|
262
389
|
super(symbol, *args)
|
|
263
390
|
end
|
|
264
391
|
end
|
|
265
|
-
|
|
266
|
-
def
|
|
267
|
-
|
|
268
|
-
|
|
392
|
+
|
|
393
|
+
def add(command, *options)
|
|
394
|
+
push "-#{command}"
|
|
395
|
+
if options.any?
|
|
396
|
+
push "\"#{options.join(" ")}\""
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
def push(arg)
|
|
401
|
+
@args << arg.to_s.strip
|
|
269
402
|
end
|
|
270
403
|
alias :<< :push
|
|
271
404
|
|
|
405
|
+
# @deprecated Please don't use the + method its has been deprecated
|
|
272
406
|
def +(value)
|
|
273
|
-
|
|
407
|
+
warn "Warning: The MiniMagick::ComandBuilder#+ command has been deprecated. Please use c << '+#{value}' instead"
|
|
274
408
|
push "+#{value}"
|
|
275
409
|
end
|
|
276
410
|
end
|
|
@@ -8,7 +8,7 @@ class CommandBuilderTest < Test::Unit::TestCase
|
|
|
8
8
|
def test_basic
|
|
9
9
|
c = CommandBuilder.new("test")
|
|
10
10
|
c.resize "30x40"
|
|
11
|
-
assert_equal "-resize 30x40", c.args.join(" ")
|
|
11
|
+
assert_equal "-resize \"30x40\"", c.args.join(" ")
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def test_complicated
|
|
@@ -16,7 +16,7 @@ class CommandBuilderTest < Test::Unit::TestCase
|
|
|
16
16
|
c.resize "30x40"
|
|
17
17
|
c.alpha 1, 3, 4
|
|
18
18
|
c.resize "mome fingo"
|
|
19
|
-
assert_equal "-resize 30x40 -alpha 1 3 4 -resize \"mome fingo\"", c.args.join(" ")
|
|
19
|
+
assert_equal "-resize \"30x40\" -alpha \"1 3 4\" -resize \"mome fingo\"", c.args.join(" ")
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def test_valid_command
|
|
@@ -28,14 +28,6 @@ class CommandBuilderTest < Test::Unit::TestCase
|
|
|
28
28
|
assert true
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
|
-
|
|
32
|
-
def test_full_command
|
|
33
|
-
c = CommandBuilder.new("test")
|
|
34
|
-
c.resize "30x40"
|
|
35
|
-
c.alpha 1, 3, 4
|
|
36
|
-
c.resize "mome fingo"
|
|
37
|
-
assert_equal "test -resize 30x40 -alpha 1 3 4 -resize \"mome fingo\"", c.command
|
|
38
|
-
end
|
|
39
31
|
|
|
40
32
|
def test_dashed
|
|
41
33
|
c = CommandBuilder.new("test")
|
data/test/image_test.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
require 'test/unit'
|
|
3
|
+
require 'mocha'
|
|
4
|
+
require 'pathname'
|
|
5
|
+
require 'stringio'
|
|
3
6
|
require File.expand_path('../../lib/mini_magick', __FILE__)
|
|
4
7
|
|
|
5
8
|
#MiniMagick.processor = :gm
|
|
@@ -10,22 +13,37 @@ class ImageTest < Test::Unit::TestCase
|
|
|
10
13
|
CURRENT_DIR = File.dirname(File.expand_path(__FILE__)) + "/"
|
|
11
14
|
|
|
12
15
|
SIMPLE_IMAGE_PATH = CURRENT_DIR + "simple.gif"
|
|
13
|
-
MINUS_IMAGE_PATH
|
|
14
|
-
TIFF_IMAGE_PATH
|
|
16
|
+
MINUS_IMAGE_PATH = CURRENT_DIR + "simple-minus.gif"
|
|
17
|
+
TIFF_IMAGE_PATH = CURRENT_DIR + "leaves.tiff"
|
|
15
18
|
NOT_AN_IMAGE_PATH = CURRENT_DIR + "not_an_image.php"
|
|
16
|
-
GIF_WITH_JPG_EXT
|
|
17
|
-
EXIF_IMAGE_PATH
|
|
18
|
-
ANIMATION_PATH
|
|
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"
|
|
19
22
|
|
|
20
23
|
def test_image_from_blob
|
|
21
24
|
File.open(SIMPLE_IMAGE_PATH, "rb") do |f|
|
|
22
|
-
image = Image.
|
|
25
|
+
image = Image.read(f.read)
|
|
26
|
+
assert image.valid?
|
|
23
27
|
image.destroy!
|
|
24
28
|
end
|
|
25
29
|
end
|
|
26
30
|
|
|
27
|
-
def
|
|
28
|
-
image = Image.
|
|
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
|
|
29
47
|
image.destroy!
|
|
30
48
|
end
|
|
31
49
|
|
|
@@ -34,6 +52,12 @@ class ImageTest < Test::Unit::TestCase
|
|
|
34
52
|
image.destroy!
|
|
35
53
|
end
|
|
36
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
|
+
|
|
37
61
|
def test_image_write
|
|
38
62
|
output_path = "output.gif"
|
|
39
63
|
begin
|
|
@@ -47,12 +71,21 @@ class ImageTest < Test::Unit::TestCase
|
|
|
47
71
|
image.destroy!
|
|
48
72
|
end
|
|
49
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
|
+
|
|
50
83
|
def test_not_an_image
|
|
51
84
|
image = Image.new(NOT_AN_IMAGE_PATH)
|
|
52
85
|
assert_equal false, image.valid?
|
|
53
86
|
image.destroy!
|
|
54
87
|
end
|
|
55
|
-
|
|
88
|
+
|
|
56
89
|
def test_throw_on_openining_not_an_image
|
|
57
90
|
assert_raise(MiniMagick::Invalid) do
|
|
58
91
|
image = Image.open(NOT_AN_IMAGE_PATH)
|
|
@@ -77,19 +110,6 @@ class ImageTest < Test::Unit::TestCase
|
|
|
77
110
|
image.destroy!
|
|
78
111
|
end
|
|
79
112
|
|
|
80
|
-
# def test_animation_pages
|
|
81
|
-
# image = Image.from_file(ANIMATION_PATH)
|
|
82
|
-
# image.format "png", 0
|
|
83
|
-
# assert_equal "png", image[:format].downcase
|
|
84
|
-
# image.destroy!
|
|
85
|
-
# end
|
|
86
|
-
|
|
87
|
-
# def test_animation_size
|
|
88
|
-
# image = Image.from_file(ANIMATION_PATH)
|
|
89
|
-
# assert_equal image[:size], 76631
|
|
90
|
-
# image.destroy!
|
|
91
|
-
# end
|
|
92
|
-
|
|
93
113
|
def test_gif_with_jpg_format
|
|
94
114
|
image = Image.new(GIF_WITH_JPG_EXT)
|
|
95
115
|
assert_equal "gif", image[:format].downcase
|
|
@@ -97,7 +117,7 @@ class ImageTest < Test::Unit::TestCase
|
|
|
97
117
|
end
|
|
98
118
|
|
|
99
119
|
def test_image_resize
|
|
100
|
-
image = Image.
|
|
120
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
101
121
|
image.resize "20x30!"
|
|
102
122
|
|
|
103
123
|
assert_equal 20, image[:width]
|
|
@@ -107,7 +127,7 @@ class ImageTest < Test::Unit::TestCase
|
|
|
107
127
|
end
|
|
108
128
|
|
|
109
129
|
def test_image_resize_with_minimum
|
|
110
|
-
image = Image.
|
|
130
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
111
131
|
original_width, original_height = image[:width], image[:height]
|
|
112
132
|
image.resize "#{original_width + 10}x#{original_height + 10}>"
|
|
113
133
|
|
|
@@ -117,10 +137,10 @@ class ImageTest < Test::Unit::TestCase
|
|
|
117
137
|
end
|
|
118
138
|
|
|
119
139
|
def test_image_combine_options_resize_blur
|
|
120
|
-
image = Image.
|
|
140
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
121
141
|
image.combine_options do |c|
|
|
122
142
|
c.resize "20x30!"
|
|
123
|
-
c.blur 50
|
|
143
|
+
c.blur "50"
|
|
124
144
|
end
|
|
125
145
|
|
|
126
146
|
assert_equal 20, image[:width]
|
|
@@ -128,56 +148,59 @@ class ImageTest < Test::Unit::TestCase
|
|
|
128
148
|
assert_match(/^gif$/i, image[:format])
|
|
129
149
|
image.destroy!
|
|
130
150
|
end
|
|
131
|
-
|
|
151
|
+
|
|
132
152
|
def test_image_combine_options_with_filename_with_minusses_in_it
|
|
133
|
-
image = Image.
|
|
153
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
154
|
+
background = "#000000"
|
|
134
155
|
assert_nothing_raised do
|
|
135
156
|
image.combine_options do |c|
|
|
136
157
|
c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
|
|
158
|
+
c.thumbnail "300x500>"
|
|
159
|
+
c.background background
|
|
137
160
|
end
|
|
138
161
|
end
|
|
139
162
|
image.destroy!
|
|
140
163
|
end
|
|
141
164
|
|
|
142
165
|
def test_exif
|
|
143
|
-
image = Image.
|
|
166
|
+
image = Image.open(EXIF_IMAGE_PATH)
|
|
144
167
|
assert_equal('0220', image["exif:ExifVersion"])
|
|
145
|
-
image = Image.
|
|
168
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
146
169
|
assert_equal('', image["EXIF:ExifVersion"])
|
|
147
170
|
image.destroy!
|
|
148
171
|
end
|
|
149
172
|
|
|
150
173
|
def test_original_at
|
|
151
|
-
image = Image.
|
|
174
|
+
image = Image.open(EXIF_IMAGE_PATH)
|
|
152
175
|
assert_equal(Time.local('2005', '2', '23', '23', '17', '24'), image[:original_at])
|
|
153
|
-
image = Image.
|
|
176
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
154
177
|
assert_nil(image[:original_at])
|
|
155
178
|
image.destroy!
|
|
156
179
|
end
|
|
157
180
|
|
|
158
181
|
def test_tempfile_at_path
|
|
159
|
-
image = Image.
|
|
160
|
-
assert_equal image.path, image.tempfile.path
|
|
182
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
183
|
+
assert_equal image.path, image.instance_eval("@tempfile.path")
|
|
161
184
|
image.destroy!
|
|
162
185
|
end
|
|
163
186
|
|
|
164
187
|
def test_tempfile_at_path_after_format
|
|
165
|
-
image = Image.
|
|
188
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
166
189
|
image.format('png')
|
|
167
|
-
assert_equal image.path, image.tempfile.path
|
|
190
|
+
assert_equal image.path, image.instance_eval("@tempfile.path")
|
|
168
191
|
image.destroy!
|
|
169
192
|
end
|
|
170
193
|
|
|
171
194
|
def test_previous_tempfile_deleted_after_format
|
|
172
|
-
image = Image.
|
|
195
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
173
196
|
before = image.path.dup
|
|
174
197
|
image.format('png')
|
|
175
198
|
assert !File.exist?(before)
|
|
176
199
|
image.destroy!
|
|
177
200
|
end
|
|
178
|
-
|
|
201
|
+
|
|
179
202
|
def test_bad_method_bug
|
|
180
|
-
image = Image.
|
|
203
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
181
204
|
begin
|
|
182
205
|
image.to_blog
|
|
183
206
|
rescue NoMethodError
|
|
@@ -187,19 +210,55 @@ class ImageTest < Test::Unit::TestCase
|
|
|
187
210
|
assert true #we made it this far without error
|
|
188
211
|
image.destroy!
|
|
189
212
|
end
|
|
190
|
-
|
|
213
|
+
|
|
191
214
|
def test_simple_composite
|
|
192
|
-
image = Image.
|
|
215
|
+
image = Image.open(EXIF_IMAGE_PATH)
|
|
193
216
|
result = image.composite(Image.open(TIFF_IMAGE_PATH)) do |c|
|
|
194
217
|
c.gravity "center"
|
|
195
218
|
end
|
|
196
219
|
assert `diff -s #{result.path} test/composited.jpg`.include?("identical")
|
|
197
220
|
end
|
|
198
221
|
|
|
199
|
-
#
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
+
|
|
205
264
|
end
|
data/test/leaves.tiff
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -3,10 +3,9 @@ name: mini_magick
|
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
|
5
5
|
segments:
|
|
6
|
-
-
|
|
6
|
+
- 3
|
|
7
7
|
- 0
|
|
8
|
-
|
|
9
|
-
version: 2.0.0
|
|
8
|
+
version: "3.0"
|
|
10
9
|
platform: ruby
|
|
11
10
|
authors:
|
|
12
11
|
- Corey Johnson
|
|
@@ -16,14 +15,13 @@ autorequire:
|
|
|
16
15
|
bindir: bin
|
|
17
16
|
cert_chain: []
|
|
18
17
|
|
|
19
|
-
date: 2010-
|
|
18
|
+
date: 2010-10-28 00:00:00 +01:00
|
|
20
19
|
default_executable:
|
|
21
20
|
dependencies:
|
|
22
21
|
- !ruby/object:Gem::Dependency
|
|
23
22
|
name: subexec
|
|
24
23
|
prerelease: false
|
|
25
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
|
26
|
-
none: false
|
|
27
25
|
requirements:
|
|
28
26
|
- - ~>
|
|
29
27
|
- !ruby/object:Gem::Version
|
|
@@ -34,6 +32,20 @@ dependencies:
|
|
|
34
32
|
version: 0.0.4
|
|
35
33
|
type: :runtime
|
|
36
34
|
version_requirements: *id001
|
|
35
|
+
- !ruby/object:Gem::Dependency
|
|
36
|
+
name: mocha
|
|
37
|
+
prerelease: false
|
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ~>
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
segments:
|
|
43
|
+
- 0
|
|
44
|
+
- 9
|
|
45
|
+
- 9
|
|
46
|
+
version: 0.9.9
|
|
47
|
+
type: :development
|
|
48
|
+
version_requirements: *id002
|
|
37
49
|
description: ""
|
|
38
50
|
email:
|
|
39
51
|
- probablycorey@gmail.com
|
|
@@ -52,16 +64,6 @@ files:
|
|
|
52
64
|
- Rakefile
|
|
53
65
|
- lib/mini_gmagick.rb
|
|
54
66
|
- lib/mini_magick.rb
|
|
55
|
-
- test/actually_a_gif.jpg
|
|
56
|
-
- test/animation.gif
|
|
57
|
-
- test/command_builder_test.rb
|
|
58
|
-
- test/composited.jpg
|
|
59
|
-
- test/image_test.rb
|
|
60
|
-
- test/leaves.tiff
|
|
61
|
-
- test/not_an_image.php
|
|
62
|
-
- test/simple-minus.gif
|
|
63
|
-
- test/simple.gif
|
|
64
|
-
- test/trogdor.jpg
|
|
65
67
|
has_rdoc: true
|
|
66
68
|
homepage: http://github.com/probablycorey/mini_magick
|
|
67
69
|
licenses: []
|
|
@@ -72,7 +74,6 @@ rdoc_options: []
|
|
|
72
74
|
require_paths:
|
|
73
75
|
- lib
|
|
74
76
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
|
-
none: false
|
|
76
77
|
requirements:
|
|
77
78
|
- - ">="
|
|
78
79
|
- !ruby/object:Gem::Version
|
|
@@ -80,7 +81,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
80
81
|
- 0
|
|
81
82
|
version: "0"
|
|
82
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
|
-
none: false
|
|
84
84
|
requirements:
|
|
85
85
|
- - ">="
|
|
86
86
|
- !ruby/object:Gem::Version
|
|
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
90
90
|
requirements: []
|
|
91
91
|
|
|
92
92
|
rubyforge_project:
|
|
93
|
-
rubygems_version: 1.3.
|
|
93
|
+
rubygems_version: 1.3.6
|
|
94
94
|
signing_key:
|
|
95
95
|
specification_version: 3
|
|
96
96
|
summary: Manipulate images with minimal use of memory via ImageMagick / GraphicsMagick
|