lib/mini_magick.rb in mini_magick-1.1.0 vs lib/mini_magick.rb in mini_magick-1.2.0

- old
+ new

@@ -1,13 +1,14 @@ require "open-uri" require "tempfile" require "stringio" +require "fileutils" module MiniMagick class MiniMagickError < Exception; end - VERSION = '1.1.0' + VERSION = '1.2.0' class Image attr :path # Class Methods @@ -15,15 +16,14 @@ class <<self def from_blob(blob) begin tmp = Tempfile.new("minimagic") tmp.binmode - tmp.write(blob) + tmp.write(blob) ensure tmp.close end - return self.new(tmp.path) end # Use this if you don't want to overwrite the image file def from_file(image_path) @@ -44,15 +44,15 @@ def [](value) # Why do I go to the trouble of putting in newline chars? Because otherwise animated gifs screw everything up case value.to_s when "format" - run_command("identify", "-format", "%m\\\\n", @path).split("\n")[0] + run_command("identify", "-format", format_option("%m"), @path).split("\n")[0] when "height" - run_command("identify", "-format", "%h\\\\n", @path).split("\n")[0].to_i + run_command("identify", "-format", format_option("%h"), @path).split("\n")[0].to_i when "width" - run_command("identify", "-format", "%w\\\\n", @path).split("\n")[0].to_i + run_command("identify", "-format", format_option("%w"), @path).split("\n")[0].to_i when "original_at" # Get the EXIF original capture as a Time object Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil when /^EXIF\:/i run_command('identify', '-format', "\"%[#{value}]\"", @path).chop @@ -66,17 +66,15 @@ run_command("mogrify", "-format", format, @path) @path = @path.sub(/\.\w+$/, ".#{format}") raise "Unable to format to #{format}" unless File.exists?(@path) end - + + # Writes the temporary image that we are using for processing to the output path def write(output_path) - open(output_path, "wb") do |output_file| - open(@path, "rb") do |image_file| - output_file.write(image_file.read) - end - end + FileUtils.copy_file @path, output_path + run_command "identify #{output_path}" # Verify that we have a good image end # If an unknown method is called then it is sent through the morgrify program # Look here to find all the commands (http://www.imagemagick.org/script/mogrify.php) def method_missing(symbol, *args) @@ -92,12 +90,34 @@ end # Private (Don't look in here!) # ----------------------------- private + + # Check to see if we are running on win32 -- we need to escape things differently + def windows? + !(RUBY_PLATFORM =~ /win32/).nil? + end + + # Outputs a carriage-return delimited format string for Unix and Windows + def format_option(format) + if windows? + format = "#{format}\\n" + else + format = "#{format}\\\\n" + end + end def run_command(command, *args) - args = args.collect {|a| a.to_s} + args = args.collect do |a| + a = a.to_s + unless a[0,1] == '-' # don't quote switches + "\"#{a}\"" # values quoted because they can contain characters like '>' + else + a + end + end + output = `#{command} #{args.join(' ')}` if $? != 0 raise MiniMagickError, "ImageMagick command (#{command} #{args.join(' ')}) failed: Error Given #{$?}" else