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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5c114ee3beecac084311f267201862541bab25ce
4
- data.tar.gz: c7baa01d41c1597b3ad6b43e6626d65b8e435c4c
2
+ SHA256:
3
+ metadata.gz: 30925f12cdcc0bbe8f6626d1f68266f00be28abe536a4c925522d8cbb99750dc
4
+ data.tar.gz: 1bf5dbdb09fa28ec7e5c0167d32c153f740d00c7541685840ce720d83f53c0fe
5
5
  SHA512:
6
- metadata.gz: cd34898ed515e081d77c3a82631df8505220a40bd49ad88bc014517774f86b368fab21609df815777796ec32429721d7773ed6dcd13528163e025cfa2321cd9f
7
- data.tar.gz: 395ab5c9bb6e222306d771cf0b0d388c3a8eeebdcf17e6d314d7565c2ec803b113b321f8aa3c511b1267903dfa72b294a70726614f51e50ed8e2cfdbf9f95637
6
+ metadata.gz: 29735844a173bc70ded217d142c8c11af8b78880f1396d4678837182d27c57f18e79b17c673d0174ab328e08f47a33aecb522e704701539c44f8723493bc8bab
7
+ data.tar.gz: 93907724a1c2822532e6009aef4a65b59db0e0197abbe214196b202dfbad337ab95ced02d38576ab68c420692403da1963e8d940637c99b627053afb9be7eb99
@@ -1 +1 @@
1
- 2.0.0-p353
1
+ 2.6.6
@@ -1,26 +1,35 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mojo_magick (0.5.2)
4
+ mojo_magick (0.5.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.2.4)
10
- multi_json (1.8.2)
11
- rake (0.9.2.2)
12
- rspec-expectations (2.14.3)
13
- diff-lcs (>= 1.1.3, < 2.0)
14
- simplecov (0.7.1)
15
- multi_json (~> 1.0)
16
- simplecov-html (~> 0.7.1)
17
- simplecov-html (0.7.1)
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
@@ -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 Magley & Elliott Nelson
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 'default' => :test
4
-
5
- desc "Default: run tests"
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
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'mojo_magick'
5
5
 
6
- MojoMagick::convert(nil, 'animated.gif') do |c|
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
-
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'mojo_magick'
5
5
 
6
- MojoMagick::convert(nil, 'composite_out.png') do |c|
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
@@ -1,3 +1 @@
1
- require File::expand_path(File::join(File::dirname(__FILE__), 'lib', 'mojo_magick'))
2
-
3
-
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'mojo_magick'))
@@ -3,10 +3,10 @@ module ImageMagick
3
3
  def get_fonts
4
4
  @parser ||= MojoMagick::Util::Parser.new
5
5
  raw_fonts = begin
6
- self.raw_command('identify', '-list font')
7
- rescue Exception => ex
8
- puts ex
9
- puts "Failed to execute font list with raw_command - trying straight up execute"
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
- def set_limits(options)
24
- mem_fix = 1
25
- options.each do |resource, value|
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
- # remove a limit
31
- def remove_limits(*options)
32
- mem_fix = 1
33
- @@resource_limits.delete_if do |resource, value|
34
- resource == options.values_at(options.index(resource))[0].to_s.downcase.to_sym
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
- # remove limits from resources
39
- def unset_limits(options = {})
40
- mem_fix = 1
41
- @@resource_limits = {}
42
- if options[:unset_env]
43
- ENV["MAGICK_AREA_LIMIT"]=nil
44
- ENV["MAGICK_MAP_LIMIT"]=nil
45
- ENV["MAGICK_MEMORY_LIMIT"]=nil
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
- # returns the default limits that imagemagick is using, when run with no "-limit" parameters
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
- def get_default_limits(options = {})
54
- mem_fix = 1
55
- parse_limits(options.merge(:get_current_limits => false))
56
- end
51
+ def get_default_limits(options = {})
52
+ parse_limits(options.merge(get_current_limits: false))
53
+ end
57
54
 
58
- # returns the limits that imagemagick is running based on any "set_limits" calls
59
- def get_current_limits(options = {})
60
- mem_fix = 1
61
- parse_limits(options.merge(:get_current_limits => true))
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
- alias :get_limits :get_current_limits
60
+ alias get_limits get_current_limits
65
61
 
66
- def parse_limits(options)
67
- show_actual_values = options[:show_actual_values]
68
- if options[:get_current_limits]
69
- raw_limits = self.raw_command('identify', '-list resource')
70
- else
71
- # we run a raw shell command here to obtain limits without applying command line limit params
72
- raw_limits = `identify -list resource`
73
- end
74
- actual_values, readable_values = parser.parse_limits(raw_limits)
75
- show_actual_values ? actual_values : readable_values
76
- end # parse_limits
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
- # returns a string suitable for passing as a set of imagemagick params
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.to_s} #{value.upcase} "
82
+ retval += " -limit #{type} #{value.upcase} "
85
83
  end
86
84
  retval
87
85
  end
@@ -1,7 +1,11 @@
1
1
  class Hash
2
2
  def symbolize_keys!
3
3
  keys.each do |key|
4
- self[(key.to_sym rescue key) || key] = delete(key)
4
+ self[begin
5
+ key.to_sym
6
+ rescue StandardError
7
+ key
8
+ end || key] = delete(key)
5
9
  end
6
10
  self
7
11
  end
@@ -1,186 +1,190 @@
1
- cwd = File::dirname(__FILE__)
2
- initializers_dir = File::expand_path(File::join(cwd, 'initializers'))
3
- Dir.glob(File::join(initializers_dir, '*.rb')).each { |f| require f }
4
- require File::join(cwd, 'mojo_magick/util/parser')
5
- require File::join(cwd, 'image_magick/resource_limits')
6
- require File::join(cwd, 'image_magick/fonts')
7
- require File::join(cwd, 'mojo_magick/opt_builder')
8
- require File::join(cwd, 'mojo_magick/font')
9
- require 'tempfile'
10
-
11
-
12
- # MojoMagick is a stateless set of module methods which present a convient interface
13
- # for accessing common tasks for ImageMagick command line library.
14
- #
15
- # MojoMagick is specifically designed to be efficient and simple and most importantly
16
- # to not leak any memory. For complex image operations, you will find MojoMagick limited.
17
- # You might consider the venerable MiniMagick or RMagick for your purposes if you care more
18
- # about ease of use rather than speed and memory management.
19
-
20
- # all commands raise "MojoMagick::MojoFailed" if command fails (ImageMagick determines command success status)
21
-
22
- # Two command-line builders, #convert and #mogrify, have been added to simplify
23
- # complex commands. Examples included below.
24
- #
25
- # Example #convert usage:
26
- #
27
- # MojoMagick::convert('source.jpg', 'dest.jpg') do |c|
28
- # c.crop '250x250+0+0'
29
- # c.repage!
30
- # c.strip
31
- # c.set 'comment', 'my favorite file'
32
- # end
33
- #
34
- # Equivalent to:
35
- #
36
- # MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0 +repage -strip -set comment "my favorite file" dest.jpg')
37
- #
38
- # Example #mogrify usage:
39
- #
40
- # MojoMagick::mogrify('image.jpg') {|i| i.shave '10x10'}
41
- #
42
- # Equivalent to:
43
- #
44
- # MojoMagick::raw_command('mogrify', '-shave 10x10 image.jpg')
45
- #
46
- # Example showing some additional options:
47
- #
48
- # MojoMagick::convert do |c|
49
- # c.file 'source.jpg'
50
- # c.blob my_binary_data
51
- # c.append
52
- # c.crop '256x256+0+0'
53
- # c.repage!
54
- # c.file 'output.jpg'
55
- # end
56
- #
57
- # Use .file to specify file names, .blob to create and include a tempfile. The
58
- # bang (!) can be appended to command names to use the '+' versions
59
- # instead of '-' versions.
60
- #
61
- module MojoMagick
62
-
63
- class MojoMagickException < StandardError; end
64
- class MojoError < MojoMagickException; end
65
- class MojoFailed < MojoMagickException; end
66
-
67
- # enable resource limiting functionality
68
- extend ImageMagick::ResourceLimits
69
- extend ImageMagick::Fonts
70
-
71
- def MojoMagick::windows?
72
- mem_fix = 1
73
- !(RUBY_PLATFORM =~ /win32/).nil?
74
- end
75
-
76
- def MojoMagick::raw_command(command, args, options = {})
77
- # this suppress error messages to the console
78
- err_pipe = windows? ? "2>nul" : "2>/dev/null"
79
- begin
80
- execute = "#{command} #{get_limits_as_params} #{args} #{err_pipe}"
81
- retval = `#{execute}`
82
- # guarantee that only MojoError exceptions are raised here
83
- rescue Exception => e
84
- raise MojoError, "#{e.class}: #{e.message}"
85
- end
86
- if $? && !$?.success?
87
- err_msg = options[:err_msg] || "MojoMagick command failed: #{command}."
88
- raise(MojoFailed, "#{err_msg} (Exit status: #{$?.exitstatus})\n Command: #{execute}")
89
- end
90
- retval
91
- end
92
-
93
- def MojoMagick::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 MojoMagick::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 MojoMagick::resize(source_file, dest_file, options)
111
- retval = nil
112
- scale_options = []
113
- scale_options << ">" unless options[:shrink_only].nil?
114
- scale_options << "<" unless options[:expand_only].nil?
115
- scale_options << "!" unless options[:absolute_aspect].nil?
116
- scale_options << "^" unless options[:fill].nil?
117
- scale_options = scale_options.join
118
-
119
- extras = []
120
- if !options[:width].nil? && !options[:height].nil?
121
- geometry = "#{options[:width]}X#{options[:height]}"
122
- elsif !options[:percent].nil?
123
- geometry = "#{options[:percent]}%"
124
- else
125
- raise MojoMagickError, "Unknown options for method resize: #{options.inspect}"
126
- end
127
- if !options[:fill].nil? && !options[:crop].nil?
128
- extras << "-gravity Center"
129
- extras << "-extent #{geometry}"
130
- end
131
- retval = raw_command("convert", "\"#{source_file}\" -resize \"#{geometry}#{scale_options}\" #{extras.join(' ')} \"#{dest_file}\"")
132
- dest_file
133
- end
134
-
135
- def MojoMagick::available_fonts
136
- # returns width, height of image if available, nil if not
137
- Font.all
138
- end
139
-
140
- # returns an empty hash or a hash with :width and :height set (e.g. {:width => INT, :height => INT})
141
- # raises MojoFailed when results are indeterminate (width and height could not be determined)
142
- def MojoMagick::get_image_size(source_file)
143
- # returns width, height of image if available, nil if not
144
- retval = raw_command("identify", "-format \"w:%w h:%h\" \"#{source_file}\"")
145
- return {} if !retval
146
- width = retval.match(%r{w:([0-9]+) })
147
- width = width ? width[1].to_i : nil
148
- height = retval.match(%r{h:([0-9]+)})
149
- height = height ? height[1].to_i : nil
150
- raise(MojoFailed, "Indeterminate results in get_image_size: #{source_file}") if !height || !width
151
- {:width=>width, :height=>height}
152
- end
153
-
154
- def MojoMagick::convert(source = nil, dest = nil)
155
- opts = OptBuilder.new
156
- opts.file source if source
157
- yield opts
158
- opts.file dest if dest
159
- raw_command('convert', opts.to_s)
160
- end
161
-
162
- def MojoMagick::mogrify(dest = nil)
163
- opts = OptBuilder.new
164
- yield opts
165
- opts.file dest if dest
166
- raw_command('mogrify', opts.to_s)
167
- end
168
-
169
-
170
- def MojoMagick::tempfile(*opts)
171
- begin
172
- data = opts[0]
173
- rest = opts[1]
174
- ext = rest && rest[:format]
175
- file = Tempfile.new(["mojo", ext ? '.' + ext.to_s : ''])
176
- file.binmode
177
- file.write(data)
178
- file.path
179
- rescue Exception => ex
180
- raise
181
- end
182
- ensure
183
- file.close
184
- end
185
-
186
- end # MojoMagick
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