lib/mini_magick.rb in mini_magick-1.0.1 vs lib/mini_magick.rb in mini_magick-1.1.0
- old
+ new
@@ -1,75 +1,85 @@
require "open-uri"
require "tempfile"
require "stringio"
module MiniMagick
+ class MiniMagickError < Exception; end
- VERSION = '1.0.1'
-
- class MiniMagickError < Exception
- end
+ VERSION = '1.1.0'
class Image
attr :path
- # Attributes
- # ----------
- def width
- info[:width]
- end
-
- def height
- info[:height]
- end
-
- def format
- info[:format]
- end
-
# Class Methods
# -------------
class <<self
def from_blob(blob)
begin
tmp = Tempfile.new("minimagic")
+ tmp.binmode
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)
- File.open(image_path, "r") do |f|
+ File.open(image_path, "rb") do |f|
self.from_blob(f.read)
end
end
end
# Instance Methods
# ----------------
- def initialize(input_path)
-
+ def initialize(input_path)
@path = input_path
# Ensure that the file is an image
run_command("identify #{@path}")
end
+
+ 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]
+ when "height"
+ run_command("identify", "-format", "%h\\\\n", @path).split("\n")[0].to_i
+ when "width"
+ run_command("identify", "-format", "%w\\\\n", @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
+ else
+ run_command('identify', '-format', "\"#{value}\"", @path)
+ end
+ end
+
+ # This is a 'special' command because it needs to change @path to reflect the new extension
+ 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
- def write(output_path)
- open(output_path, "w") do |output_file|
- open(@path) do |image_file|
+ 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
+ end
end
- # Any message that sent that is unknown is sent through morgrify
- #
+ # 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)
args.push(@path) # push the path onto the end
run_command("mogrify", "-#{symbol}", *args)
end
@@ -82,22 +92,14 @@
end
# Private (Don't look in here!)
# -----------------------------
private
-
- def info
- info_array = run_command("identify", @path).split
- info = {
- :format => info_array[1],
- :width => info_array[2].match(/^\d+/)[0].to_i,
- :height => info_array[2].match(/\d+$/)[0].to_i
- }
- end
-
+
def run_command(command, *args)
args = args.collect {|a| a.to_s}
output = `#{command} #{args.join(' ')}`
+
if $? != 0
raise MiniMagickError, "ImageMagick command (#{command} #{args.join(' ')}) failed: Error Given #{$?}"
else
return output
end