Skip to content

Instantly share code, notes, and snippets.

@bumi
Forked from reddavis/gist:332054
Created March 15, 2010 10:24
Show Gist options
  • Save bumi/332714 to your computer and use it in GitHub Desktop.
Save bumi/332714 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(/,/).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},"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment