mojo_magick 0.5.2 → 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/LICENSE.txt +1 -1
- data/README.md +34 -0
- data/Rakefile +6 -12
- 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 -45
- data/lib/initializers/hash.rb +5 -1
- data/lib/mojo_magick.rb +190 -186
- 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 +21 -9
- data/lib/mojo_magick/util/parser.rb +25 -28
- data/lib/mojo_magick/version.rb +1 -1
- data/mojo_magick.gemspec +19 -17
- data/test/font_test.rb +19 -25
- data/test/fonts_test.rb +4 -5
- data/test/mojo_magick_test.rb +277 -256
- data/test/opt_builder_test.rb +31 -14
- data/test/parser_test.rb +15 -16
- data/test/resource_limits_test.rb +33 -21
- data/test/test_helper.rb +3 -3
- metadata +36 -20
- data/lib/image_resources.rb +0 -0
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/LICENSE.txt
CHANGED
@@ -2,7 +2,7 @@ MojoMagick is released under the MIT license
|
|
2
2
|
|
3
3
|
The MIT License (MIT)
|
4
4
|
|
5
|
-
Copyright (c) 2013 Jon Rogers, Steve
|
5
|
+
Copyright (c) 2013 Jon Rogers, Steve Midgley & Elliott Nelson
|
6
6
|
|
7
7
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
8
|
of this software and associated documentation files (the "Software"), to deal
|
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
|
========
|
@@ -176,14 +177,47 @@ Note: Use with care. If you don't have fonts installed ImageMagick can spin off
|
|
176
177
|
c.composite
|
177
178
|
end
|
178
179
|
|
180
|
+
Dependencies
|
181
|
+
============
|
182
|
+
|
183
|
+
This library has (in the past) been good for ruby 1.8.7 and beyond.
|
184
|
+
Recent mods (as of 0.5.x) will require a more recent ruby (>1.9.3).
|
185
|
+
|
186
|
+
If you're running on 1.8.7, you should be able to safely use 0.4.3
|
187
|
+
|
188
|
+
Do this by pinning it in your Gemfile
|
189
|
+
|
190
|
+
gem 'mojo_magick', '0.4.3'
|
179
191
|
|
180
192
|
Availablility
|
181
193
|
=============
|
182
194
|
* [Github Repo](http://github.com/rcode5/mojo_magick) This is the current canonical branch.
|
183
195
|
* Issues/Pull Requests can be submitted through the above repository.
|
184
196
|
|
197
|
+
Contributions
|
198
|
+
=============
|
199
|
+
|
200
|
+
Got a fix? Got a feature?
|
201
|
+
|
202
|
+
* fork it
|
203
|
+
* make a branch (named appropriately)
|
204
|
+
* write your code
|
205
|
+
* write your tests
|
206
|
+
* test it (`rake` will run the tests)
|
207
|
+
* submit a pull request
|
208
|
+
|
209
|
+
Note: please don't change the version. We'll do that when we merge
|
210
|
+
in the new code
|
211
|
+
|
185
212
|
Recent Changes
|
186
213
|
==============
|
214
|
+
#### Version 0.5.4
|
215
|
+
|
216
|
+
* Include image magick commandline failure from raw_command (on failure)
|
217
|
+
* moved to Popen3
|
218
|
+
* added checks for Popen3 on windows
|
219
|
+
* updated README to include info about submissions
|
220
|
+
* probably no good for ruby 1.8.7 anymore
|
187
221
|
|
188
222
|
#### Version 0.5.1
|
189
223
|
|
data/Rakefile
CHANGED
@@ -1,19 +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
|
|
9
|
+
desc 'Build gem'
|
16
10
|
task :build do
|
17
11
|
`rm mojo_magick-*.gem`
|
18
|
-
puts `gem build mojo_magick.gemspec`
|
12
|
+
puts `gem build mojo_magick.gemspec`
|
19
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,68 +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
|
-
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
|
36
34
|
end
|
35
|
+
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
ENV["MAGICK_DISK_LIMIT"]=nil
|
47
|
-
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
|
48
45
|
end
|
46
|
+
end
|
49
47
|
|
50
|
-
|
48
|
+
# returns the default limits that imagemagick is using, when run with no "-limit" parameters
|
51
49
|
# options:
|
52
50
|
# :show_actual_values => true (default false) - will return integers instead of readable values
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
51
|
+
def get_default_limits(options = {})
|
52
|
+
parse_limits(options.merge(get_current_limits: false))
|
53
|
+
end
|
57
54
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
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
|
63
59
|
|
64
|
-
|
60
|
+
alias get_limits get_current_limits
|
65
61
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
77
75
|
|
78
|
-
|
76
|
+
# returns a string suitable for passing as a set of imagemagick params
|
79
77
|
# that contains all the limit constraints
|
80
78
|
def get_limits_as_params
|
81
79
|
retval = ''
|
82
80
|
# we upcase the value here for newer versions of ImageMagick (>=6.8.x)
|
83
81
|
@@resource_limits.each do |type, value|
|
84
|
-
retval += " -limit #{type
|
82
|
+
retval += " -limit #{type} #{value.upcase} "
|
85
83
|
end
|
86
84
|
retval
|
87
85
|
end
|
data/lib/initializers/hash.rb
CHANGED
data/lib/mojo_magick.rb
CHANGED
@@ -1,186 +1,190 @@
|
|
1
|
-
cwd = File
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require File
|
6
|
-
require File
|
7
|
-
require File
|
8
|
-
require File
|
9
|
-
require '
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
|
20
|
-
#
|
21
|
-
|
22
|
-
#
|
23
|
-
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
# c.
|
31
|
-
# c.
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
# c.
|
52
|
-
# c.
|
53
|
-
# c.
|
54
|
-
# c.
|
55
|
-
#
|
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
|
-
opts
|
101
|
-
opts.
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
scale_options
|
117
|
-
scale_options
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
end
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
opts
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
file.
|
184
|
-
|
185
|
-
|
186
|
-
|
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')).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
|
+
require 'tempfile'
|
13
|
+
|
14
|
+
# MojoMagick is a stateless set of module methods which present a convient interface
|
15
|
+
# for accessing common tasks for ImageMagick command line library.
|
16
|
+
#
|
17
|
+
# MojoMagick is specifically designed to be efficient and simple and most importantly
|
18
|
+
# to not leak any memory. For complex image operations, you will find MojoMagick limited.
|
19
|
+
# You might consider the venerable MiniMagick or RMagick for your purposes if you care more
|
20
|
+
# about ease of use rather than speed and memory management.
|
21
|
+
|
22
|
+
# all commands raise "MojoMagick::MojoFailed" if command fails (ImageMagick determines command success status)
|
23
|
+
|
24
|
+
# Two command-line builders, #convert and #mogrify, have been added to simplify
|
25
|
+
# complex commands. Examples included below.
|
26
|
+
#
|
27
|
+
# Example #convert usage:
|
28
|
+
#
|
29
|
+
# MojoMagick::convert('source.jpg', 'dest.jpg') do |c|
|
30
|
+
# c.crop '250x250+0+0'
|
31
|
+
# c.repage!
|
32
|
+
# c.strip
|
33
|
+
# c.set 'comment', 'my favorite file'
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# Equivalent to:
|
37
|
+
#
|
38
|
+
# MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0 +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
|
+
# enable resource limiting functionality
|
65
|
+
extend ImageMagick::ResourceLimits
|
66
|
+
extend ImageMagick::Fonts
|
67
|
+
|
68
|
+
def self.windows?
|
69
|
+
!(RUBY_PLATFORM =~ /win32/).nil?
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.execute(command, args, _options = {})
|
73
|
+
# this suppress error messages to the console
|
74
|
+
# err_pipe = windows? ? "2>nul" : "2>/dev/null"
|
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}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.execute!(command, args, options = {})
|
84
|
+
# this suppress error messages to the console
|
85
|
+
# err_pipe = windows? ? "2>nul" : "2>/dev/null"
|
86
|
+
status = execute(command, args, options)
|
87
|
+
unless status.success?
|
88
|
+
err_msg = options[:err_msg] || "MojoMagick command failed: #{command}."
|
89
|
+
raise(MojoFailed, "#{err_msg} (Exit status: #{status.exit_code})\n Command: #{status.command}\n Error: #{status.error}")
|
90
|
+
end
|
91
|
+
status.return_value
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.raw_command(*args)
|
95
|
+
execute!(*args)
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.shrink(source_file, dest_file, options)
|
99
|
+
opts = options.dup
|
100
|
+
opts.delete(:expand_only)
|
101
|
+
MojoMagick.resize(source_file, dest_file, opts.merge(shrink_only: true))
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.expand(source_file, dest_file, options)
|
105
|
+
opts = options.dup
|
106
|
+
opts.delete(:shrink_only)
|
107
|
+
MojoMagick.resize(source_file, dest_file, opts.merge(expand_only: true))
|
108
|
+
end
|
109
|
+
|
110
|
+
# resizes an image and returns the filename written to
|
111
|
+
# options:
|
112
|
+
# :width / :height => scale to these dimensions
|
113
|
+
# :scale => pass scale options such as ">" to force shrink scaling only or "!" to force absolute width/height scaling (do not preserve aspect ratio)
|
114
|
+
# :percent => scale image to this percentage (do not specify :width/:height in this case)
|
115
|
+
def self.resize(source_file, dest_file, options)
|
116
|
+
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?
|
121
|
+
scale_options = scale_options.join
|
122
|
+
|
123
|
+
extras = []
|
124
|
+
if !options[:width].nil? && !options[:height].nil?
|
125
|
+
geometry = "#{options[:width]}X#{options[:height]}"
|
126
|
+
elsif !options[:percent].nil?
|
127
|
+
geometry = "#{options[:percent]}%"
|
128
|
+
else
|
129
|
+
raise MojoMagickError, "Unknown options for method resize: #{options.inspect}"
|
130
|
+
end
|
131
|
+
if !options[:fill].nil? && !options[:crop].nil?
|
132
|
+
extras << '-gravity Center'
|
133
|
+
extras << "-extent #{geometry}"
|
134
|
+
end
|
135
|
+
raw_command('convert', "\"#{source_file}\" -resize \"#{geometry}#{scale_options}\" #{extras.join(' ')} \"#{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
|
+
raw_command('convert', opts.to_s)
|
170
|
+
end
|
171
|
+
|
172
|
+
def self.mogrify(dest = nil)
|
173
|
+
opts = OptBuilder.new
|
174
|
+
yield opts
|
175
|
+
opts.file dest if dest
|
176
|
+
raw_command('mogrify', opts.to_s)
|
177
|
+
end
|
178
|
+
|
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
|
189
|
+
end
|
190
|
+
end # MojoMagick
|