lib/mini_magick.rb in mini_magick-2.3 vs lib/mini_magick.rb in mini_magick-3.0
- old
+ new
@@ -1,13 +1,25 @@
require 'tempfile'
require 'subexec'
-require 'open-uri'
module MiniMagick
class << self
attr_accessor :processor
attr_accessor :timeout
+
+
+ # Experimental method for automatically selecting a processor
+ # such as gm. Only works on *nix.
+ #
+ # TODO: Write tests for this and figure out what platforms it supports
+ def choose_processor
+ if `type -P mogrify`.size > 0
+ return
+ elsif `type -P gm`.size > 0
+ self.processor = "gm"
+ end
+ end
end
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}
class Error < RuntimeError; end
@@ -53,23 +65,19 @@
#
# Use this if you don't want to overwrite the image file.
#
# Extension is either guessed from the path or you can specify it as a second parameter.
#
- # If you pass in what looks like a URL, we will see if Kernel#open exists. If it doesn't
- # then we require 'open-uri'. That way, if you have a work-alike library, we won't demolish it.
- # Open-uri never gets required unless you pass in something with "://" in it.
+ # If you pass in what looks like a URL, we require 'open-uri' before opening it.
#
# @param file_or_url [String] Either a local file path or a URL that open-uri can read
# @param ext [String] Specify the extension you want to read it as
# @return [Image] The loaded image
def open(file_or_url, ext = File.extname(file_or_url))
file_or_url = file_or_url.to_s # Force it to be a String... hell or highwater
if file_or_url.include?("://")
- if !Kernel.respond_to?("open")
- require 'open-uri'
- end
+ require 'open-uri'
self.read(Kernel::open(file_or_url), ext)
else
File.open(file_or_url, "rb") do |f|
self.read(f, ext)
end
@@ -109,11 +117,11 @@
end
end
# Create a new MiniMagick::Image object
#
- # _DANGER_: The file location passed in here is the *working copy*. That is, it gets *modified*.
+ # _DANGER_: The file location passed in here is the *working copy*. That is, it gets *modified*.
# you can either copy it yourself or use the MiniMagick::Image.open(path) method which creates a
# temporary file for you and protects your original!
#
# @param input_path [String] The location of an image file
# @todo Allow this to accept a block that can pass off to Image#combine_options
@@ -212,11 +220,11 @@
unless File.exists?(@path)
begin
FileUtils.copy_file(@path.sub(".#{format}", "-#{page}.#{format}"), @path)
rescue => ex
- raise MiniMagickError, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
+ raise MiniMagick::Error, "Unable to format to #{format}; #{ex}" unless File.exist?(@path)
end
end
ensure
Dir[@path.sub(/(\.\w+)?$/, "-[0-9]*.#{format}")].each do |fname|
File.unlink(fname)
@@ -227,14 +235,29 @@
# preserve quality
def collapse!
run_command("mogrify", "-quality", "100", "#{path}[0]")
end
+ # Writes the temporary file out to either a file location (by passing in a String) or by
+ # passing in a Stream that you can #write(chunk) to repeatedly
+ #
+ # @param output_to [IOStream, String] Some kind of stream object that needs to be read or a file path as a String
+ # @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.
# Writes the temporary image that we are using for processing to the output path
- def write(output_path)
- FileUtils.copy_file @path, output_path
- run_command "identify", output_path # Verify that we have a good image
+ def write(output_to)
+ if output_to.kind_of?(String) || !output_to.respond_to?(:write)
+ FileUtils.copy_file @path, output_to
+ run_command "identify", output_to # Verify that we have a good image
+ else # stream
+ File.open(@path, "rb") do |f|
+ f.binmode
+ while chunk = f.read(8192)
+ output_to.write(chunk)
+ end
+ end
+ output_to
+ end
end
# Gives you raw image data back
# @return [String] binary string
def to_blob
@@ -253,27 +276,27 @@
end
end
# You can use multiple commands together using this method. Very easy to use!
#
- # @example
+ # @example
# image.combine_options do |c|
# c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
# c.thumbnail "300x500>"
# c.background background
# end
#
- # @yieldparam command [CommandBuilder]
+ # @yieldparam command [CommandBuilder]
def combine_options(&block)
c = CommandBuilder.new('mogrify')
block.call(c)
c << @path
run(c)
end
# Check to see if we are running on win32 -- we need to escape things differently
def windows?
- !(RUBY_PLATFORM =~ /win32/).nil?
+ !(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil?
end
def composite(other_image, output_extension = 'jpg', &block)
begin
second_tempfile = Tempfile.new(output_extension)