Class: MiniMagick::Image
- Inherits:
-
Object
- Object
- MiniMagick::Image
- Defined in:
- lib/mini_magick.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#tempfile ⇒ Object
readonly
Returns the value of attribute tempfile.
Class Method Summary collapse
- .from_blob(blob, extension = nil) ⇒ Object
-
.from_file(image_path) ⇒ Object
Use this if you don’t want to overwrite the image file.
Instance Method Summary collapse
-
#[](value) ⇒ Object
For reference see www.imagemagick.org/script/command-line-options.php#format.
-
#cksum(input_path) ⇒ Object
Instance Methods —————-.
-
#combine_options(&block) ⇒ Object
You can use multiple commands together using this method.
-
#format(format) ⇒ Object
This is a ‘special’ command because it needs to change @path to reflect the new extension.
-
#format_option(format) ⇒ Object
Outputs a carriage-return delimited format string for Unix and Windows.
- #get_file_info(input_path) ⇒ Object
-
#initialize(input_path, tempfile = nil) ⇒ Image
constructor
A new instance of Image.
-
#method_missing(symbol, *args) ⇒ Object
If an unknown method is called then it is sent through the morgrify program Look here to find all the commands (www.imagemagick.org/script/mogrify.php).
- #run_command(command, *args) ⇒ Object
-
#to_blob ⇒ Object
Give you raw data back.
-
#windows? ⇒ Boolean
Check to see if we are running on win32 – we need to escape things differently.
-
#write(output_path) ⇒ Object
Writes the temporary image that we are using for processing to the output path.
Constructor Details
#initialize(input_path, tempfile = nil) ⇒ Image
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mini_magick.rb', line 64 def initialize(input_path, tempfile=nil) @path = input_path @tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected. # Ensure that the file is an image # might as well collect some information here. # get_file_info(input_path) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
If an unknown method is called then it is sent through the morgrify program Look here to find all the commands (www.imagemagick.org/script/mogrify.php)
130 131 132 133 134 |
# File 'lib/mini_magick.rb', line 130 def method_missing(symbol, *args) args.push(@path) # push the path onto the end run_command("mogrify", "-#{symbol}", *args) self end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
15 16 17 |
# File 'lib/mini_magick.rb', line 15 def output @output end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/mini_magick.rb', line 13 def path @path end |
#tempfile ⇒ Object (readonly)
Returns the value of attribute tempfile.
14 15 16 |
# File 'lib/mini_magick.rb', line 14 def tempfile @tempfile end |
Class Method Details
.from_blob(blob, extension = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mini_magick.rb', line 20 def from_blob(blob, extension=nil) begin tempfile = ImageTempFile.new("minimagick#{extension}") tempfile.binmode tempfile.write(blob) ensure tempfile.close end return self.new(tempfile.path, tempfile) end |
.from_file(image_path) ⇒ Object
Use this if you don’t want to overwrite the image file
33 34 35 36 37 |
# File 'lib/mini_magick.rb', line 33 def from_file(image_path) File.open(image_path, "rb") do |f| self.from_blob(f.read, File.extname(image_path)) end end |
Instance Method Details
#[](value) ⇒ Object
For reference see www.imagemagick.org/script/command-line-options.php#format
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mini_magick.rb', line 76 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 "format" if !(@image_info && @image_hash == cksum(@path)) get_file_info(@path) end @image_info[:format] when "height" if !(@image_info && @image_hash == cksum(@path)) get_file_info(@path) end @image_info[:height] when "width" if !(@image_info && @image_hash == cksum(@path)) get_file_info(@path) end @image_info[:width] 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 else run_command('identify', '-format', "\"#{value}\"", @path).split("\n")[0] end end |
#cksum(input_path) ⇒ Object
Instance Methods
42 43 44 |
# File 'lib/mini_magick.rb', line 42 def cksum(input_path) run_command("cksum", @path.gsub(/\[\d+\]$/,"")).split(/\s/)[0] end |
#combine_options(&block) ⇒ Object
You can use multiple commands together using this method
137 138 139 140 141 |
# File 'lib/mini_magick.rb', line 137 def (&block) c = CommandBuilder.new block.call c run_command("mogrify", *c.args << @path) end |
#format(format) ⇒ Object
This is a ‘special’ command because it needs to change @path to reflect the new extension
108 109 110 111 112 113 |
# File 'lib/mini_magick.rb', line 108 def format(format) run_command("mogrify", "-format", format, @path) @path = @path.sub(/(\.\w+)?$/, ".#{format}") raise "Unable to format to #{format}" unless File.exists?(@path) end |
#format_option(format) ⇒ Object
Outputs a carriage-return delimited format string for Unix and Windows
149 150 151 |
# File 'lib/mini_magick.rb', line 149 def format_option(format) windows? ? "#{format}\\n" : "#{format}\\\\n" end |
#get_file_info(input_path) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mini_magick.rb', line 46 def get_file_info(input_path) @image_hash = cksum(@path) @image_info_string = run_command("identify", "-format", "|s:%s|m:%m|h:%h|w:%w|%[EXIF:*]\n", @path) # save the hash of the file, so we can validate it again. mm=@image_info_string.match(/^\|s\:(\d+)\|m\:([^\|]*)\|h\:(\d+)\|w\:(\d+)\|(EXIF)?(.*)/) if mm @image_info = {} @image_info[:format] = mm[2]; @image_info[:height] = mm[3]; @image_info[:width] = mm[4]; @image_info[:exif] = mm[5]; end end |
#run_command(command, *args) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/mini_magick.rb', line 153 def run_command(command, *args) args.collect! do |arg| arg = arg.to_s arg = %|"#{arg}"| unless arg[0] == ?- # values quoted because they can contain characters like '>', but don't quote switches arg end @output = `#{command} #{args.join(' ')}` if $? != 0 raise MiniMagickError, "ImageMagick command (#{command} #{args.join(' ')}) failed: Error Given #{$?}" else @output end end |
#to_blob ⇒ Object
Give you raw data back
124 125 126 |
# File 'lib/mini_magick.rb', line 124 def to_blob File.read @path end |
#windows? ⇒ Boolean
Check to see if we are running on win32 – we need to escape things differently
144 145 146 |
# File 'lib/mini_magick.rb', line 144 def windows? !(RUBY_PLATFORM =~ /win32/).nil? end |
#write(output_path) ⇒ Object
Writes the temporary image that we are using for processing to the output path
116 117 118 119 120 121 |
# File 'lib/mini_magick.rb', line 116 def write(output_path) FileUtils.copy_file @path, output_path unless @image_info && @image_hash == cksum(@path) # skip if we have the same file and we've read it run_command "identify", output_path # Verify that we have a good image end end |