lib/mojo_magick.rb in mojo_magick-0.4.3 vs lib/mojo_magick.rb in mojo_magick-0.4.4
- old
+ new
@@ -1,8 +1,11 @@
-require File::expand_path(File::join(File::dirname(__FILE__), 'image_resources'))
+cwd = File::dirname(__FILE__)
+require File::join(cwd, 'image_resources')
+require File::join(cwd, 'mojo_magick/opt_builder')
require 'tempfile'
+
# MojoMagick is a stateless set of module methods which present a convient interface
# for accessing common tasks for ImageMagick command line library.
#
# MojoMagick is specifically designed to be efficient and simple and most importantly
# to not leak any memory. For complex image operations, you will find MojoMagick limited.
@@ -20,21 +23,21 @@
# c.crop '250x250+0+0'
# c.repage!
# c.strip
# c.set 'comment', 'my favorite file'
# end
-#
+#
# Equivalent to:
#
# MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0 +repage -strip -set comment "my favorite file" dest.jpg')
#
# Example #mogrify usage:
#
# MojoMagick::mogrify('image.jpg') {|i| i.shave '10x10'}
#
# Equivalent to:
-#
+#
# MojoMagick::raw_command('mogrify', '-shave 10x10 image.jpg')
#
# Example showing some additional options:
#
# MojoMagick::convert do |c|
@@ -104,11 +107,11 @@
scale_options << ">" unless options[:shrink_only].nil?
scale_options << "<" unless options[:expand_only].nil?
scale_options << "!" unless options[:absolute_aspect].nil?
scale_options << "^" unless options[:fill].nil?
scale_options = scale_options.join
-
+
extras = []
if !options[:width].nil? && !options[:height].nil?
geometry = "#{options[:width]}X#{options[:height]}"
elsif !options[:percent].nil?
geometry = "#{options[:percent]}%"
@@ -150,11 +153,11 @@
yield opts
opts.file dest if dest
raw_command('mogrify', opts.to_s)
end
-
+
def MojoMagick::tempfile(*opts)
begin
data = opts[0]
rest = opts[1]
ext = rest && rest[:format]
@@ -167,79 +170,6 @@
end
ensure
file.close
end
- # Option builder used in #convert and #mogrify helpers.
- class OptBuilder
- def initialize
- @opts = []
- end
-
- # Add command-line options with no processing
- def <<(arg)
- if arg.is_a?(Array)
- @opts += arg
- else
- @opts << arg
- end
- self
- end
-
- # Add files to command line, formatted if necessary
- def file(*args)
- args.each do |arg|
- add_formatted arg
- end
- self
- end
- alias files file
-
- # Create a temporary file for the given image and add to command line
- def format(*args)
- @opts << '-format'
- args.each do |arg|
- add_formatted arg
- end
- end
-
- def blob(*args)
- data = args[0]
- opts = args[1] || {}
- opts.each do |k,v|
- send(k.to_s,v.to_s)
- end
- tmpfile = MojoMagick::tempfile(data, opts)
- file tmpfile
- end
-
- # Generic commands. Arguments will be formatted if necessary
- def method_missing(command, *args)
- if command.to_s[-1, 1] == '!'
- @opts << "+#{command.to_s.chop}"
- else
- @opts << "-#{command}"
- end
- args.each do |arg|
- add_formatted arg
- end
- self
- end
-
- def to_s
- @opts.join ' '
- end
-
- protected
- def add_formatted(arg)
- # Quote anything that would cause problems on *nix or windows
- if arg =~ /[<>^|&();` ]/
- @opts << "\"#{arg.gsub('"', '\"')}\""
- else
- @opts << arg
- end
- end
- end
-
end # MojoMagick
-
-