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