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
1
require "tempfile"
2
+ require "open3"
3
+ require_relative "./mojo_magick/util/font_parser"
4
+ require_relative "./mojo_magick/errors"
5
+ require_relative "./mojo_magick/command_status"
6
+ require_relative "./mojo_magick/commands"
7
+ require_relative "./image_magick/fonts"
8
+ require_relative "./mojo_magick/opt_builder"
9
+ require_relative "./mojo_magick/font"
12
10
13
11
# MojoMagick is a stateless set of module methods which present a convient interface
14
12
# for accessing common tasks for ImageMagick command line library.
34
32
#
35
33
# Equivalent to:
36
34
#
37
- # MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0\
35
+ # MojoMagick::Commands. raw_command('convert', 'source.jpg -crop 250x250+0+0\
38
36
# +repage -strip -set comment "my favorite file" dest.jpg')
39
37
#
40
38
# Example #mogrify usage:
43
41
#
44
42
# Equivalent to:
45
43
#
46
- # MojoMagick::raw_command('mogrify', '-shave 10x10 image.jpg')
44
+ # MojoMagick::Commands. raw_command('mogrify', '-shave 10x10 image.jpg')
47
45
#
48
46
# Example showing some additional options:
49
47
#
60
58
# bang (!) can be appended to command names to use the '+' versions
61
59
# instead of '-' versions.
62
60
#
63
- module MojoMagick
64
- extend ImageMagick ::Fonts
65
61
66
- def self . windows?
67
- !( RUBY_PLATFORM =~ /win32/ ) . nil?
62
+ module MojoMagickDeprecations
63
+ # rubocop:disable Naming/AccessorMethodName
64
+ def get_fonts
65
+ warn "DEPRECATION WARNING: #{ __method__ } is deprecated and will be removed with the next minor version release." \
66
+ " Please use `available_fonts` instead"
67
+ MojoMagick . available_fonts
68
68
end
69
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 } "
70
+ # rubocop:enable Naming/AccessorMethodName
71
+ ### Moved to `Commands`
72
+ def execute! ( *args )
73
+ warn "DEPRECATION WARNING: #{ __method__ } is deprecated and will be removed with the next minor version release." \
74
+ " Please use `MojoMagick::Commands.execute!` instead"
75
+ MojoMagick :: Commands . send ( :execute! , * args )
76
76
end
77
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
78
+ def execute ( *args )
79
+ warn "DEPRECATION WARNING: #{ __method__ } is deprecated and will be removed with the next minor version release." \
80
+ " Please use `MojoMagick::Commands.execute!` instead"
81
+ MojoMagick ::Commands . send ( :execute , *args )
82
+ end
83
+
84
+ def raw_command ( *args )
85
+ warn "DEPRECATION WARNING: #{ __method__ } is deprecated and will be removed with the next minor version release." \
86
+ " Please use `MojoMagick::Commands.execute!` instead"
87
+ MojoMagick ::Commands . raw_command ( *args )
87
88
end
89
+ end
88
90
89
- def self . raw_command ( *args )
90
- execute! ( *args )
91
+ module MojoMagick
92
+ extend MojoMagickDeprecations
93
+ def self . windows?
94
+ !RUBY_PLATFORM . include ( win32 )
91
95
end
92
96
93
97
def self . shrink ( source_file , dest_file , options )
@@ -105,44 +109,49 @@ def self.expand(source_file, dest_file, options)
105
109
# resizes an image and returns the filename written to
106
110
# options:
107
111
# :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)
112
+ # :scale => pass scale options such as ">" to force shrink scaling only or
113
+ # "!" to force absolute width/height scaling (do not preserve aspect ratio)
109
114
# :percent => scale image to this percentage (do not specify :width/:height in this case)
110
115
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
-
116
+ scale_options = extract_scale_options ( options )
117
+ geometry = extract_geometry_options ( options )
118
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
119
if !options [ :fill ] . nil? && !options [ :crop ] . nil?
127
120
extras << "-gravity"
128
121
extras << "Center"
129
122
extras << "-extent"
130
123
extras << geometry . to_s
131
124
end
132
- raw_command ( "convert" ,
133
- source_file ,
134
- "-resize" , "#{ geometry } #{ scale_options } " ,
135
- *extras , dest_file )
125
+ Commands . raw_command ( "convert" ,
126
+ source_file ,
127
+ "-resize" , "#{ geometry } #{ scale_options } " ,
128
+ *extras , dest_file )
136
129
dest_file
137
130
end
138
131
132
+ def self . convert ( source = nil , dest = nil )
133
+ opts = OptBuilder . new
134
+ opts . file source if source
135
+ yield opts
136
+ opts . file dest if dest
137
+
138
+ Commands . raw_command ( "convert" , *opts . to_a )
139
+ end
140
+
141
+ def self . mogrify ( dest = nil )
142
+ opts = OptBuilder . new
143
+ yield opts
144
+ opts . file dest if dest
145
+ Commands . raw_command ( "mogrify" , *opts . to_a )
146
+ end
147
+
139
148
def self . available_fonts
140
149
# returns width, height of image if available, nil if not
141
150
Font . all
142
151
end
143
152
144
153
def self . get_format ( source_file , format_string )
145
- raw_command ( "identify" , "-format" , format_string , source_file )
154
+ Commands . raw_command ( "identify" , "-format" , format_string , source_file )
146
155
end
147
156
148
157
# returns an empty hash or a hash with :width and :height set (e.g. {:width => INT, :height => INT})
@@ -161,22 +170,6 @@ def self.get_image_size(source_file)
161
170
{ width : width , height : height }
162
171
end
163
172
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
173
def self . tempfile ( *opts )
181
174
data = opts [ 0 ]
182
175
rest = opts [ 1 ]
@@ -188,4 +181,27 @@ def self.tempfile(*opts)
188
181
ensure
189
182
file . close
190
183
end
184
+
185
+ class << self
186
+ private
187
+
188
+ def extract_geometry_options ( options )
189
+ if !options [ :width ] . nil? && !options [ :height ] . nil?
190
+ "#{ options [ :width ] } X#{ options [ :height ] } "
191
+ elsif !options [ :percent ] . nil?
192
+ "#{ options [ :percent ] } %"
193
+ else
194
+ raise MojoMagickError , "Resize requires width and height or percentage: #{ options . inspect } "
195
+ end
196
+ end
197
+
198
+ def extract_scale_options ( options )
199
+ [ ] . tap { |scale_options |
200
+ scale_options << ">" unless options [ :shrink_only ] . nil?
201
+ scale_options << "<" unless options [ :expand_only ] . nil?
202
+ scale_options << "!" unless options [ :absolute_aspect ] . nil?
203
+ scale_options << "^" unless options [ :fill ] . nil?
204
+ } . join
205
+ end
206
+ end
191
207
end
0 commit comments