graphicsmagick 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTM2MzNjZGMyMjVlYjhlMDhiZjgyYmJhMWRhNTcwOGZmZTE3YjZmOA==
5
+ data.tar.gz: !binary |-
6
+ NTY0ZWRiYzQ4YzdmOTA0OTJiODA5ZmM1NGM1ZjEzYjkwZWY2ZWYyNQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MjVjZGIyZjliZmE2YWQyNDEwMzViZWU2NWY1YzNlYzRhYTJmNWZkOGFiMjkx
10
+ MzE1NzcwZjM3ZDZhZmYwM2U2MGZlNzc3MTQ4NzIwYTNlODI4OTk2NDRmN2U5
11
+ NTkzZmI2ZDA3MjYxNTU0NmQ0NDZjODBmYTA0NmYxMWJmMThlMjY=
12
+ data.tar.gz: !binary |-
13
+ NWUwMTEyNGRlYmFhNzAwOTUxOTc0NjZmNmUzNjZiNzVkMTY2YjU1NGJlZjI4
14
+ NTc2ODI2NDI4NjY0MzQzNmUzODk4N2ZlZmJjMTJiNWFhZDhhZmY4ODc1OTZk
15
+ ZTU1ZGI5NmQyM2ViMjAwOWYxZWRjZDRkMWJkOTMzYTQ2NWRiNDQ=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # GraphicsMagick Changelog
2
+
3
+ ## 1.0.0
4
+ Initial release with support for mogrify, convert, and composite utilities, plus a few identify convenience methods.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in graphicsmagick.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chad McGimpsey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # Graphicsmagick
2
+
3
+ A very light Ruby wrapper for the Graphicsmagick CLI that supports mogrify, composite, and convert, plus a few convenience methods.
4
+
5
+ ## Why?
6
+
7
+ Graphicsmagick was 10x faster for our image processing needs than ImageMagick.
8
+ I wanted to use Graphicsmagick, but wanted a Ruby wrapper to help deal with exceptions and long running tasks. I needed to use multiple utilities within Graphicsmagick and the current crop of *_magick gems either did not support all of them, or had costly overhead (or both).
9
+
10
+ ## Installation
11
+
12
+ #### Install Graphicsmagick
13
+ http://www.graphicsmagick.org/README.html
14
+
15
+ On OS X, it's recommended to use `brew`
16
+ ```bash
17
+ brew update
18
+ brew install graphicsmagick
19
+ ```
20
+
21
+ #### Install gem
22
+ Add this line to your application's Gemfile:
23
+
24
+ gem 'graphicsmagick'
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install graphicsmagick
33
+
34
+ ## Usage
35
+
36
+ #### Mogrify commands and overwriting file
37
+ ```ruby
38
+ img = GraphicsMagick::Image.new('my_file.jpg')
39
+ img.crop('360x504+432+72').resize('125x177!')
40
+ img.write!
41
+ ```
42
+ Equivalent to
43
+ ```bash
44
+ gm mogrify -crop '360x504+432+72' -resize '125x177!' my_file.jpg
45
+ ```
46
+
47
+
48
+ #### Mogrify commands and write to new file
49
+ ```ruby
50
+ img = GraphicsMagick::Image.new('my_file.jpg')
51
+ img.crop('360x504+432+72').resize('125x177!')
52
+ img.write('my_new_file.jpg')
53
+ ```
54
+ Equivalent to
55
+ ```bash
56
+ cp my_file.jpg my_new_file.jpg & gm mogrify -crop '360x504+432+72' -resize '125x177!' my_new_file.jpg
57
+ ```
58
+
59
+
60
+ #### Convert a PDF file to a PNG while passing options at both steps
61
+ ```ruby
62
+ img = GraphicsMagick::Image.new("my_file.pdf")
63
+ img.strip.density(144).colorspace('RGB').auto-orient
64
+ img.convert.resize('50%')
65
+ img.write('my_new_file.png')
66
+ ```
67
+ Equivalent to
68
+ ```bash
69
+ gm convert -density 144 -strip -colorspace RGB -auto-orient "my_file.pdf" -resize '50%' my_new_file.png
70
+ ```
71
+
72
+
73
+ #### Composite an image over another image
74
+ ```ruby
75
+ background = GraphicsMagick::Image.new("background.png")
76
+ img = GraphicsMagick::Image.new("overlay.png")
77
+ img.composite(background).geometry('+100+150')
78
+ img.write('composite.png')
79
+ ```
80
+ Equivalent to
81
+ ```bash
82
+ gm composite -geometry +100+150 background.png overlay.png composite.png
83
+ ```
84
+
85
+
86
+ #### You can pass a file too
87
+ ```ruby
88
+ file = File.new('my_file.png')
89
+ img = GraphicsMagick::Image.new(file)
90
+
91
+ temp_img = GraphicsMagick::Image.new(Tempfile.new('foo'))
92
+ temp_img.path #=> "path/to/temp/file"
93
+ ```
94
+
95
+
96
+ ## Contributing
97
+
98
+ 1. Fork it
99
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
100
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
101
+ 4. Push to the branch (`git push origin my-new-feature`)
102
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphicsmagick/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "graphicsmagick"
8
+ gem.version = GraphicsMagick::VERSION
9
+ gem.authors = ["Chad McGimpsey"]
10
+ gem.email = ["chad.mcgimpsey@gmail.com"]
11
+ gem.description = "Light ruby wrapper for the GraphicsMagick CLI."
12
+ gem.summary = "Light ruby wrapper for the GraphicsMagick CLI."
13
+ gem.homepage = "https://github.com/dignoe/graphicsmagick"
14
+
15
+ gem.add_dependency('subexec')
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,8 @@
1
+ module GraphicsMagick
2
+
3
+ end
4
+
5
+ raise StandardError, "Please install GraphicsMagick" unless system("hash gm 2>&-")
6
+
7
+ require "graphicsmagick/version"
8
+ require "graphicsmagick/image"
Binary file
@@ -0,0 +1,88 @@
1
+ require 'tempfile'
2
+ require 'subexec'
3
+ require 'shellwords'
4
+ require 'graphicsmagick/utilities'
5
+ require 'active_support/core_ext/numeric/time'
6
+
7
+ module GraphicsMagick
8
+
9
+ UnknownOptionError = Class.new(StandardError)
10
+
11
+ class Image
12
+ attr_reader :file
13
+
14
+ include GraphicsMagick::Utilities
15
+
16
+ def initialize(input)
17
+ @command_options = []
18
+ @utility = nil
19
+
20
+ @file = parse_input(input)
21
+ end
22
+
23
+ # Returns the path of the associated file
24
+ def path
25
+ @file.path
26
+ end
27
+
28
+ # Writes the changes to the file specified in +output+.
29
+ # Set +:timeout => 30.seconds+ to change the timeout value. Default is one minute.
30
+ def write output, opts={}
31
+ output_path = parse_input(output, false)
32
+
33
+ FileUtils.copy_file(self.path, output_path) unless requires_output_file?
34
+
35
+ command = build_command(output_path)
36
+ run(command, opts)
37
+ GraphicsMagick::Image.new(output_path)
38
+ end
39
+
40
+
41
+ # Writes the changes to the current file.
42
+ # Set +:timeout => 30.seconds+ to change the timeout value. Default is one minute.
43
+ def write! opts={}
44
+ raise NoMethodError, "You cannot use Image#write(output) with the #{current_utility} command" if requires_output_file?
45
+
46
+ command = build_command(path)
47
+ run(command, opts)
48
+ self
49
+ end
50
+
51
+ protected
52
+
53
+ def method_missing(method, *args, &block)
54
+ add_option("-#{method.to_s.gsub(/_/,'-')}", *args)
55
+ end
56
+
57
+ private
58
+
59
+ def add_option option_name, *args
60
+ @command_options << {:name => option_name, :args => args.collect { |a| Shellwords.escape(a.to_s) }.join(" ")}
61
+ self
62
+ end
63
+
64
+ def run command, opts={}
65
+ opts.reverse_merge!(:timeout => 1.minute)
66
+ command = "gm #{command}"
67
+ cmd = Subexec.run(command, opts)
68
+
69
+ if cmd.exitstatus != 0
70
+ raise UnknownOptionError, "#{command} failed: #{cmd.output}"
71
+ end
72
+ ensure
73
+ @command_options = []
74
+ @utility = nil
75
+ end
76
+
77
+ def parse_input(input, output_as_file=true)
78
+ if input.is_a? String
79
+ return output_as_file ? File.new(input) : input
80
+ elsif input.is_a?(File) || input.is_a?(Tempfile)
81
+ return output_as_file ? input : input.path
82
+ else
83
+ raise TypeError, "You must specify a file as a String, File, or Tempfile object"
84
+ end
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,42 @@
1
+ require 'graphicsmagick/utilities/mogrify'
2
+ require 'graphicsmagick/utilities/identify'
3
+ require 'graphicsmagick/utilities/convert'
4
+ require 'graphicsmagick/utilities/composite'
5
+
6
+ module GraphicsMagick
7
+ module Utilities
8
+ include GraphicsMagick::Utilities::Identify
9
+ include GraphicsMagick::Utilities::Convert
10
+ include GraphicsMagick::Utilities::Composite
11
+ include GraphicsMagick::Utilities::Mogrify
12
+
13
+ DEFAULT_UTILITY = "mogrify"
14
+
15
+ def current_utility
16
+ @utility || DEFAULT_UTILITY
17
+ end
18
+
19
+ def to_cmd
20
+ "gm " + build_command('output_file')
21
+ end
22
+
23
+ private
24
+
25
+ def requires_output_file?
26
+ send(:"#{current_utility}_requires_output_file?")
27
+ end
28
+
29
+ def build_command(file)
30
+ send(:"build_#{current_utility}_command", file)
31
+ end
32
+
33
+ def options_to_str(opts)
34
+ if opts.nil?
35
+ ""
36
+ else
37
+ opts.collect {|opt| "#{opt[:name]} #{opt[:args]}"}.join(" ")
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,30 @@
1
+ module GraphicsMagick
2
+ module Utilities
3
+ module Composite
4
+
5
+ def composite(base, mask=nil)
6
+ if @utility.nil?
7
+ @utility = "composite"
8
+ @base_file = parse_input(base)
9
+ @mask_file = parse_input(mask) if mask
10
+ else
11
+ raise NoMethodError, "You can't use Image#composite with #{@utility}"
12
+ end
13
+ self
14
+ end
15
+
16
+
17
+ private
18
+
19
+ def composite_requires_output_file?
20
+ true
21
+ end
22
+
23
+ # gm convert [options] change-path base-path mask-path output-path
24
+ def build_composite_command(output_path)
25
+ "composite #{options_to_str(@command_options)} #{path} #{@base_file.path} #{@mask_file.path if @mask_file} #{output_path}"
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ module GraphicsMagick
2
+ module Utilities
3
+ module Convert
4
+ def convert
5
+ if @utility.nil?
6
+ @utility = "convert"
7
+ @command_options = [@command_options]
8
+ else
9
+ raise NoMethodError, "You can't use Image#convert with #{@utility}"
10
+ end
11
+ self
12
+ end
13
+
14
+ private
15
+
16
+ def convert_requires_output_file?
17
+ true
18
+ end
19
+
20
+ # gm convert [options] input-path [options] output-path
21
+ def build_convert_command(output_path)
22
+ first_options = @command_options[0]
23
+ second_options = @command_options[1..@command_options.length]
24
+ "convert #{options_to_str(first_options)} #{path} #{options_to_str(second_options)} #{output_path}"
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ module GraphicsMagick
2
+ module Utilities
3
+ module Identify
4
+
5
+ def width
6
+ get_identity.strip.split('x')[0].to_i
7
+ end
8
+
9
+ def height
10
+ get_identity.strip.split('x')[1].to_i
11
+ end
12
+
13
+ private
14
+
15
+ def composite_requires_output_file?
16
+ false
17
+ end
18
+
19
+ def get_identity
20
+ @identity ||= `gm identify -ping -format '%wx%h' #{path}`
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module GraphicsMagick
2
+ module Utilities
3
+ module Mogrify
4
+
5
+ private
6
+
7
+ def mogrify_requires_output_file?
8
+ false
9
+ end
10
+
11
+ # gm mogrify [options] file
12
+ def build_mogrify_command(file_path)
13
+ "mogrify #{options_to_str(@command_options)} #{file_path}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module GraphicsMagick
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphicsmagick
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Chad McGimpsey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: subexec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Light ruby wrapper for the GraphicsMagick CLI.
28
+ email:
29
+ - chad.mcgimpsey@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - graphicsmagick.gemspec
41
+ - lib/graphicsmagick.rb
42
+ - lib/graphicsmagick/.DS_Store
43
+ - lib/graphicsmagick/image.rb
44
+ - lib/graphicsmagick/utilities.rb
45
+ - lib/graphicsmagick/utilities/composite.rb
46
+ - lib/graphicsmagick/utilities/convert.rb
47
+ - lib/graphicsmagick/utilities/identify.rb
48
+ - lib/graphicsmagick/utilities/mogrify.rb
49
+ - lib/graphicsmagick/version.rb
50
+ homepage: https://github.com/dignoe/graphicsmagick
51
+ licenses: []
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.0.7
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Light ruby wrapper for the GraphicsMagick CLI.
73
+ test_files: []