Class: JekyllImgFlow::Providers::Libvips
Overview
Libvips provider implementation using the standardized tag interface
Constant Summary
Constants inherited
from BaseProvider
BaseProvider::KNOWN_OPERATIONS, BaseProvider::SMARTCROP_POSITIONS
Instance Attribute Summary
Attributes inherited from BaseProvider
#config
Instance Method Summary
collapse
-
#apply_watermark_alpha(base_path, input_path, commands) ⇒ Object
-
#available? ⇒ Boolean
-
#build_alpha_command(input_path, output_path) ⇒ Object
-
#build_alpha_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
-
#build_base_pipeline(input_path, temp_path, has_crop, has_resize) ⇒ Object
-
#build_composite_commands(base_path, output_path) ⇒ Object
-
#build_copy_command(input_path, output_path) ⇒ Object
-
#build_crop_command(input_path, output_path) ⇒ Object
-
#build_extract_command(crop_op, opts, input_path, output_path) ⇒ Object
-
#build_format_spec(output_path) ⇒ Object
Build vips output specification with format and quality.
-
#build_resize_command(input_path, output_path) ⇒ Object
-
#build_sequential_crop_resize(input_path, output_path) ⇒ Object
-
#build_smartcrop_command(_crop_op, opts, keep, input_path, output_path) ⇒ Object
-
#build_svg_crop_pipeline(input_path, output_path) ⇒ Object
SVG crop: use vips thumbnail with --crop to crop during thumbnailing.
-
#build_vips_command(input_path, output_path) ⇒ Object
Convenience wrapper for backward compatibility with tests.
-
#build_vips_commands(input_path, output_path) ⇒ Object
Build all vips commands as an array of command arrays.
-
#build_watermark_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
-
#cleanup_paths(commands, input_path, temp_path, base_path) ⇒ Object
-
#crop_commands(input_path, output_path) ⇒ Object
-
#execute(input_path, output_path) ⇒ Object
-
#execute_command(command) ⇒ Object
Execute a command array via Open3.capture3 (no shell invocation).
-
#operation_flags ⇒ Object
-
#position_to_vips_xy(position, _base_path, watermark_path) ⇒ Object
Return x/y arguments as an array of strings (no shell substitution).
-
#select_operation_builder(flags) ⇒ Object
-
#smartcrop?(crop_op, keep) ⇒ Boolean
-
#vips_image_dimension(image_path, field) ⇒ Object
Get an image dimension (width or height) using vips header.
#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
#apply_watermark_alpha(base_path, input_path, commands) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 99
def apply_watermark_alpha(base_path, input_path, commands)
alpha_op = @operations.find { |op| op[:type] == :alpha_opacity }
return [base_path, commands] unless alpha_op
alpha_temp = input_path.gsub(/\.[^.]+$/, ".tmp_alpha.jpg")
[alpha_temp, commands << build_alpha_command(base_path, alpha_temp)]
end
|
#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
170
171
172
173
174
175
176
177
178
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 170
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, output_path,
"1 1 1 #{alpha_value}", "0"]
end
|
#build_alpha_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 74
def build_alpha_pipeline(input_path, output_path, has_crop, has_resize)
temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_base.jpg")
commands, base_path = build_base_pipeline(input_path, temp_path, has_crop, has_resize)
commands << build_alpha_command(base_path, output_path)
commands << [:cleanup, temp_path] unless base_path == input_path
commands
end
|
#build_base_pipeline(input_path, temp_path, has_crop, has_resize) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 91
def build_base_pipeline(input_path, temp_path, has_crop, has_resize)
return [build_sequential_crop_resize(input_path, temp_path), temp_path] if has_crop && has_resize
return [[build_resize_command(input_path, temp_path)], temp_path] if has_resize
return [[build_crop_command(input_path, temp_path)], temp_path] if has_crop
[[], input_path]
end
|
#build_composite_commands(base_path, output_path) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 112
def build_composite_commands(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]
if opacity && opacity < 1.0
wm_temp = watermark_path.gsub(/\.[^.]+$/, ".tmp_wm.png")
alpha_value = opacity
alpha_cmd = ["vips", "linear", watermark_path,
"#{wm_temp}[alpha]", "1 1 1 #{alpha_value}", "0"]
xy_args = position_to_vips_xy(position, base_path, watermark_path)
composite_cmd = ["vips", "composite2", base_path, wm_temp,
build_format_spec(output_path), "over"] + xy_args
[alpha_cmd, composite_cmd, [:cleanup, wm_temp]]
else
xy_args = position_to_vips_xy(position, base_path, watermark_path)
[["vips", "composite2", base_path, watermark_path,
build_format_spec(output_path), "over"] + xy_args]
end
end
|
#build_copy_command(input_path, output_path) ⇒ Object
275
276
277
278
279
280
281
282
283
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 275
def build_copy_command(input_path, output_path)
format_spec = build_format_spec(output_path)
return ["vips", "thumbnail", input_path, format_spec, "2000"] if svg?(input_path)
["vips", "copy", input_path, format_spec]
end
|
#build_crop_command(input_path, output_path) ⇒ Object
218
219
220
221
222
223
224
225
226
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 218
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]
return build_smartcrop_command(crop_op, opts, keep, input_path, output_path) if smartcrop?(crop_op, keep)
build_extract_command(crop_op, opts, input_path, output_path)
end
|
#build_extract_command(crop_op, opts, input_path, output_path) ⇒ Object
240
241
242
243
244
245
246
247
248
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 240
def build_extract_command(crop_op, opts, input_path, output_path)
ratio = crop_op[:ratio]
x = ratio ? opts[:calculated_x] : (opts[:x] || 0)
y = ratio ? opts[:calculated_y] : (opts[:y] || 0)
width = ratio ? opts[:calculated_width] : opts[:width]
height = ratio ? opts[:calculated_height] : opts[:height]
["vips", "extract_area", input_path, output_path,
x.to_s, y.to_s, width.to_s, height.to_s]
end
|
Build vips output specification with format and quality.
Returns a raw string — no shell quoting needed since commands
are executed via Open3.capture3 array form (no shell).
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 288
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 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 197
def build_resize_command(input_path, output_path)
resize_op = @operations.find { |op| op[:type] == :resize }
format_spec = build_format_spec(output_path)
width = resize_op[:width]
height = resize_op[:height]
if width && height
["vips", "thumbnail", input_path, format_spec,
width.to_s, "--height=#{height}", "--crop=attention"]
else
["vips", "thumbnail", input_path, format_spec, width.to_s]
end
end
|
#build_sequential_crop_resize(input_path, output_path) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 180
def build_sequential_crop_resize(input_path, output_path)
if svg?(input_path)
resize_op = @operations.find { |op| op[:type] == :resize }
format_spec = build_format_spec(output_path)
width = resize_op[:width]
height = resize_op[:height]
[["vips", "thumbnail", input_path, format_spec,
width.to_s, "--height=#{height}", "--crop=attention"]]
else
temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_crop.jpg")
[build_crop_command(input_path, temp_path),
build_resize_command(temp_path, output_path),
[:cleanup, temp_path]]
end
end
|
#build_smartcrop_command(_crop_op, opts, keep, input_path, output_path) ⇒ Object
232
233
234
235
236
237
238
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 232
def build_smartcrop_command(_crop_op, opts, keep, input_path, output_path)
interestingness = %w[center centre].include?(keep.to_s) ? "centre" : keep.to_s
interestingness = "attention" unless %w[entropy centre].include?(interestingness)
["vips", "smartcrop", input_path, output_path,
opts[:calculated_width].to_s, opts[:calculated_height].to_s,
"--interesting=#{interestingness}"]
end
|
#build_svg_crop_pipeline(input_path, output_path) ⇒ Object
SVG crop: use vips thumbnail with --crop to crop during thumbnailing.
This avoids the coordinate mismatch that occurs when rasterizing at a
different size than the SVG's viewBox (which caused "bad extract area").
thumbnail --crop uses smartcrop (attention/entropy/centre) to select
the most interesting region — this is the correct behavior for SVGs
since they have no fixed pixel dimensions.
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 256
def build_svg_crop_pipeline(input_path, output_path)
crop_op = @operations.find { |op| op[:type] == :crop }
opts = crop_op[:options] || {}
crop_width = crop_op[:ratio] ? opts[:calculated_width] : (opts[:width] || 1000)
crop_height = crop_op[:ratio] ? opts[:calculated_height] : (opts[:height] || 1000)
format_spec = build_format_spec(output_path)
keep = opts[:keep] || crop_op[:params]&.[](:keep) || crop_op[:params]&.[](:position)
interestingness = case keep.to_s
when "entropy" then "entropy"
when "center", "centre" then "centre"
else "attention"
end
[["vips", "thumbnail", input_path, format_spec,
crop_width.to_s, "--height=#{crop_height}",
"--crop=#{interestingness}"]]
end
|
#build_vips_command(input_path, output_path) ⇒ Object
Convenience wrapper for backward compatibility with tests.
Returns the commands joined as a debug string.
64
65
66
67
68
69
70
71
72
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 64
def build_vips_command(input_path, output_path)
build_vips_commands(input_path, output_path).map do |cmd|
if cmd.is_a?(Array) && cmd.first == :cleanup
"rm -f #{cmd[1]}"
else
cmd.join(" ")
end
end.join(" && ")
end
|
#build_vips_commands(input_path, output_path) ⇒ Object
Build all vips commands as an array of command arrays.
Each sub-array is passed directly to Open3.capture3 (no shell).
Returns Array[Array[String]].
33
34
35
36
37
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 33
def build_vips_commands(input_path, output_path)
flags = operation_flags
operation_builder = select_operation_builder(flags)
operation_builder.call(input_path, output_path)
end
|
#build_watermark_pipeline(input_path, output_path, has_crop, has_resize) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 82
def build_watermark_pipeline(input_path, output_path, has_crop, has_resize)
temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_base.jpg")
commands, base_path = build_base_pipeline(input_path, temp_path, has_crop, has_resize)
base_path, commands = apply_watermark_alpha(base_path, input_path, commands)
commands.concat(build_composite_commands(base_path, output_path))
cleanup_paths(commands, input_path, temp_path, base_path)
commands
end
|
#cleanup_paths(commands, input_path, temp_path, base_path) ⇒ Object
107
108
109
110
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 107
def cleanup_paths(commands, input_path, temp_path, base_path)
temp_files = [temp_path, base_path == input_path ? nil : base_path].compact
temp_files.each { |file| commands << [:cleanup, file] }
end
|
#crop_commands(input_path, output_path) ⇒ Object
58
59
60
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 58
def crop_commands(input_path, output_path)
svg?(input_path) ? build_svg_crop_pipeline(input_path, output_path) : [build_crop_command(input_path, output_path)]
end
|
#execute(input_path, output_path) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 17
def execute(input_path, output_path)
return if @operations.empty?
commands = build_vips_commands(input_path, output_path)
commands.each { |cmd_array| execute_command(cmd_array) }
output_path
ensure
reset_operations
end
|
#execute_command(command) ⇒ Object
Execute a command array via Open3.capture3 (no shell invocation).
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 311
def execute_command(command)
if command.is_a?(Array) && command.first == :cleanup
FileUtils.rm_f(command[1])
return
end
Jekyll.logger.debug "LibVips command: #{command.join(' ')}"
stdout, stderr, status = Open3.capture3(*command)
output = "#{stdout}#{stderr}"
success = status.success?
unless success
cmd_str = command.join(" ")
raise "LibVips command failed: #{cmd_str}\nError: #{output}" unless output.include?("same file") || output.include?("VipsForeignSave")
Jekyll.logger.warn "LibVips warning: #{output.strip}"
return output.strip
end
output.strip
end
|
#operation_flags ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 49
def operation_flags
{
crop: @operations.any? { |op| op[:type] == :crop },
resize: @operations.any? { |op| op[:type] == :resize },
watermark: @operations.any? { |op| op[:type] == :watermark },
alpha: @operations.any? { |op| op[:type] == :alpha_opacity }
}
end
|
#position_to_vips_xy(position, _base_path, watermark_path) ⇒ Object
Return x/y arguments as an array of strings (no shell substitution).
Pre-computes image dimensions via vips header in Ruby.
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 139
def position_to_vips_xy(position, _base_path, watermark_path)
case position.to_s
when "northeast", "top-right"
width = vips_image_dimension(watermark_path, "width")
["--x", width.to_s, "--y", "0"]
when "southwest", "bottom-left"
height = vips_image_dimension(watermark_path, "height")
["--x", "0", "--y", height.to_s]
when "southeast", "bottom-right"
width = vips_image_dimension(watermark_path, "width")
height = vips_image_dimension(watermark_path, "height")
["--x", width.to_s, "--y", height.to_s]
else
["--x", "0", "--y", "0"] end
end
|
#select_operation_builder(flags) ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 39
def select_operation_builder(flags)
return ->(input, output) { build_watermark_pipeline(input, output, flags[:crop], flags[:resize]) } if flags[:watermark]
return ->(input, output) { build_alpha_pipeline(input, output, flags[:crop], flags[:resize]) } if flags[:alpha]
return ->(input, output) { build_sequential_crop_resize(input, output) } if flags[:crop] && flags[:resize]
return ->(input, output) { [build_resize_command(input, output)] } if flags[:resize]
return ->(input, output) { crop_commands(input, output) } if flags[:crop]
->(input, output) { [build_copy_command(input, output)] }
end
|
#smartcrop?(crop_op, keep) ⇒ Boolean
228
229
230
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 228
def smartcrop?(crop_op, keep)
crop_op[:ratio] && keep && %w[attention entropy center centre].include?(keep.to_s)
end
|
#vips_image_dimension(image_path, field) ⇒ Object
Get an image dimension (width or height) using vips header.
Returns integer or 0 if vips is unavailable.
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 158
def vips_image_dimension(image_path, field)
return 0 unless %w[width height].include?(field)
stdout, _, status = Open3.capture3("vips", "header", image_path, field)
return 0 unless status.success?
stdout.strip.to_i
rescue StandardError
0
end
|