ImageSorcery 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.markdown +28 -0
  2. data/lib/image_sorcery.rb +68 -0
  3. metadata +59 -0
@@ -0,0 +1,28 @@
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
+ At [Fol.io](http://fol.io), we need server-side image processing to work well and bend to our will. I wrote this because the ImageMagick libraries we tried suffered from at least one of two problems:
6
+
7
+ * Large memory consumption/leaking
8
+ * Didn't expose the entire ImageMagick library
9
+
10
+ Due to the way Image Sorcery was written, it manages to avoid both of these problems.
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
+ * More unit tests
28
+ * A few more convenience methods (like "dimensions").
@@ -0,0 +1,68 @@
1
+ require 'subexec'
2
+
3
+ class Sorcery
4
+ def initialize(file)
5
+ @file = file
6
+ end
7
+
8
+ # Runs ImageMagick's 'mogrify'.
9
+ # See http://www.imagemagick.org/script/mogrify.php
10
+ #
11
+ def manipulate!(args={})
12
+ tokens = ["mogrify"]
13
+ tokens << convert_to_arguments(args) if args
14
+ tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
15
+ tokens = convert_to_command(tokens)
16
+ success = run(tokens)[1]
17
+ success
18
+ end
19
+
20
+ # Runs ImageMagick's 'convert'.
21
+ # See http://www.imagemagick.org/script/convert.php
22
+ #
23
+ def convert(output, args={})
24
+ tokens = ["convert"]
25
+ tokens << convert_to_arguments(args) if args
26
+ tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
27
+ tokens << " #{output}"
28
+ tokens = convert_to_command(tokens)
29
+ success = run(tokens)[1]
30
+ success
31
+ end
32
+
33
+ # Runs ImageMagick's 'identify'.
34
+ # See http://www.imagemagick.org/script/identify.php
35
+ #
36
+ def identify(args={})
37
+ tokens = ["identify"]
38
+ tokens << convert_to_arguments(args) if args
39
+ tokens << " '#{@file}#{"[#{args[:layer].to_s}]" if args[:layer]}'"
40
+ tokens = convert_to_command(tokens)
41
+ output = run(tokens)[0]
42
+ output
43
+ end
44
+
45
+ # Return the x and y dimensions of an image as a hash.
46
+ #
47
+ def dimensions
48
+ dimensions = identify(layer: 0, format: "%wx%h").chomp.split("x")
49
+ { x: dimensions[0],
50
+ y: dimensions[1] }
51
+ end
52
+
53
+ private
54
+
55
+ def convert_to_command(tokens)
56
+ tokens.flatten.join("")
57
+ end
58
+
59
+ def convert_to_arguments(args)
60
+ args.reject {|k, v| k == :layer }.map {|k, v| " -#{k} '#{v}'"}
61
+ end
62
+
63
+ def run(cmds)
64
+ sub = Subexec.run(cmds.to_s)
65
+ success = sub.exitstatus == 0 ? true : false
66
+ [sub.output,success]
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ImageSorcery
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Rafaloff
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: subexec
16
+ requirement: &70240589735500 !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: *70240589735500
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: https://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
+ - ImageMagick
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.10
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A ruby ImageMagick library that doesn't suck
59
+ test_files: []