forked from EricR/image_sorcery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a341593
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"). |