Class: JekyllImgFlow::Providers::Libvips
Overview
Libvips provider implementation using the standardized tag interface
Constant Summary
Constants inherited
from BaseProvider
BaseProvider::SMARTCROP_POSITIONS
Instance Attribute Summary
Attributes inherited from BaseProvider
#config
Instance Method Summary
collapse
-
#available? ⇒ Boolean
-
#build_alpha_command(input_path, output_path) ⇒ Object
-
#build_alpha_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
-
#build_composite_command(base_path, output_path) ⇒ Object
-
#build_copy_command(input_path, output_path) ⇒ Object
-
#build_crop_command(input_path, output_path) ⇒ Object
-
#build_format_spec(output_path) ⇒ Object
-
#build_resize_command(input_path, output_path) ⇒ Object
-
#build_sequential_crop_resize(input_path, output_path) ⇒ Object
-
#build_vips_command(input_path, output_path) ⇒ Object
-
#build_watermark_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
-
#execute(input_path, output_path) ⇒ Object
-
#execute_command(command) ⇒ Object
-
#position_to_vips_xy(position, _base_path, watermark_path) ⇒ Object
#add_watermark, #alpha_opacity=, #check_http_service, #convert_format, #crop, #encode_file_url, #format, #initialize, #opacity, #operations, #optimize, provider_name, #quality, #quality=, #reset_operations, #resize, #supports_operation?, supports_operation?, unsupported_operations, #watermark
Instance Method Details
#available? ⇒ Boolean
11
12
13
14
15
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 11
def available?
_, _, status = Open3.capture3("which", "vips")
status.success?
end
|
#build_alpha_command(input_path, output_path) ⇒ Object
151
152
153
154
155
156
157
158
159
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 151
def build_alpha_command(input_path, output_path)
alpha_op = @operations.find { |op| op[:type] == :alpha_opacity }
opacity = alpha_op[:opacity]
alpha_value = opacity.round(2)
["vips", "linear", input_path.shellescape, output_path.shellescape,
"\"1 1 1 #{alpha_value}\"", "0"].join(" ")
end
|
#build_alpha_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 49
def build_alpha_pipeline(input_path, output_path, has_crop, has_resize)
commands = []
temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_base.jpg")
if has_crop && has_resize
commands << build_sequential_crop_resize(input_path, temp_path)
base_path = temp_path
elsif has_resize
commands << build_resize_command(input_path, temp_path)
base_path = temp_path
elsif has_crop
commands << build_crop_command(input_path, temp_path)
base_path = temp_path
else
base_path = input_path
end
commands << build_alpha_command(base_path, output_path)
commands << "rm -f #{temp_path.shellescape}" unless base_path == input_path
commands.join(" && ")
end
|
#build_composite_command(base_path, output_path) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 109
def build_composite_command(base_path, output_path)
wm_op = @operations.find { |op| op[:type] == :watermark }
watermark_path = wm_op[:watermark_path]
position = wm_op[:options][:position]
opacity = wm_op[:options][:opacity]
format_spec = build_format_spec(output_path)
if opacity && opacity < 1.0
wm_temp = watermark_path.gsub(/\.[^.]+$/, ".tmp_wm.png")
alpha_value = opacity
temp_cmd = ["vips", "linear", watermark_path.shellescape,
"'#{wm_temp}[alpha]'", "\"1 1 1 #{alpha_value}\"", "0"].join(" ")
full_cmd = ["#{temp_cmd} && vips composite2",
base_path.shellescape, wm_temp.shellescape,
format_spec, "over",
position_to_vips_xy(position, base_path, watermark_path)].join(" ")
"#{full_cmd} && rm -f #{wm_temp.shellescape}"
else
["vips", "composite2", base_path.shellescape,
watermark_path.shellescape, format_spec, "over",
position_to_vips_xy(position, base_path, watermark_path)].join(" ")
end
end
|
#build_copy_command(input_path, output_path) ⇒ Object
243
244
245
246
247
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 243
def build_copy_command(input_path, output_path)
format_spec = build_format_spec(output_path)
["vips", "copy", input_path.shellescape, format_spec].join(" ")
end
|
#build_crop_command(input_path, output_path) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 203
def build_crop_command(input_path, output_path)
crop_op = @operations.find { |op| op[:type] == :crop }
opts = crop_op[:options] || {}
params = crop_op[:params] || {}
keep = opts[:keep] || params[:keep] || params[:position]
if crop_op[:ratio] && keep && %w[attention entropy center
centre].include?(keep.to_s)
crop_width = opts[:calculated_width]
crop_height = opts[:calculated_height]
interestingness = case keep.to_s
when "entropy" then "entropy"
when "center", "centre" then "centre"
else "attention" end
["vips", "smartcrop", input_path.shellescape, output_path.shellescape,
crop_width.to_s, crop_height.to_s,
"--interesting=#{interestingness}"].join(" ")
else
crop_x = crop_op[:ratio] ? opts[:calculated_x] : (opts[:x] || 0)
crop_y = crop_op[:ratio] ? opts[:calculated_y] : (opts[:y] || 0)
crop_width = crop_op[:ratio] ? opts[:calculated_width] : opts[:width]
crop_height = crop_op[:ratio] ? opts[:calculated_height] : opts[:height]
["vips", "extract_area", input_path.shellescape, output_path.shellescape,
crop_x.to_s, crop_y.to_s, crop_width.to_s, crop_height.to_s].join(" ")
end
end
|
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 249
def build_format_spec(output_path)
format_op = @operations.find { |op| op[:type] == :format }
quality_op = @operations.find { |op| op[:type] == :quality }
return output_path.shellescape unless format_op || quality_op
ext = format_op ? format_op[:format] : File.extname(output_path).delete(".")
base = output_path.gsub(/\.[^.]+$/, "")
if quality_op
quality = quality_op[:quality]
"'#{base}.#{ext}[Q=#{quality}]'"
else
"'#{base}.#{ext}'"
end
end
|
#build_resize_command(input_path, output_path) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 175
def build_resize_command(input_path, output_path)
resize_op = @operations.find { |op| op[:type] == :resize }
resize_data = resize_op[:options] || {}
if resize_op[:width] && resize_op[:height]
scale_x = resize_data[:scale_x]
scale_y = resize_data[:scale_y]
format_spec = build_format_spec(output_path)
if scale_x && scale_y
["vips", "VipsResize", input_path.shellescape, format_spec,
scale_x.to_s, "--vscale=#{scale_y}"].join(" ")
else
["vips", "VipsResize", input_path.shellescape, format_spec,
(scale_x || 1.0).to_s].join(" ")
end
else
scale_x = resize_data[:scale_x] || 1.0
format_spec = build_format_spec(output_path)
["vips", "VipsResize", input_path.shellescape, format_spec, scale_x.to_s].join(" ")
end
end
|
#build_sequential_crop_resize(input_path, output_path) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 161
def build_sequential_crop_resize(input_path, output_path)
temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_crop.jpg")
crop_cmd = build_crop_command(input_path, temp_path)
resize_cmd = build_resize_command(temp_path, output_path)
"#{crop_cmd} && #{resize_cmd} && rm -f #{temp_path.shellescape}"
end
|
#build_vips_command(input_path, output_path) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 28
def build_vips_command(input_path, output_path)
has_crop = @operations.any? { |op| op[:type] == :crop }
has_resize = @operations.any? { |op| op[:type] == :resize }
has_watermark = @operations.any? { |op| op[:type] == :watermark }
has_alpha = @operations.any? { |op| op[:type] == :alpha_opacity }
if has_watermark
build_watermark_pipeline(input_path, output_path, has_crop, has_resize)
elsif has_alpha
build_alpha_pipeline(input_path, output_path, has_crop, has_resize)
elsif has_crop && has_resize
build_sequential_crop_resize(input_path, output_path)
elsif has_resize
build_resize_command(input_path, output_path)
elsif has_crop
build_crop_command(input_path, output_path)
else
build_copy_command(input_path, output_path)
end
end
|
#build_watermark_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
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
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 71
def build_watermark_pipeline(input_path, output_path, has_crop, has_resize)
temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_base.jpg")
final_path = output_path
commands = []
if has_crop && has_resize
commands << build_sequential_crop_resize(input_path, temp_path)
base_path = temp_path
elsif has_resize
commands << build_resize_command(input_path, temp_path)
base_path = temp_path
elsif has_crop
commands << build_crop_command(input_path, temp_path)
base_path = temp_path
else
base_path = input_path
end
alpha_op = @operations.find { |op| op[:type] == :alpha_opacity }
if alpha_op
alpha_temp = input_path.gsub(/\.[^.]+$/, ".tmp_alpha.jpg")
commands << build_alpha_command(base_path, alpha_temp)
base_path = alpha_temp
end
commands << build_composite_command(base_path, final_path)
temp_files = [temp_path, base_path == input_path ? nil : base_path].compact
temp_files.each { |f| commands << "rm -f #{f.shellescape}" }
commands.join(" && ")
end
|
#execute(input_path, output_path) ⇒ Object
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 17
def execute(input_path, output_path)
return if @operations.empty?
command = build_vips_command(input_path, output_path)
execute_command(command)
reset_operations
output_path
end
|
#execute_command(command) ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 271
def execute_command(command)
Jekyll.logger.debug "LibVips command: #{command}"
stdout, stderr, status = Open3.capture3(command)
output = "#{stdout}#{stderr}"
success = status.success?
unless success
raise "LibVips command failed: #{command}\nError: #{output}" unless output.include?("same file") || output.include?("VipsForeignSave")
Jekyll.logger.warn "LibVips warning: #{output.strip}"
return output.strip
end
output.strip
end
|
#position_to_vips_xy(position, _base_path, watermark_path) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 134
def position_to_vips_xy(position, _base_path, watermark_path)
case position.to_s
when "northeast", "top-right"
"--x $(vips header #{watermark_path.shellescape} width) --y 0"
when "southwest", "bottom-left"
"--x 0 --y $(vips header #{watermark_path.shellescape} height)"
when "southeast", "bottom-right"
"--x $(vips header #{watermark_path.shellescape} width) " \
"--y $(vips header #{watermark_path.shellescape} height)"
else
"--x 0 --y 0" end
end
|