mini_magick 2.1 → 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 +195 -55
- data/test/image_test.rb +86 -31
- data/test/leaves.tiff +0 -0
- metadata +19 -22
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
|
|
@@ -146,7 +276,16 @@ module MiniMagick
|
|
|
146
276
|
end
|
|
147
277
|
end
|
|
148
278
|
|
|
149
|
-
# 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]
|
|
150
289
|
def combine_options(&block)
|
|
151
290
|
c = CommandBuilder.new('mogrify')
|
|
152
291
|
block.call(c)
|
|
@@ -156,25 +295,25 @@ module MiniMagick
|
|
|
156
295
|
|
|
157
296
|
# Check to see if we are running on win32 -- we need to escape things differently
|
|
158
297
|
def windows?
|
|
159
|
-
!(RUBY_PLATFORM =~ /win32/).nil?
|
|
298
|
+
!(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil?
|
|
160
299
|
end
|
|
161
|
-
|
|
300
|
+
|
|
162
301
|
def composite(other_image, output_extension = 'jpg', &block)
|
|
163
302
|
begin
|
|
164
|
-
|
|
165
|
-
|
|
303
|
+
second_tempfile = Tempfile.new(output_extension)
|
|
304
|
+
second_tempfile.binmode
|
|
166
305
|
ensure
|
|
167
|
-
|
|
306
|
+
second_tempfile.close
|
|
168
307
|
end
|
|
169
|
-
|
|
308
|
+
|
|
170
309
|
command = CommandBuilder.new("composite")
|
|
171
310
|
block.call(command) if block
|
|
172
311
|
command.push(other_image.path)
|
|
173
312
|
command.push(self.path)
|
|
174
|
-
command.push(
|
|
175
|
-
|
|
313
|
+
command.push(second_tempfile.path)
|
|
314
|
+
|
|
176
315
|
run(command)
|
|
177
|
-
return Image.new(
|
|
316
|
+
return Image.new(second_tempfile.path, second_tempfile)
|
|
178
317
|
end
|
|
179
318
|
|
|
180
319
|
# Outputs a carriage-return delimited format string for Unix and Windows
|
|
@@ -185,7 +324,7 @@ module MiniMagick
|
|
|
185
324
|
def run_command(command, *args)
|
|
186
325
|
run(CommandBuilder.new(command, *args))
|
|
187
326
|
end
|
|
188
|
-
|
|
327
|
+
|
|
189
328
|
def run(command_builder)
|
|
190
329
|
command = command_builder.command
|
|
191
330
|
|
|
@@ -194,7 +333,7 @@ module MiniMagick
|
|
|
194
333
|
if sub.exitstatus != 0
|
|
195
334
|
# Clean up after ourselves in case of an error
|
|
196
335
|
destroy!
|
|
197
|
-
|
|
336
|
+
|
|
198
337
|
# Raise the appropriate error
|
|
199
338
|
if sub.output =~ /no decode delegate/i || sub.output =~ /did not return an image/i
|
|
200
339
|
raise Invalid, sub.output
|
|
@@ -207,13 +346,13 @@ module MiniMagick
|
|
|
207
346
|
sub.output
|
|
208
347
|
end
|
|
209
348
|
end
|
|
210
|
-
|
|
349
|
+
|
|
211
350
|
def destroy!
|
|
212
|
-
return if tempfile.nil?
|
|
213
|
-
File.unlink(tempfile.path)
|
|
351
|
+
return if @tempfile.nil?
|
|
352
|
+
File.unlink(@tempfile.path)
|
|
214
353
|
@tempfile = nil
|
|
215
354
|
end
|
|
216
|
-
|
|
355
|
+
|
|
217
356
|
private
|
|
218
357
|
# Sometimes we get back a list of character values
|
|
219
358
|
def read_character_data(list_of_characters)
|
|
@@ -235,11 +374,11 @@ module MiniMagick
|
|
|
235
374
|
@args = []
|
|
236
375
|
options.each { |arg| push(arg) }
|
|
237
376
|
end
|
|
238
|
-
|
|
377
|
+
|
|
239
378
|
def command
|
|
240
379
|
"#{MiniMagick.processor} #{@command} #{@args.join(' ')}".strip
|
|
241
380
|
end
|
|
242
|
-
|
|
381
|
+
|
|
243
382
|
def method_missing(symbol, *options)
|
|
244
383
|
guessed_command_name = symbol.to_s.gsub('_','-')
|
|
245
384
|
if guessed_command_name == "format"
|
|
@@ -250,21 +389,22 @@ module MiniMagick
|
|
|
250
389
|
super(symbol, *args)
|
|
251
390
|
end
|
|
252
391
|
end
|
|
253
|
-
|
|
392
|
+
|
|
254
393
|
def add(command, *options)
|
|
255
394
|
push "-#{command}"
|
|
256
395
|
if options.any?
|
|
257
396
|
push "\"#{options.join(" ")}\""
|
|
258
397
|
end
|
|
259
398
|
end
|
|
260
|
-
|
|
399
|
+
|
|
261
400
|
def push(arg)
|
|
262
|
-
@args << arg.strip
|
|
401
|
+
@args << arg.to_s.strip
|
|
263
402
|
end
|
|
264
403
|
alias :<< :push
|
|
265
404
|
|
|
405
|
+
# @deprecated Please don't use the + method its has been deprecated
|
|
266
406
|
def +(value)
|
|
267
|
-
|
|
407
|
+
warn "Warning: The MiniMagick::ComandBuilder#+ command has been deprecated. Please use c << '+#{value}' instead"
|
|
268
408
|
push "+#{value}"
|
|
269
409
|
end
|
|
270
410
|
end
|
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)
|
|
@@ -84,7 +117,7 @@ class ImageTest < Test::Unit::TestCase
|
|
|
84
117
|
end
|
|
85
118
|
|
|
86
119
|
def test_image_resize
|
|
87
|
-
image = Image.
|
|
120
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
88
121
|
image.resize "20x30!"
|
|
89
122
|
|
|
90
123
|
assert_equal 20, image[:width]
|
|
@@ -94,7 +127,7 @@ class ImageTest < Test::Unit::TestCase
|
|
|
94
127
|
end
|
|
95
128
|
|
|
96
129
|
def test_image_resize_with_minimum
|
|
97
|
-
image = Image.
|
|
130
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
98
131
|
original_width, original_height = image[:width], image[:height]
|
|
99
132
|
image.resize "#{original_width + 10}x#{original_height + 10}>"
|
|
100
133
|
|
|
@@ -104,7 +137,7 @@ class ImageTest < Test::Unit::TestCase
|
|
|
104
137
|
end
|
|
105
138
|
|
|
106
139
|
def test_image_combine_options_resize_blur
|
|
107
|
-
image = Image.
|
|
140
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
108
141
|
image.combine_options do |c|
|
|
109
142
|
c.resize "20x30!"
|
|
110
143
|
c.blur "50"
|
|
@@ -115,9 +148,9 @@ class ImageTest < Test::Unit::TestCase
|
|
|
115
148
|
assert_match(/^gif$/i, image[:format])
|
|
116
149
|
image.destroy!
|
|
117
150
|
end
|
|
118
|
-
|
|
151
|
+
|
|
119
152
|
def test_image_combine_options_with_filename_with_minusses_in_it
|
|
120
|
-
image = Image.
|
|
153
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
121
154
|
background = "#000000"
|
|
122
155
|
assert_nothing_raised do
|
|
123
156
|
image.combine_options do |c|
|
|
@@ -130,44 +163,44 @@ class ImageTest < Test::Unit::TestCase
|
|
|
130
163
|
end
|
|
131
164
|
|
|
132
165
|
def test_exif
|
|
133
|
-
image = Image.
|
|
166
|
+
image = Image.open(EXIF_IMAGE_PATH)
|
|
134
167
|
assert_equal('0220', image["exif:ExifVersion"])
|
|
135
|
-
image = Image.
|
|
168
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
136
169
|
assert_equal('', image["EXIF:ExifVersion"])
|
|
137
170
|
image.destroy!
|
|
138
171
|
end
|
|
139
172
|
|
|
140
173
|
def test_original_at
|
|
141
|
-
image = Image.
|
|
174
|
+
image = Image.open(EXIF_IMAGE_PATH)
|
|
142
175
|
assert_equal(Time.local('2005', '2', '23', '23', '17', '24'), image[:original_at])
|
|
143
|
-
image = Image.
|
|
176
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
144
177
|
assert_nil(image[:original_at])
|
|
145
178
|
image.destroy!
|
|
146
179
|
end
|
|
147
180
|
|
|
148
181
|
def test_tempfile_at_path
|
|
149
|
-
image = Image.
|
|
150
|
-
assert_equal image.path, image.tempfile.path
|
|
182
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
183
|
+
assert_equal image.path, image.instance_eval("@tempfile.path")
|
|
151
184
|
image.destroy!
|
|
152
185
|
end
|
|
153
186
|
|
|
154
187
|
def test_tempfile_at_path_after_format
|
|
155
|
-
image = Image.
|
|
188
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
156
189
|
image.format('png')
|
|
157
|
-
assert_equal image.path, image.tempfile.path
|
|
190
|
+
assert_equal image.path, image.instance_eval("@tempfile.path")
|
|
158
191
|
image.destroy!
|
|
159
192
|
end
|
|
160
193
|
|
|
161
194
|
def test_previous_tempfile_deleted_after_format
|
|
162
|
-
image = Image.
|
|
195
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
163
196
|
before = image.path.dup
|
|
164
197
|
image.format('png')
|
|
165
198
|
assert !File.exist?(before)
|
|
166
199
|
image.destroy!
|
|
167
200
|
end
|
|
168
|
-
|
|
201
|
+
|
|
169
202
|
def test_bad_method_bug
|
|
170
|
-
image = Image.
|
|
203
|
+
image = Image.open(TIFF_IMAGE_PATH)
|
|
171
204
|
begin
|
|
172
205
|
image.to_blog
|
|
173
206
|
rescue NoMethodError
|
|
@@ -177,17 +210,18 @@ class ImageTest < Test::Unit::TestCase
|
|
|
177
210
|
assert true #we made it this far without error
|
|
178
211
|
image.destroy!
|
|
179
212
|
end
|
|
180
|
-
|
|
213
|
+
|
|
181
214
|
def test_simple_composite
|
|
182
|
-
image = Image.
|
|
215
|
+
image = Image.open(EXIF_IMAGE_PATH)
|
|
183
216
|
result = image.composite(Image.open(TIFF_IMAGE_PATH)) do |c|
|
|
184
217
|
c.gravity "center"
|
|
185
218
|
end
|
|
186
219
|
assert `diff -s #{result.path} test/composited.jpg`.include?("identical")
|
|
187
220
|
end
|
|
188
|
-
|
|
221
|
+
|
|
222
|
+
# http://github.com/probablycorey/mini_magick/issues#issue/8
|
|
189
223
|
def test_issue_8
|
|
190
|
-
image = Image.
|
|
224
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
191
225
|
assert_nothing_raised do
|
|
192
226
|
image.combine_options do |c|
|
|
193
227
|
c.sample "50%"
|
|
@@ -196,9 +230,18 @@ class ImageTest < Test::Unit::TestCase
|
|
|
196
230
|
end
|
|
197
231
|
image.destroy!
|
|
198
232
|
end
|
|
199
|
-
|
|
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
|
+
|
|
200
243
|
def test_throw_format_error
|
|
201
|
-
image = Image.
|
|
244
|
+
image = Image.open(SIMPLE_IMAGE_PATH)
|
|
202
245
|
assert_raise MiniMagick::Error do
|
|
203
246
|
image.combine_options do |c|
|
|
204
247
|
c.format "png"
|
|
@@ -206,4 +249,16 @@ class ImageTest < Test::Unit::TestCase
|
|
|
206
249
|
end
|
|
207
250
|
image.destroy!
|
|
208
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
|
+
|
|
209
264
|
end
|
data/test/leaves.tiff
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mini_magick
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash: 1
|
|
5
4
|
prerelease: false
|
|
6
5
|
segments:
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
version: "
|
|
6
|
+
- 3
|
|
7
|
+
- 0
|
|
8
|
+
version: "3.0"
|
|
10
9
|
platform: ruby
|
|
11
10
|
authors:
|
|
12
11
|
- Corey Johnson
|
|
@@ -16,18 +15,16 @@ 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
|
|
30
|
-
hash: 23
|
|
31
28
|
segments:
|
|
32
29
|
- 0
|
|
33
30
|
- 0
|
|
@@ -35,6 +32,20 @@ dependencies:
|
|
|
35
32
|
version: 0.0.4
|
|
36
33
|
type: :runtime
|
|
37
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
|
|
38
49
|
description: ""
|
|
39
50
|
email:
|
|
40
51
|
- probablycorey@gmail.com
|
|
@@ -53,16 +64,6 @@ files:
|
|
|
53
64
|
- Rakefile
|
|
54
65
|
- lib/mini_gmagick.rb
|
|
55
66
|
- lib/mini_magick.rb
|
|
56
|
-
- test/actually_a_gif.jpg
|
|
57
|
-
- test/animation.gif
|
|
58
|
-
- test/command_builder_test.rb
|
|
59
|
-
- test/composited.jpg
|
|
60
|
-
- test/image_test.rb
|
|
61
|
-
- test/leaves.tiff
|
|
62
|
-
- test/not_an_image.php
|
|
63
|
-
- test/simple-minus.gif
|
|
64
|
-
- test/simple.gif
|
|
65
|
-
- test/trogdor.jpg
|
|
66
67
|
has_rdoc: true
|
|
67
68
|
homepage: http://github.com/probablycorey/mini_magick
|
|
68
69
|
licenses: []
|
|
@@ -73,27 +74,23 @@ rdoc_options: []
|
|
|
73
74
|
require_paths:
|
|
74
75
|
- lib
|
|
75
76
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
|
-
none: false
|
|
77
77
|
requirements:
|
|
78
78
|
- - ">="
|
|
79
79
|
- !ruby/object:Gem::Version
|
|
80
|
-
hash: 3
|
|
81
80
|
segments:
|
|
82
81
|
- 0
|
|
83
82
|
version: "0"
|
|
84
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
|
-
none: false
|
|
86
84
|
requirements:
|
|
87
85
|
- - ">="
|
|
88
86
|
- !ruby/object:Gem::Version
|
|
89
|
-
hash: 3
|
|
90
87
|
segments:
|
|
91
88
|
- 0
|
|
92
89
|
version: "0"
|
|
93
90
|
requirements: []
|
|
94
91
|
|
|
95
92
|
rubyforge_project:
|
|
96
|
-
rubygems_version: 1.3.
|
|
93
|
+
rubygems_version: 1.3.6
|
|
97
94
|
signing_key:
|
|
98
95
|
specification_version: 3
|
|
99
96
|
summary: Manipulate images with minimal use of memory via ImageMagick / GraphicsMagick
|