Skip to content

Commit

Permalink
Fist commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EricR committed Jun 9, 2012
0 parents commit a341593
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added image_sorcery-1.0.1.gem
Binary file not shown.
17 changes: 17 additions & 0 deletions image_sorcery.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
lib = File.expand_path('../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)

require 'bundler/version'

Gem::Specification.new do |s|
s.name = "image_sorcery"
s.version = "1.0.1"
s.platform = Gem::Platform::RUBY
s.authors = ["Eric Rafaloff"]
s.email = ["hello@ericrafaloff.com"]
s.homepage = "http://github.com/ericr/image_sorcery"
s.summary = "A ruby ImageMagick library that doesn't suck."
s.description = "A ruby ImageMagick library that doesn't suck."
s.files = Dir.glob("{lib}/**/*") + %w(README.markdown)
s.require_path = 'lib'
end
66 changes: 66 additions & 0 deletions lib/image_sorcery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class Sorcery
def initialize(file)
@file = file
end

# Runs ImageMagick's 'mogrify'.
# See http://www.imagemagick.org/script/mogrify.php
#
def manipulate!(args={})
tokens = ["mogrify"]
tokens << convert_to_arguments(args) if args
tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
tokens = convert_to_command(tokens)
success = run(tokens)[1]
success
end

# Runs ImageMagick's 'convert'.
# See http://www.imagemagick.org/script/convert.php
#
def convert(output, args={})
tokens = ["convert"]
tokens << convert_to_arguments(args) if args
tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
tokens << " #{output}"
tokens = convert_to_command(tokens)
success = run(tokens)[1]
success
end

# Runs ImageMagick's 'identify'.
# See http://www.imagemagick.org/script/identify.php
#
def identify(args={})
tokens = ["identify"]
tokens << convert_to_arguments(args) if args
tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
tokens = convert_to_command(tokens)
output = run(tokens)[0]
output
end

# Return the x and y dimensions of an image as a hash.
#
def dimensions
dimensions = identify(layer: 0, format: "%wx%h").chomp.split("x")
{ x: dimensions[0],
y: dimensions[1] }
end

private

def convert_to_command(tokens)
tokens.flatten.join("")
end

def convert_to_arguments(args)
args.reject {|k, v| k == :layer }.map {|k, v| " -#{k} '#{v}'"}
end

def run(cmds)
sub = Subexec.run(cmds.to_s)
success = sub.exitstatus == 0 ? true : false
[sub.output,success]
end
end
17 changes: 17 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Image Sorcery allows you to leverage all three of ImageMagick's command line tools, [mogrify](http://www.imagemagick.org/script/mogrify.php), [convert](http://www.imagemagick.org/script/convert.php), and [identify](http://www.imagemagick.org/script/identify.php), for maximum magickal power and minimum memory consumption!

## Installation

gem install image_sorcery

## Code Examples

image = Sorcery.new("image.png")
image.identify # => "image.png PNG 500x500 500x500+0+0 8-bit DirectClass 236KB 0.010u 0:00.010\n"
image.manipulate!(scale: "50%") # => true
image.dimensions #=> { x: 250, y: 250 }
image.convert("thumbnail.jpg", quality: 80, crop: "100x100>") # => true

## Todo

Unit tests coming soon! And a few new convenience methods (like "dimensions").

0 comments on commit a341593

Please sign in to comment.