image_sorcery 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.markdown +27 -0
  2. data/lib/image_sorcery.rb +66 -0
  3. metadata +58 -0
data/README.markdown ADDED
@@ -0,0 +1,27 @@
1
+ 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!
2
+
3
+ ## Why?
4
+
5
+ Several other ImageMagick libraries exist for Ruby, but most have at least one of two issues:
6
+
7
+ * Large memory consumption/leaking
8
+ * Doesn't expose the entire ImageMagick library
9
+
10
+ Due to the way Image Sorcery was written, it manages to avoid both of these issues.
11
+
12
+ ## Installation
13
+
14
+ gem install image_sorcery
15
+
16
+ ## Code Examples
17
+ ```ruby
18
+ image = Sorcery.new("image.png")
19
+ image.identify # => "image.png PNG 500x500 500x500+0+0 8-bit DirectClass 236KB 0.010u 0:00.010\n"
20
+ image.manipulate!(scale: "50%") # => true
21
+ image.dimensions # => { x: 250, y: 250 }
22
+ image.convert("thumbnail.jpg", quality: 80, crop: "100x100>") # => true
23
+ ```
24
+
25
+ ## Todo
26
+
27
+ Unit tests coming soon! And a few new convenience methods (like "dimensions").
@@ -0,0 +1,66 @@
1
+ class Sorcery
2
+ def initialize(file)
3
+ @file = file
4
+ end
5
+
6
+ # Runs ImageMagick's 'mogrify'.
7
+ # See http://www.imagemagick.org/script/mogrify.php
8
+ #
9
+ def manipulate!(args={})
10
+ tokens = ["mogrify"]
11
+ tokens << convert_to_arguments(args) if args
12
+ tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
13
+ tokens = convert_to_command(tokens)
14
+ success = run(tokens)[1]
15
+ success
16
+ end
17
+
18
+ # Runs ImageMagick's 'convert'.
19
+ # See http://www.imagemagick.org/script/convert.php
20
+ #
21
+ def convert(output, args={})
22
+ tokens = ["convert"]
23
+ tokens << convert_to_arguments(args) if args
24
+ tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
25
+ tokens << " #{output}"
26
+ tokens = convert_to_command(tokens)
27
+ success = run(tokens)[1]
28
+ success
29
+ end
30
+
31
+ # Runs ImageMagick's 'identify'.
32
+ # See http://www.imagemagick.org/script/identify.php
33
+ #
34
+ def identify(args={})
35
+ tokens = ["identify"]
36
+ tokens << convert_to_arguments(args) if args
37
+ tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
38
+ tokens = convert_to_command(tokens)
39
+ output = run(tokens)[0]
40
+ output
41
+ end
42
+
43
+ # Return the x and y dimensions of an image as a hash.
44
+ #
45
+ def dimensions
46
+ dimensions = identify(layer: 0, format: "%wx%h").chomp.split("x")
47
+ { x: dimensions[0],
48
+ y: dimensions[1] }
49
+ end
50
+
51
+ private
52
+
53
+ def convert_to_command(tokens)
54
+ tokens.flatten.join("")
55
+ end
56
+
57
+ def convert_to_arguments(args)
58
+ args.reject {|k, v| k == :layer }.map {|k, v| " -#{k} '#{v}'"}
59
+ end
60
+
61
+ def run(cmds)
62
+ sub = Subexec.run(cmds.to_s)
63
+ success = sub.exitstatus == 0 ? true : false
64
+ [sub.output,success]
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_sorcery
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Rafaloff
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: subexec
16
+ requirement: &70141614869640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70141614869640
25
+ description: A ruby ImageMagick library that doesn't suck.
26
+ email:
27
+ - hello@ericrafaloff.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/image_sorcery.rb
33
+ - README.markdown
34
+ homepage: http://github.com/ericr/image_sorcery
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.10
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A ruby ImageMagick library that doesn't suck.
58
+ test_files: []