Skip to content

Instantly share code, notes, and snippets.

@reddavis
Forked from bumi/magick_identify.rb
Created March 15, 2010 11:37
Show Gist options
  • Save reddavis/332761 to your computer and use it in GitHub Desktop.
Save reddavis/332761 to your computer and use it in GitHub Desktop.
class MagickIdentify
attr_reader :data
def initialize(image_path)
@image_path = image_path
@data = {}
execute_and_parse
end
def method_missing(method, *args)
@data.key?(method) ? @data[method] : super(method, *args)
end
private
def execute_and_parse
@output = %x{identify -format "#{format}" #{@image_path}}.chomp
parse
end
def parse
@output.split(/\n/).each do |feature_line|
feature_name, feature_output = feature_line.split(/-/)
@data[feature_name.to_sym] = feature_output
end
end
def format
format_string = ""
features.each do |feature, syntax|
format_string << "#{feature}-#{syntax}\n"
end
format_string
end
def features
{
:file_size => '%b',
:comment => '%c',
:directory => '%d',
:filename_extension => '%e',
:filename => '%f',
:page_geometry => '%g',
:height => '%[height]',
:input_filename => '%i',
:number_of_unique_colors => '%k',
:label => '%l',
:output_filename => '%o',
:width => '%[width]',
:x_resolution => '%x',
:y_resolution => '%y',
:page_height => '%H',
:page_width => '%W',
:base => '%[base]',
:directory => '%[directory]',
:extension => '%[extension]',
:size => '%[size]'
}
end
end
require 'magick_identify'
require 'spec'
describe "MagickIdentify" do
before do
@a = MagickIdentify.new(image_path)
end
it "should return 'example'" do
@a.base.should == "example"
end
private
def image_path
File.expand_path(File.dirname(__FILE__) + '/files/example.png')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment