mojo_magick 0.5.6 → 0.5.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/Gemfile.lock +19 -10
- data/README.md +1 -0
- data/Rakefile +6 -13
- data/examples/animated_gif.rb +1 -2
- data/examples/composite.rb +1 -2
- data/init.rb +1 -3
- data/lib/image_magick/fonts.rb +4 -4
- data/lib/image_magick/resource_limits.rb +43 -48
- data/lib/initializers/hash.rb +5 -1
- data/lib/mojo_magick.rb +59 -67
- data/lib/mojo_magick/command_status.rb +2 -1
- data/lib/mojo_magick/errors.rb +1 -1
- data/lib/mojo_magick/font.rb +4 -6
- data/lib/mojo_magick/opt_builder.rb +12 -12
- data/lib/mojo_magick/util/parser.rb +24 -28
- data/lib/mojo_magick/version.rb +1 -1
- data/mojo_magick.gemspec +19 -18
- data/test/font_test.rb +19 -25
- data/test/fonts_test.rb +4 -5
- data/test/mojo_magick_test.rb +277 -283
- data/test/opt_builder_test.rb +11 -14
- data/test/parser_test.rb +15 -16
- data/test/resource_limits_test.rb +14 -16
- data/test/test_helper.rb +3 -3
- metadata +21 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 30925f12cdcc0bbe8f6626d1f68266f00be28abe536a4c925522d8cbb99750dc
|
4
|
+
data.tar.gz: 1bf5dbdb09fa28ec7e5c0167d32c153f740d00c7541685840ce720d83f53c0fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29735844a173bc70ded217d142c8c11af8b78880f1396d4678837182d27c57f18e79b17c673d0174ab328e08f47a33aecb522e704701539c44f8723493bc8bab
|
7
|
+
data.tar.gz: 93907724a1c2822532e6009aef4a65b59db0e0197abbe214196b202dfbad337ab95ced02d38576ab68c420692403da1963e8d940637c99b627053afb9be7eb99
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.6.6
|
data/Gemfile.lock
CHANGED
@@ -1,26 +1,35 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mojo_magick (0.5.
|
4
|
+
mojo_magick (0.5.7)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
diff-lcs (1.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
diff-lcs (1.3)
|
10
|
+
docile (1.3.2)
|
11
|
+
json (2.2.0)
|
12
|
+
minitest (5.14.2)
|
13
|
+
rake (12.3.3)
|
14
|
+
rspec-expectations (3.9.0)
|
15
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
16
|
+
rspec-support (~> 3.9.0)
|
17
|
+
rspec-support (3.9.0)
|
18
|
+
simplecov (0.17.0)
|
19
|
+
docile (~> 1.1)
|
20
|
+
json (>= 1.8, < 3)
|
21
|
+
simplecov-html (~> 0.10.0)
|
22
|
+
simplecov-html (0.10.2)
|
18
23
|
|
19
24
|
PLATFORMS
|
20
25
|
ruby
|
21
26
|
|
22
27
|
DEPENDENCIES
|
28
|
+
minitest
|
23
29
|
mojo_magick!
|
24
30
|
rake
|
25
31
|
rspec-expectations
|
26
32
|
simplecov
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.17.2
|
data/README.md
CHANGED
@@ -10,6 +10,7 @@ This tool came about because I wanted a fast, simple, lightweight, nothing-goes-
|
|
10
10
|
because-it's-too-simple-to-break image tool.
|
11
11
|
|
12
12
|
[![Gem Version](https://badge.fury.io/rb/mojo_magick.png)](http://badge.fury.io/rb/mojo_magick)
|
13
|
+
[![Build Status](https://circleci.com/gh/rcode5/mojo_magick/tree/master.svg?style=shield&circle-token=8d2252bd460bf1a7cdaad5c2e169698605afb981)]()
|
13
14
|
|
14
15
|
Using it
|
15
16
|
========
|
data/Rakefile
CHANGED
@@ -1,20 +1,13 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
require 'rake/testtask'
|
2
3
|
|
3
|
-
task
|
4
|
-
|
5
|
-
|
6
|
-
task :test do
|
7
|
-
require 'simplecov'
|
8
|
-
SimpleCov.start
|
9
|
-
require 'rake/runtest'
|
10
|
-
files = Dir.glob(File.join(File.dirname(__FILE__), 'test/*_test.rb'))
|
11
|
-
files.each do |f|
|
12
|
-
Rake.run_tests f
|
13
|
-
end
|
4
|
+
task default: 'test'
|
5
|
+
Rake::TestTask.new do |task|
|
6
|
+
task.pattern = 'test/*_test.rb'
|
14
7
|
end
|
15
8
|
|
16
|
-
desc
|
9
|
+
desc 'Build gem'
|
17
10
|
task :build do
|
18
11
|
`rm mojo_magick-*.gem`
|
19
|
-
puts `gem build mojo_magick.gemspec`
|
12
|
+
puts `gem build mojo_magick.gemspec`
|
20
13
|
end
|
data/examples/animated_gif.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
require 'mojo_magick'
|
5
5
|
|
6
|
-
MojoMagick
|
6
|
+
MojoMagick.convert(nil, 'animated.gif') do |c|
|
7
7
|
c.size '200x200'
|
8
8
|
c.delay 100
|
9
9
|
c.image_block do # first layer
|
@@ -19,4 +19,3 @@ MojoMagick::convert(nil, 'animated.gif') do |c|
|
|
19
19
|
c.label 'SE'
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
data/examples/composite.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
require 'mojo_magick'
|
5
5
|
|
6
|
-
MojoMagick
|
6
|
+
MojoMagick.convert(nil, 'composite_out.png') do |c|
|
7
7
|
c.size '200x200'
|
8
8
|
c.delay 100
|
9
9
|
c.image_block do # first layer
|
@@ -20,4 +20,3 @@ MojoMagick::convert(nil, 'composite_out.png') do |c|
|
|
20
20
|
end
|
21
21
|
c.composite
|
22
22
|
end
|
23
|
-
|
data/init.rb
CHANGED
data/lib/image_magick/fonts.rb
CHANGED
@@ -3,10 +3,10 @@ module ImageMagick
|
|
3
3
|
def get_fonts
|
4
4
|
@parser ||= MojoMagick::Util::Parser.new
|
5
5
|
raw_fonts = begin
|
6
|
-
|
7
|
-
rescue Exception =>
|
8
|
-
puts
|
9
|
-
puts
|
6
|
+
raw_command('identify', '-list font')
|
7
|
+
rescue Exception => e
|
8
|
+
puts e
|
9
|
+
puts 'Failed to execute font list with raw_command - trying straight up execute'
|
10
10
|
`convert -list font`
|
11
11
|
end
|
12
12
|
@parser.parse_fonts(raw_fonts)
|
@@ -20,71 +20,66 @@ module ImageMagick
|
|
20
20
|
# MiniMagick::Image::set_limit(:disk => 5)
|
21
21
|
# # set memory to 32mb, map to 64mb and disk to 0
|
22
22
|
# MiniMagick::Image::set_limit(:memory => 32, 'map' => 64, 'disk' => 0)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
@@resource_limits[resource.to_s.downcase.to_sym] = value.to_s
|
27
|
-
end
|
23
|
+
def set_limits(options)
|
24
|
+
options.each do |resource, value|
|
25
|
+
@@resource_limits[resource.to_s.downcase.to_sym] = value.to_s
|
28
26
|
end
|
27
|
+
end
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
resource == options.values_at(idx)[0].to_s.downcase.to_sym if idx
|
36
|
-
end
|
29
|
+
# remove a limit
|
30
|
+
def remove_limits(*options)
|
31
|
+
@@resource_limits.delete_if do |resource, _value|
|
32
|
+
idx = options.index(resource)
|
33
|
+
resource == options.values_at(idx)[0].to_s.downcase.to_sym if idx
|
37
34
|
end
|
35
|
+
end
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
ENV["MAGICK_DISK_LIMIT"]=nil
|
48
|
-
end
|
37
|
+
# remove limits from resources
|
38
|
+
def unset_limits(options = {})
|
39
|
+
@@resource_limits = {}
|
40
|
+
if options[:unset_env]
|
41
|
+
ENV['MAGICK_AREA_LIMIT'] = nil
|
42
|
+
ENV['MAGICK_MAP_LIMIT'] = nil
|
43
|
+
ENV['MAGICK_MEMORY_LIMIT'] = nil
|
44
|
+
ENV['MAGICK_DISK_LIMIT'] = nil
|
49
45
|
end
|
46
|
+
end
|
50
47
|
|
51
|
-
|
48
|
+
# returns the default limits that imagemagick is using, when run with no "-limit" parameters
|
52
49
|
# options:
|
53
50
|
# :show_actual_values => true (default false) - will return integers instead of readable values
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
51
|
+
def get_default_limits(options = {})
|
52
|
+
parse_limits(options.merge(get_current_limits: false))
|
53
|
+
end
|
58
54
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
55
|
+
# returns the limits that imagemagick is running based on any "set_limits" calls
|
56
|
+
def get_current_limits(options = {})
|
57
|
+
parse_limits(options.merge(get_current_limits: true))
|
58
|
+
end
|
64
59
|
|
65
|
-
|
60
|
+
alias get_limits get_current_limits
|
66
61
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
62
|
+
def parse_limits(options)
|
63
|
+
show_actual_values = options[:show_actual_values]
|
64
|
+
if options[:get_current_limits]
|
65
|
+
status = execute('identify', '-list resource')
|
66
|
+
raw_limits = status.return_value
|
67
|
+
else
|
68
|
+
# we run a raw shell command here to obtain
|
69
|
+
# limits without applying command line limit params
|
70
|
+
raw_limits = `identify -list resource`
|
71
|
+
end
|
72
|
+
actual_values, readable_values = parser.parse_limits(raw_limits)
|
73
|
+
show_actual_values ? actual_values : readable_values
|
74
|
+
end # parse_limits
|
80
75
|
|
81
|
-
|
76
|
+
# returns a string suitable for passing as a set of imagemagick params
|
82
77
|
# that contains all the limit constraints
|
83
78
|
def get_limits_as_params
|
84
79
|
retval = ''
|
85
80
|
# we upcase the value here for newer versions of ImageMagick (>=6.8.x)
|
86
81
|
@@resource_limits.each do |type, value|
|
87
|
-
retval += " -limit #{type
|
82
|
+
retval += " -limit #{type} #{value.upcase} "
|
88
83
|
end
|
89
84
|
retval
|
90
85
|
end
|
data/lib/initializers/hash.rb
CHANGED
data/lib/mojo_magick.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
cwd = File
|
1
|
+
cwd = File.dirname(__FILE__)
|
2
2
|
require 'open3'
|
3
|
-
initializers_dir = File
|
4
|
-
Dir.glob(File
|
5
|
-
require File
|
6
|
-
require File
|
7
|
-
require File
|
8
|
-
require File
|
9
|
-
require File
|
10
|
-
require File
|
11
|
-
require File
|
3
|
+
initializers_dir = File.expand_path(File.join(cwd, 'initializers'))
|
4
|
+
Dir.glob(File.join(initializers_dir, '*.rb')).each { |f| require f }
|
5
|
+
require File.join(cwd, 'mojo_magick/util/parser')
|
6
|
+
require File.join(cwd, 'mojo_magick/errors')
|
7
|
+
require File.join(cwd, 'mojo_magick/command_status')
|
8
|
+
require File.join(cwd, 'image_magick/resource_limits')
|
9
|
+
require File.join(cwd, 'image_magick/fonts')
|
10
|
+
require File.join(cwd, 'mojo_magick/opt_builder')
|
11
|
+
require File.join(cwd, 'mojo_magick/font')
|
12
12
|
require 'tempfile'
|
13
13
|
|
14
|
-
|
15
14
|
# MojoMagick is a stateless set of module methods which present a convient interface
|
16
15
|
# for accessing common tasks for ImageMagick command line library.
|
17
16
|
#
|
@@ -62,53 +61,50 @@ require 'tempfile'
|
|
62
61
|
# instead of '-' versions.
|
63
62
|
#
|
64
63
|
module MojoMagick
|
65
|
-
|
66
64
|
# enable resource limiting functionality
|
67
65
|
extend ImageMagick::ResourceLimits
|
68
66
|
extend ImageMagick::Fonts
|
69
67
|
|
70
|
-
def
|
71
|
-
mem_fix = 1
|
68
|
+
def self.windows?
|
72
69
|
!(RUBY_PLATFORM =~ /win32/).nil?
|
73
70
|
end
|
74
71
|
|
75
|
-
def
|
72
|
+
def self.execute(command, args, _options = {})
|
76
73
|
# this suppress error messages to the console
|
77
74
|
# err_pipe = windows? ? "2>nul" : "2>/dev/null"
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
end
|
75
|
+
|
76
|
+
execute = "#{command} #{get_limits_as_params} #{args}"
|
77
|
+
out, outerr, status = Open3.capture3(execute)
|
78
|
+
CommandStatus.new execute, out, outerr, status
|
79
|
+
rescue Exception => e
|
80
|
+
raise MojoError, "#{e.class}: #{e.message}"
|
85
81
|
end
|
86
|
-
|
87
|
-
def
|
82
|
+
|
83
|
+
def self.execute!(command, args, options = {})
|
88
84
|
# this suppress error messages to the console
|
89
85
|
# err_pipe = windows? ? "2>nul" : "2>/dev/null"
|
90
86
|
status = execute(command, args, options)
|
91
|
-
|
87
|
+
unless status.success?
|
92
88
|
err_msg = options[:err_msg] || "MojoMagick command failed: #{command}."
|
93
89
|
raise(MojoFailed, "#{err_msg} (Exit status: #{status.exit_code})\n Command: #{status.command}\n Error: #{status.error}")
|
94
90
|
end
|
95
91
|
status.return_value
|
96
92
|
end
|
97
|
-
|
98
|
-
def
|
99
|
-
|
93
|
+
|
94
|
+
def self.raw_command(*args)
|
95
|
+
execute!(*args)
|
100
96
|
end
|
101
97
|
|
102
|
-
def
|
98
|
+
def self.shrink(source_file, dest_file, options)
|
103
99
|
opts = options.dup
|
104
100
|
opts.delete(:expand_only)
|
105
|
-
MojoMagick
|
101
|
+
MojoMagick.resize(source_file, dest_file, opts.merge(shrink_only: true))
|
106
102
|
end
|
107
103
|
|
108
|
-
def
|
104
|
+
def self.expand(source_file, dest_file, options)
|
109
105
|
opts = options.dup
|
110
106
|
opts.delete(:shrink_only)
|
111
|
-
MojoMagick
|
107
|
+
MojoMagick.resize(source_file, dest_file, opts.merge(expand_only: true))
|
112
108
|
end
|
113
109
|
|
114
110
|
# resizes an image and returns the filename written to
|
@@ -116,13 +112,12 @@ module MojoMagick
|
|
116
112
|
# :width / :height => scale to these dimensions
|
117
113
|
# :scale => pass scale options such as ">" to force shrink scaling only or "!" to force absolute width/height scaling (do not preserve aspect ratio)
|
118
114
|
# :percent => scale image to this percentage (do not specify :width/:height in this case)
|
119
|
-
def
|
120
|
-
retval = nil
|
115
|
+
def self.resize(source_file, dest_file, options)
|
121
116
|
scale_options = []
|
122
|
-
scale_options <<
|
123
|
-
scale_options <<
|
124
|
-
scale_options <<
|
125
|
-
scale_options <<
|
117
|
+
scale_options << '>' unless options[:shrink_only].nil?
|
118
|
+
scale_options << '<' unless options[:expand_only].nil?
|
119
|
+
scale_options << '!' unless options[:absolute_aspect].nil?
|
120
|
+
scale_options << '^' unless options[:fill].nil?
|
126
121
|
scale_options = scale_options.join
|
127
122
|
|
128
123
|
extras = []
|
@@ -134,37 +129,39 @@ module MojoMagick
|
|
134
129
|
raise MojoMagickError, "Unknown options for method resize: #{options.inspect}"
|
135
130
|
end
|
136
131
|
if !options[:fill].nil? && !options[:crop].nil?
|
137
|
-
extras <<
|
132
|
+
extras << '-gravity Center'
|
138
133
|
extras << "-extent #{geometry}"
|
139
134
|
end
|
140
|
-
|
135
|
+
raw_command('convert', "\"#{source_file}\" -resize \"#{geometry}#{scale_options}\" #{extras.join(' ')} \"#{dest_file}\"")
|
141
136
|
dest_file
|
142
137
|
end
|
143
138
|
|
144
|
-
def
|
139
|
+
def self.available_fonts
|
145
140
|
# returns width, height of image if available, nil if not
|
146
141
|
Font.all
|
147
142
|
end
|
148
143
|
|
149
|
-
def
|
150
|
-
|
144
|
+
def self.get_format(source_file, format_string)
|
145
|
+
raw_command('identify', "-format \"#{format_string}\" \"#{source_file}\"")
|
151
146
|
end
|
152
|
-
|
147
|
+
|
153
148
|
# returns an empty hash or a hash with :width and :height set (e.g. {:width => INT, :height => INT})
|
154
149
|
# raises MojoFailed when results are indeterminate (width and height could not be determined)
|
155
|
-
def
|
150
|
+
def self.get_image_size(source_file)
|
156
151
|
# returns width, height of image if available, nil if not
|
157
|
-
retval =
|
158
|
-
return {}
|
159
|
-
|
152
|
+
retval = get_format(source_file, 'w:%w h:%h')
|
153
|
+
return {} unless retval
|
154
|
+
|
155
|
+
width = retval.match(/w:([0-9]+) /)
|
160
156
|
width = width ? width[1].to_i : nil
|
161
|
-
height = retval.match(
|
157
|
+
height = retval.match(/h:([0-9]+)/)
|
162
158
|
height = height ? height[1].to_i : nil
|
163
159
|
raise(MojoFailed, "Indeterminate results in get_image_size: #{source_file}") if !height || !width
|
164
|
-
|
160
|
+
|
161
|
+
{ width: width, height: height }
|
165
162
|
end
|
166
163
|
|
167
|
-
def
|
164
|
+
def self.convert(source = nil, dest = nil)
|
168
165
|
opts = OptBuilder.new
|
169
166
|
opts.file source if source
|
170
167
|
yield opts
|
@@ -172,27 +169,22 @@ module MojoMagick
|
|
172
169
|
raw_command('convert', opts.to_s)
|
173
170
|
end
|
174
171
|
|
175
|
-
def
|
172
|
+
def self.mogrify(dest = nil)
|
176
173
|
opts = OptBuilder.new
|
177
174
|
yield opts
|
178
175
|
opts.file dest if dest
|
179
176
|
raw_command('mogrify', opts.to_s)
|
180
177
|
end
|
181
178
|
|
182
|
-
def
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
raise
|
193
|
-
ensure
|
194
|
-
file.close
|
195
|
-
end
|
179
|
+
def self.tempfile(*opts)
|
180
|
+
data = opts[0]
|
181
|
+
rest = opts[1]
|
182
|
+
ext = rest && rest[:format]
|
183
|
+
file = Tempfile.new(['mojo', ext ? '.' + ext.to_s : ''])
|
184
|
+
file.binmode
|
185
|
+
file.write(data)
|
186
|
+
file.path
|
187
|
+
ensure
|
188
|
+
file.close
|
196
189
|
end
|
197
|
-
|
198
190
|
end # MojoMagick
|