mojo_magick 0.5.4 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +24 -0
- data/.github/dependabot.yml +8 -0
- data/.rubocop-common.yml +99 -0
- data/.rubocop-performance.yml +2 -0
- data/.rubocop-rspec.yml +33 -0
- data/.rubocop.yml +10 -0
- data/.ruby-version +1 -1
- data/Gemfile +6 -1
- data/Gemfile.lock +58 -10
- data/README.md +54 -2
- data/Rakefile +15 -12
- data/examples/animated_gif.rb +11 -14
- data/examples/composite.rb +11 -14
- data/init.rb +1 -3
- data/lib/image_magick/fonts.rb +6 -6
- data/lib/initializers/hash.rb +5 -1
- data/lib/mojo_magick.rb +191 -188
- data/lib/mojo_magick/command_status.rb +11 -0
- data/lib/mojo_magick/errors.rb +5 -0
- data/lib/mojo_magick/font.rb +4 -6
- data/lib/mojo_magick/opt_builder.rb +25 -34
- data/lib/mojo_magick/util/parser.rb +7 -48
- data/lib/mojo_magick/version.rb +1 -1
- data/mojo_magick.gemspec +20 -15
- data/test/fixtures/roll with it.jpg +0 -0
- data/test/font_test.rb +22 -28
- data/test/fonts_test.rb +4 -5
- data/test/mojo_magick_test.rb +282 -283
- data/test/opt_builder_test.rb +56 -49
- data/test/parser_test.rb +17 -18
- data/test/test_helper.rb +7 -5
- metadata +86 -23
- data/lib/image_magick/resource_limits.rb +0 -94
- data/test/resource_limits_test.rb +0 -51
data/examples/composite.rb
CHANGED
@@ -1,23 +1,20 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
2
|
+
require "mojo_magick"
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
MojoMagick::convert(nil, 'composite_out.png') do |c|
|
7
|
-
c.size '200x200'
|
4
|
+
MojoMagick.convert(nil, "composite_out.png") do |c|
|
5
|
+
c.size "200x200"
|
8
6
|
c.delay 100
|
9
7
|
c.image_block do # first layer
|
10
|
-
c.background
|
11
|
-
c.fill
|
12
|
-
c.gravity
|
13
|
-
c.label
|
8
|
+
c.background "blue"
|
9
|
+
c.fill "white"
|
10
|
+
c.gravity "northwest"
|
11
|
+
c.label "NW"
|
14
12
|
end
|
15
13
|
c.image_block do # second layer
|
16
|
-
c.background
|
17
|
-
c.fill
|
18
|
-
c.gravity
|
19
|
-
c.label
|
14
|
+
c.background "transparent"
|
15
|
+
c.fill "red"
|
16
|
+
c.gravity "southeast"
|
17
|
+
c.label "SE"
|
20
18
|
end
|
21
19
|
c.composite
|
22
20
|
end
|
23
|
-
|
data/init.rb
CHANGED
data/lib/image_magick/fonts.rb
CHANGED
@@ -3,12 +3,12 @@ module ImageMagick
|
|
3
3
|
def get_fonts
|
4
4
|
@parser ||= MojoMagick::Util::Parser.new
|
5
5
|
raw_fonts = begin
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
+
`convert -list font`
|
11
|
+
end
|
12
12
|
@parser.parse_fonts(raw_fonts)
|
13
13
|
end
|
14
14
|
end
|
data/lib/initializers/hash.rb
CHANGED
data/lib/mojo_magick.rb
CHANGED
@@ -1,188 +1,191 @@
|
|
1
|
-
cwd = File
|
2
|
-
require
|
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
|
11
|
-
|
12
|
-
|
13
|
-
# MojoMagick is a stateless set of module methods which present a convient interface
|
14
|
-
# for accessing common tasks for ImageMagick command line library.
|
15
|
-
#
|
16
|
-
# MojoMagick is specifically designed to be efficient and simple and most importantly
|
17
|
-
# to not leak any memory. For complex image operations, you will find MojoMagick limited.
|
18
|
-
# You might consider the venerable MiniMagick or RMagick for your purposes if you care more
|
19
|
-
# about ease of use rather than speed and memory management.
|
20
|
-
|
21
|
-
# all commands raise "MojoMagick::MojoFailed" if command fails (ImageMagick determines command success status)
|
22
|
-
|
23
|
-
# Two command-line builders, #convert and #mogrify, have been added to simplify
|
24
|
-
# complex commands. Examples included below.
|
25
|
-
#
|
26
|
-
# Example #convert usage:
|
27
|
-
#
|
28
|
-
# MojoMagick::convert('source.jpg', 'dest.jpg') do |c|
|
29
|
-
# c.crop '250x250+0+0'
|
30
|
-
# c.repage!
|
31
|
-
# c.strip
|
32
|
-
# c.set 'comment', 'my favorite file'
|
33
|
-
# end
|
34
|
-
#
|
35
|
-
# Equivalent to:
|
36
|
-
#
|
37
|
-
# MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
# c.
|
52
|
-
# c.
|
53
|
-
# c.
|
54
|
-
# c.
|
55
|
-
# c.
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
#
|
109
|
-
#
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
scale_options
|
116
|
-
scale_options
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
height
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
opts
|
167
|
-
yield opts
|
168
|
-
opts.file dest if dest
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
1
|
+
cwd = File.dirname(__FILE__)
|
2
|
+
require "open3"
|
3
|
+
initializers_dir = File.expand_path(File.join(cwd, "initializers"))
|
4
|
+
Dir.glob(File.join(initializers_dir, "*.rb")).sort.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/fonts")
|
9
|
+
require File.join(cwd, "mojo_magick/opt_builder")
|
10
|
+
require File.join(cwd, "mojo_magick/font")
|
11
|
+
require "tempfile"
|
12
|
+
|
13
|
+
# MojoMagick is a stateless set of module methods which present a convient interface
|
14
|
+
# for accessing common tasks for ImageMagick command line library.
|
15
|
+
#
|
16
|
+
# MojoMagick is specifically designed to be efficient and simple and most importantly
|
17
|
+
# to not leak any memory. For complex image operations, you will find MojoMagick limited.
|
18
|
+
# You might consider the venerable MiniMagick or RMagick for your purposes if you care more
|
19
|
+
# about ease of use rather than speed and memory management.
|
20
|
+
|
21
|
+
# all commands raise "MojoMagick::MojoFailed" if command fails (ImageMagick determines command success status)
|
22
|
+
|
23
|
+
# Two command-line builders, #convert and #mogrify, have been added to simplify
|
24
|
+
# complex commands. Examples included below.
|
25
|
+
#
|
26
|
+
# Example #convert usage:
|
27
|
+
#
|
28
|
+
# MojoMagick::convert('source.jpg', 'dest.jpg') do |c|
|
29
|
+
# c.crop '250x250+0+0'
|
30
|
+
# c.repage!
|
31
|
+
# c.strip
|
32
|
+
# c.set 'comment', 'my favorite file'
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# Equivalent to:
|
36
|
+
#
|
37
|
+
# MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0\
|
38
|
+
# +repage -strip -set comment "my favorite file" dest.jpg')
|
39
|
+
#
|
40
|
+
# Example #mogrify usage:
|
41
|
+
#
|
42
|
+
# MojoMagick::mogrify('image.jpg') {|i| i.shave '10x10'}
|
43
|
+
#
|
44
|
+
# Equivalent to:
|
45
|
+
#
|
46
|
+
# MojoMagick::raw_command('mogrify', '-shave 10x10 image.jpg')
|
47
|
+
#
|
48
|
+
# Example showing some additional options:
|
49
|
+
#
|
50
|
+
# MojoMagick::convert do |c|
|
51
|
+
# c.file 'source.jpg'
|
52
|
+
# c.blob my_binary_data
|
53
|
+
# c.append
|
54
|
+
# c.crop '256x256+0+0'
|
55
|
+
# c.repage!
|
56
|
+
# c.file 'output.jpg'
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# Use .file to specify file names, .blob to create and include a tempfile. The
|
60
|
+
# bang (!) can be appended to command names to use the '+' versions
|
61
|
+
# instead of '-' versions.
|
62
|
+
#
|
63
|
+
module MojoMagick
|
64
|
+
extend ImageMagick::Fonts
|
65
|
+
|
66
|
+
def self.windows?
|
67
|
+
!(RUBY_PLATFORM =~ /win32/).nil?
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.execute(command, *args)
|
71
|
+
execute = "#{command} #{args}"
|
72
|
+
out, outerr, status = Open3.capture3(command, *args.map(&:to_s))
|
73
|
+
CommandStatus.new execute, out, outerr, status
|
74
|
+
rescue Exception => e
|
75
|
+
raise MojoError, "#{e.class}: #{e.message}"
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.execute!(command, *args)
|
79
|
+
status = execute(command, *args)
|
80
|
+
unless status.success?
|
81
|
+
err_msg = "MojoMagick command failed: #{command}."
|
82
|
+
raise(MojoFailed, "#{err_msg} (Exit status: #{status.exit_code})\n" +
|
83
|
+
" Command: #{status.command}\n" +
|
84
|
+
" Error: #{status.error}")
|
85
|
+
end
|
86
|
+
status.return_value
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.raw_command(*args)
|
90
|
+
execute!(*args)
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.shrink(source_file, dest_file, options)
|
94
|
+
opts = options.dup
|
95
|
+
opts.delete(:expand_only)
|
96
|
+
MojoMagick.resize(source_file, dest_file, opts.merge(shrink_only: true))
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.expand(source_file, dest_file, options)
|
100
|
+
opts = options.dup
|
101
|
+
opts.delete(:shrink_only)
|
102
|
+
MojoMagick.resize(source_file, dest_file, opts.merge(expand_only: true))
|
103
|
+
end
|
104
|
+
|
105
|
+
# resizes an image and returns the filename written to
|
106
|
+
# options:
|
107
|
+
# :width / :height => scale to these dimensions
|
108
|
+
# :scale => pass scale options such as ">" to force shrink scaling only or "!" to force absolute width/height scaling (do not preserve aspect ratio)
|
109
|
+
# :percent => scale image to this percentage (do not specify :width/:height in this case)
|
110
|
+
def self.resize(source_file, dest_file, options)
|
111
|
+
scale_options = []
|
112
|
+
scale_options << ">" unless options[:shrink_only].nil?
|
113
|
+
scale_options << "<" unless options[:expand_only].nil?
|
114
|
+
scale_options << "!" unless options[:absolute_aspect].nil?
|
115
|
+
scale_options << "^" unless options[:fill].nil?
|
116
|
+
scale_options = scale_options.join
|
117
|
+
|
118
|
+
extras = []
|
119
|
+
if !options[:width].nil? && !options[:height].nil?
|
120
|
+
geometry = "#{options[:width]}X#{options[:height]}"
|
121
|
+
elsif !options[:percent].nil?
|
122
|
+
geometry = "#{options[:percent]}%"
|
123
|
+
else
|
124
|
+
raise MojoMagickError, "Unknown options for method resize: #{options.inspect}"
|
125
|
+
end
|
126
|
+
if !options[:fill].nil? && !options[:crop].nil?
|
127
|
+
extras << "-gravity"
|
128
|
+
extras << "Center"
|
129
|
+
extras << "-extent"
|
130
|
+
extras << geometry.to_s
|
131
|
+
end
|
132
|
+
raw_command("convert",
|
133
|
+
source_file,
|
134
|
+
"-resize", "#{geometry}#{scale_options}",
|
135
|
+
*extras, dest_file)
|
136
|
+
dest_file
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.available_fonts
|
140
|
+
# returns width, height of image if available, nil if not
|
141
|
+
Font.all
|
142
|
+
end
|
143
|
+
|
144
|
+
def self.get_format(source_file, format_string)
|
145
|
+
raw_command("identify", "-format", format_string, source_file)
|
146
|
+
end
|
147
|
+
|
148
|
+
# returns an empty hash or a hash with :width and :height set (e.g. {:width => INT, :height => INT})
|
149
|
+
# raises MojoFailed when results are indeterminate (width and height could not be determined)
|
150
|
+
def self.get_image_size(source_file)
|
151
|
+
# returns width, height of image if available, nil if not
|
152
|
+
retval = get_format(source_file, "w:%w h:%h")
|
153
|
+
return {} unless retval
|
154
|
+
|
155
|
+
width = retval.match(/w:([0-9]+) /)
|
156
|
+
width = width ? width[1].to_i : nil
|
157
|
+
height = retval.match(/h:([0-9]+)/)
|
158
|
+
height = height ? height[1].to_i : nil
|
159
|
+
raise(MojoFailed, "Indeterminate results in get_image_size: #{source_file}") if !height || !width
|
160
|
+
|
161
|
+
{ width: width, height: height }
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.convert(source = nil, dest = nil)
|
165
|
+
opts = OptBuilder.new
|
166
|
+
opts.file source if source
|
167
|
+
yield opts
|
168
|
+
opts.file dest if dest
|
169
|
+
|
170
|
+
raw_command("convert", *opts.to_a)
|
171
|
+
end
|
172
|
+
|
173
|
+
def self.mogrify(dest = nil)
|
174
|
+
opts = OptBuilder.new
|
175
|
+
yield opts
|
176
|
+
opts.file dest if dest
|
177
|
+
raw_command("mogrify", *opts.to_a)
|
178
|
+
end
|
179
|
+
|
180
|
+
def self.tempfile(*opts)
|
181
|
+
data = opts[0]
|
182
|
+
rest = opts[1]
|
183
|
+
ext = rest && rest[:format]
|
184
|
+
file = Tempfile.new(["mojo", ext ? ".#{ext}" : ""])
|
185
|
+
file.binmode
|
186
|
+
file.write(data)
|
187
|
+
file.path
|
188
|
+
ensure
|
189
|
+
file.close
|
190
|
+
end
|
191
|
+
end
|