lib/mini_magick.rb in mini_magick-3.5.0 vs lib/mini_magick.rb in mini_magick-3.6.0

- old
+ new

@@ -5,10 +5,11 @@ require 'shellwords' module MiniMagick class << self attr_accessor :processor + attr_accessor :processor_path attr_accessor :timeout # Experimental method for automatically selecting a processor # such as gm. Only works on *nix. @@ -104,12 +105,12 @@ def import_pixels(blob, columns, rows, depth, map, format="png") # Create an image object with the raw pixel data string: image = create(".dat", validate = false) { |f| f.write(blob) } # Use ImageMagick to convert the raw data file to an image file of the desired format: converted_image_path = image.path[0..-4] + format - argument = "-size #{columns}x#{rows} -depth #{depth} #{map}:#{image.path} #{converted_image_path}" - cmd = CommandBuilder.new("convert", argument) #Example: convert -size 256x256 -depth 16 gray:blob.dat blob.png + arguments = ["-size", "#{columns}x#{rows}", "-depth", "#{depth}", "#{map}:#{image.path}", "#{converted_image_path}"] + cmd = CommandBuilder.new("convert", *arguments) #Example: convert -size 256x256 -depth 16 gray:blob.dat blob.png image.run(cmd) # Update the image instance with the path of the properly formatted image, and return: image.path = converted_image_path image end @@ -184,23 +185,19 @@ def initialize(input_path, tempfile = nil) @path = input_path @tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected. end - def escaped_path - Pathname.new(@path).to_s.inspect - end - # Checks to make sure that MiniMagick can read the file and understand it. # # This uses the 'identify' command line utility to check the file. If you are having # issues with this, then please work directly with the 'identify' command and see if you # can figure out what the issue is. # # @return [Boolean] def valid? - run_command("identify", @path) + run_command("identify", path) true rescue MiniMagick::Invalid false end @@ -222,43 +219,43 @@ # @return [String, Numeric, Array, Time, Object] Depends on the method called! Defaults to String for unknown commands def [](value) # Why do I go to the trouble of putting in newlines? Because otherwise animated gifs screw everything up case value.to_s when "colorspace" - run_command("identify", "-format", format_option("%r"), escaped_path).split("\n")[0].strip + run_command("identify", "-format", '%r\n', path).split("\n")[0].strip when "format" - run_command("identify", "-format", format_option("%m"), escaped_path).split("\n")[0] + run_command("identify", "-format", '%m\n', path).split("\n")[0] when "height" - run_command("identify", "-format", format_option("%h"), escaped_path).split("\n")[0].to_i + run_command("identify", "-format", '%h\n', path).split("\n")[0].to_i when "width" - run_command("identify", "-format", format_option("%w"), escaped_path).split("\n")[0].to_i + run_command("identify", "-format", '%w\n', path).split("\n")[0].to_i when "dimensions" - run_command("identify", "-format", format_option("%w %h"), escaped_path).split("\n")[0].split.map{|v|v.to_i} + run_command("identify", "-format", '%w %h\n', path).split("\n")[0].split.map{|v|v.to_i} when "size" - File.size(@path) # Do this because calling identify -format "%b" on an animated gif fails! + File.size(path) # Do this because calling identify -format "%b" on an animated gif fails! when "original_at" # Get the EXIF original capture as a Time object Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil when /^EXIF\:/i - result = run_command('identify', '-format', "\"%[#{value}]\"", escaped_path).chop + result = run_command('identify', '-format', "%[#{value}]", path).chop if result.include?(",") read_character_data(result) else result end else - run_command('identify', '-format', "\"#{value}\"", escaped_path).split("\n")[0] + run_command('identify', '-format', value, path).split("\n")[0] end end # Sends raw commands to imagemagick's `mogrify` command. The image path is automatically appended to the command. # # Remember, we are always acting on this instance of the Image when messing with this. # # @return [String] Whatever the result from the command line is. May not be terribly useful. def <<(*args) - run_command("mogrify", *args << escaped_path) + run_command("mogrify", *args << path) end # This is used to change the format of the image. That is, from "tiff to jpg" or something like that. # Once you run it, the instance is pointing to a new file with a new extension! # @@ -280,21 +277,21 @@ # @return [nil] def format(format, page = 0) c = CommandBuilder.new('mogrify', '-format', format) yield c if block_given? if page - c << @path + "[#{page}]" + c << "#{path}[#{page}]" else - c << @path + c << path end run(c) - old_path = @path.dup - @path.sub!(/(\.\w*)?$/, ".#{format}") - File.delete(old_path) if old_path != @path + old_path = path + self.path = path.sub(/(\.\w*)?$/, ".#{format}") + File.delete(old_path) if old_path != path - unless File.exists?(@path) + unless File.exists?(path) raise MiniMagick::Error, "Unable to format to #{format}" end end # Collapse images with sequences to the first frame (ie. animated gifs) and @@ -309,14 +306,14 @@ # @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_to) if output_to.kind_of?(String) || !output_to.respond_to?(:write) - FileUtils.copy_file @path, output_to - run_command "identify", output_to.to_s.inspect # Verify that we have a good image + FileUtils.copy_file path, output_to + run_command "identify", output_to.to_s # Verify that we have a good image else # stream - File.open(@path, "rb") do |f| + File.open(path, "rb") do |f| f.binmode while chunk = f.read(8192) output_to.write(chunk) end end @@ -325,11 +322,11 @@ end # Gives you raw image data back # @return [String] binary string def to_blob - f = File.new @path + f = File.new path f.binmode f.read ensure f.close if f end @@ -358,21 +355,16 @@ # # @yieldparam command [CommandBuilder] def combine_options(tool = "mogrify", &block) c = CommandBuilder.new(tool) - c << @path if tool.to_s == "convert" + c << path if tool.to_s == "convert" block.call(c) - c << @path + c << path run(c) end - # Check to see if we are running on win32 -- we need to escape things differently - def windows? - RUBY_PLATFORM =~ /mswin|mingw|cygwin/ - end - def composite(other_image, output_extension = 'jpg', &block) begin second_tempfile = Tempfile.new(output_extension) second_tempfile.binmode ensure @@ -387,15 +379,10 @@ run(command) return Image.new(second_tempfile.path, second_tempfile) end - # Outputs a carriage-return delimited format string for Unix and Windows - def format_option(format) - windows? ? "\"#{format}\\n\"" : "\"#{format}\\\\n\"" - end - def run_command(command, *args) # -ping "efficiently determine image characteristics." if command == 'identify' args.unshift '-ping' args.unshift '-quiet' unless MiniMagick.processor.to_s == 'gm' @@ -443,22 +430,28 @@ result end end class CommandBuilder - attr_reader :args - def initialize(tool, *options) @tool = tool @args = [] options.each { |arg| push(arg) } end def command - "#{MiniMagick.processor} #{@tool} #{@args.join(' ')}".strip + com = "#{@tool} #{args.join(' ')}".strip + com = "#{MiniMagick.processor} #{com}" unless MiniMagick.processor.nil? + + com = File.join MiniMagick.processor_path, com unless MiniMagick.processor_path.nil? + com.strip end + def args + @args.map(&:shellescape) + end + # Add each mogrify command in both underscore and dash format MOGRIFY_COMMANDS.each do |mogrify_command| # Example of what is generated here: # @@ -491,25 +484,21 @@ def +(*options) push(@args.pop.gsub(/^-/, '+')) if options.any? options.each do |o| - push escape_string(o) + push o end end end def add_command(command, *options) push "-#{command}" if options.any? options.each do |o| - push escape_string(o) + push o end end - end - - def escape_string(value) - Shellwords.escape(value.to_s) end def add_creation_operator(command, *options) creation_command = command if options.any?