Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

completed version 0.0.1

  • Loading branch information...
commit 16e84837a49390294db26749fd5e99e72d220f92 0 parents
Eric Berry authored
18 .gitignore
@@ -0,0 +1,18 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
+.rvmrc
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in rmagick-metadata.gemspec
+gemspec
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2012 TODO: Write your name
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89 README.md
@@ -0,0 +1,89 @@
+# Rmagick::Metadata
+
+Parses an image using RMagick and parses the metadata.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'rmagick-metadata'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install rmagick-metadata
+
+## Usage
+
+```ruby
+image_path = "/my_rails_app/app/assets/images/rails.png"
+
+data = RMagickMetadata.new(image_path)
+
+# Helpers
+assert data.filename_suffix == 'png'
+assert data.has_transparency? == true
+assert data.dimensions == [ 50, 64 ]
+assert data.width == 50
+assert data.height == 64
+assert data.file_size == 6646
+assert data.resolution == 72
+assert data.compression_percent == 0.0
+
+# Auto-populated
+assert data.file_size_of_image == '6646B'
+assert data.comment == ''
+assert data.directory =~ /\.*\/rmagick-metadata\/test/
+assert data.filename_suffix == 'png'
+assert data.filename_with_suffix == 'rails.png'
+assert data.layer_canvas_page_geometry == '50x64+0+0'
+assert data.current_height_in_pixels == '64'
+assert data.image_filename =~ /\.*\/rmagick-metadata\/test\/rails.png/
+assert data.calculated_number_of_unique_colors == '2018'
+assert data.label == ''
+assert data.image_file_format == 'PNG'
+assert data.number_of_images_in_current_image_sequence == '1'
+assert data.output_filename == ''
+assert data.index_of_image_in_current_image_list == '0'
+assert data.quantum_depth == '16'
+assert data.image_class_and_colorspace == 'DirectClass sRGB Matte'
+assert data.scene_number == '0'
+assert data.filename_without_directory_or_extension == 'rails'
+assert data.unique_temporary_filename == ''
+assert data.current_width_in_pixels == '50'
+assert data.x_resolution_density == '72 Undefined'
+assert data.y_resolution_density == '72 Undefined'
+assert data.image_depth == '8'
+assert data.image_transparency_channel_enabled == 'True'
+assert data.image_compression_type == 'Zip'
+assert data.image_gif_dispose_method == 'Undefined'
+assert data.image_size_w_x_h == '50x64'
+assert data.page_canvas_height == '64'
+assert data.magick_filename =~ /\.*\/rmagick-metadata\/test\/rails.png/
+assert data.page_canvas_offset == '+0+0'
+assert data.page_canvas_size == '50x64'
+assert data.image_compression_quality == '0'
+assert data.scenes == '2147483647'
+assert data.image_time_delay == '0'
+assert data.page_canvas_width == '50'
+assert data.page_canvas_x_offset == '0'
+assert data.page_canvas_y_offset == '0'
+assert data.unique_filename == ''
+assert data.calculated_trim_bounding_box == '50x64+0+0'
+assert data.calculated_signature_hash_of_image_values == 'ae231a37d573c2ef941f3345932183b58cf5c0e6a569839d931b6172cebdbe27'
+```
+
+## Testing
+
+The gem is fully tested using [MicroTest](http://hopsoft.github.com/micro_test/). Use the command `mt` to perform the tests.
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
1  Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
115 lib/rmagick-metadata.rb
@@ -0,0 +1,115 @@
+require "RMagick"
+
+class RMagickMetadata
+
+ def initialize( file_path )
+
+ # Create attr_accessors for all keys in the data_map
+ data_map.each do |k, v|
+ create_attr v
+ end
+
+ # Get the data for the image using 'identify'
+ format_str = data_map.keys.map{|k|"%#{k}"}.join('|')
+ data = %x[identify -format "#{format_str}" #{file_path}].chomp
+
+ # Parse the data response and assign it to the instance values
+ data.split('|').each_with_index do |val, idx|
+ attr_name = data_map.values[idx]
+ instance_variable_set("@#{attr_name}", val)
+ end
+
+ end
+
+ # Helper methods
+
+ def has_transparency?
+ @image_transparency_channel_enabled == 'True'
+ end
+
+ def dimensions
+ [@current_width_in_pixels.to_i, @current_height_in_pixels.to_i]
+ end
+
+ def file_size
+ @file_size_of_image.scan(/\d/).join().to_i if @file_size_of_image
+ end
+
+ def resolution
+ @x_resolution_density.scan(/\d/).join().to_i if @x_resolution_density
+ end
+
+ def compression_percent
+ @image_compression_quality.to_f if @image_compression_quality
+ end
+
+ def width
+ dimensions[0]
+ end
+
+ def height
+ dimensions[1]
+ end
+
+protected
+
+ def data_map
+ {
+ 'b' => 'file_size_of_image',
+ 'c' => 'comment',
+ 'd' => 'directory',
+ 'e' => 'filename_suffix',
+ 'f' => 'filename_with_suffix',
+ 'g' => 'layer_canvas_page_geometry',
+ 'h' => 'current_height_in_pixels',
+ 'i' => 'image_filename',
+ 'k' => 'calculated_number_of_unique_colors',
+ 'l' => 'label',
+ 'm' => 'image_file_format',
+ 'n' => 'number_of_images_in_current_image_sequence',
+ 'o' => 'output_filename',
+ 'p' => 'index_of_image_in_current_image_list',
+ 'q' => 'quantum_depth',
+ 'r' => 'image_class_and_colorspace',
+ 's' => 'scene_number',
+ 't' => 'filename_without_directory_or_extension',
+ 'u' => 'unique_temporary_filename',
+ 'w' => 'current_width_in_pixels',
+ 'x' => 'x_resolution_density',
+ 'y' => 'y_resolution_density',
+ 'z' => 'image_depth',
+ 'A' => 'image_transparency_channel_enabled',
+ 'C' => 'image_compression_type',
+ 'D' => 'image_gif_dispose_method',
+ 'G' => 'image_size_w_x_h',
+ 'H' => 'page_canvas_height',
+ 'M' => 'magick_filename',
+ 'O' => 'page_canvas_offset',
+ 'P' => 'page_canvas_size',
+ 'Q' => 'image_compression_quality',
+ 'S' => 'scenes',
+ 'T' => 'image_time_delay',
+ 'W' => 'page_canvas_width',
+ 'X' => 'page_canvas_x_offset',
+ 'Y' => 'page_canvas_y_offset',
+ 'Z' => 'unique_filename',
+ '@' => 'calculated_trim_bounding_box',
+ '#' => 'calculated_signature_hash_of_image_values'
+ }
+ end
+
+ def create_method( name, &block )
+ self.class.send( :define_method, name, &block )
+ end
+
+ def create_attr( name )
+ create_method( "#{name}=".to_sym ) { |val|
+ instance_variable_set( "@" + name, val)
+ }
+
+ create_method( name.to_sym ) {
+ instance_variable_get( "@" + name )
+ }
+ end
+
+end
21 rmagick-metadata.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+
+Gem::Specification.new do |gem|
+ gem.name = "rmagick-metadata"
+ gem.version = "0.0.1"
+ gem.authors = ["Eric Berry"]
+ gem.email = ["cavneb@gmail.com"]
+ gem.description = %q{Parses an image using RMagick and parses the metadata}
+ gem.summary = %q{Parses an image using RMagick and parses the metadata}
+ gem.homepage = "http://github.com/cavneb/rmagick-metadata"
+
+ gem.add_dependency "rmagick"
+ gem.add_development_dependency "micro_test"
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+end
BIN  test/rails.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 test/rmagick_metadata_test.rb
@@ -0,0 +1,64 @@
+require File.expand_path("../test_helper", __FILE__)
+
+class RMagickMetadataTest < MicroTest::Test
+
+ test "initialize and populate data" do
+ image_path = File.expand_path("../rails.png", __FILE__)
+ assert File.exist? image_path
+
+ data = ::RMagickMetadata.new(image_path)
+
+ # Helpers
+ assert data.filename_suffix == 'png'
+ assert data.has_transparency? == true
+ assert data.dimensions == [ 50, 64 ]
+ assert data.width == 50
+ assert data.height == 64
+ assert data.file_size == 6646
+ assert data.resolution == 72
+ assert data.compression_percent == 0.0
+
+ # Auto-populated
+ assert data.file_size_of_image == '6646B'
+ assert data.comment == ''
+ assert data.directory =~ /\.*\/rmagick-metadata\/test/
+ assert data.filename_suffix == 'png'
+ assert data.filename_with_suffix == 'rails.png'
+ assert data.layer_canvas_page_geometry == '50x64+0+0'
+ assert data.current_height_in_pixels == '64'
+ assert data.image_filename =~ /\.*\/rmagick-metadata\/test\/rails.png/
+ assert data.calculated_number_of_unique_colors == '2018'
+ assert data.label == ''
+ assert data.image_file_format == 'PNG'
+ assert data.number_of_images_in_current_image_sequence == '1'
+ assert data.output_filename == ''
+ assert data.index_of_image_in_current_image_list == '0'
+ assert data.quantum_depth == '16'
+ assert data.image_class_and_colorspace == 'DirectClass sRGB Matte'
+ assert data.scene_number == '0'
+ assert data.filename_without_directory_or_extension == 'rails'
+ assert data.unique_temporary_filename == ''
+ assert data.current_width_in_pixels == '50'
+ assert data.x_resolution_density == '72 Undefined'
+ assert data.y_resolution_density == '72 Undefined'
+ assert data.image_depth == '8'
+ assert data.image_transparency_channel_enabled == 'True'
+ assert data.image_compression_type == 'Zip'
+ assert data.image_gif_dispose_method == 'Undefined'
+ assert data.image_size_w_x_h == '50x64'
+ assert data.page_canvas_height == '64'
+ assert data.magick_filename =~ /\.*\/rmagick-metadata\/test\/rails.png/
+ assert data.page_canvas_offset == '+0+0'
+ assert data.page_canvas_size == '50x64'
+ assert data.image_compression_quality == '0'
+ assert data.scenes == '2147483647'
+ assert data.image_time_delay == '0'
+ assert data.page_canvas_width == '50'
+ assert data.page_canvas_x_offset == '0'
+ assert data.page_canvas_y_offset == '0'
+ assert data.unique_filename == ''
+ assert data.calculated_trim_bounding_box == '50x64+0+0'
+ assert data.calculated_signature_hash_of_image_values == 'ae231a37d573c2ef941f3345932183b58cf5c0e6a569839d931b6172cebdbe27'
+ end
+
+end
4 test/test_helper.rb
@@ -0,0 +1,4 @@
+require 'bundler'
+Bundler.require
+
+Dir[File.dirname(__FILE__) + '/../lib/**/*.rb'].each {|file| require file }
Please sign in to comment.
Something went wrong with that request. Please try again.