Class: JekyllImgFlow::Providers::Libvips
- Inherits:
-
CliBase
show all
- Defined in:
- lib/jekyll-imgflow/providers/libvips.rb
Overview
Libvips provider implementation using the standardized tag interface
Constant Summary
Constants inherited
from BaseProvider
BaseProvider::COMPASS_TO_SHORT, 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_commands(input_path, output_path) ⇒ 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(geo, 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(geo, 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_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).
-
#run_command(cmd) ⇒ Object
-
#select_operation_builder(flags) ⇒ Object
-
#vips_image_dimension(image_path, field) ⇒ Object
Get an image dimension (width or height) using vips header.
Methods inherited from CliBase
#execute
#add_watermark, #alpha_byte_value, #alpha_opacity=, #check_http_service, #cli_available?, #compass_to_short, #convert_format, #crop, #crop_coords, #crop_geometry, #encode_file_url, #execute, #find_op, #format, #initialize, #input_format_ext, #op?, #opacity, #operations, #optimize, provider_name, #quality, #quality=, #relative_file_path, #reset_operations, #resize, #smartcrop_interestingness, #supports_operation?, supports_operation?, #temp_path, unsupported_operations, #watermark, #watermark_parts
Instance Method Details
#apply_watermark_alpha(base_path, input_path, commands) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 92
def apply_watermark_alpha(base_path, input_path, commands)
alpha_op = find_op(:alpha_opacity)
return [base_path, commands] unless alpha_op
alpha_temp = temp_path(input_path, "tmp_alpha.jpg")
[alpha_temp, commands << build_alpha_command(base_path, alpha_temp)]
end
|
#available? ⇒ Boolean
11
12
13
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 11
def available?
cli_available?("vips")
end
|
#build_alpha_command(input_path, output_path) ⇒ Object
161
162
163
164
165
166
167
168
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 161
def build_alpha_command(input_path, output_path)
alpha_op = find_op(:alpha_opacity)
alpha_value = alpha_op[: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
67
68
69
70
71
72
73
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 67
def build_alpha_pipeline(input_path, output_path, has_crop, has_resize)
temp = temp_path(input_path, "tmp_base.jpg")
commands, base_path = build_base_pipeline(input_path, temp, has_crop, has_resize)
commands << build_alpha_command(base_path, output_path)
commands << [:cleanup, temp] unless base_path == input_path
commands
end
|
#build_base_pipeline(input_path, temp_path, has_crop, has_resize) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 84
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_commands(input_path, output_path) ⇒ Object
15
16
17
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 15
def build_commands(input_path, output_path)
build_vips_commands(input_path, output_path)
end
|
#build_composite_commands(base_path, output_path) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 105
def build_composite_commands(base_path, output_path)
wm_op = find_op(:watermark)
parts = watermark_parts(wm_op)
if parts[:opacity] && parts[:opacity] < 1.0
wm_temp = temp_path(parts[:watermark_path], "tmp_wm.png")
alpha_value = parts[:opacity]
alpha_cmd = ["vips", "linear", parts[:watermark_path],
"#{wm_temp}[alpha]", "1 1 1 #{alpha_value}", "0"]
xy_args = position_to_vips_xy(parts[:position], base_path, parts[: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(parts[:position], base_path, parts[:watermark_path])
[["vips", "composite2", base_path, parts[:watermark_path],
build_format_spec(output_path), "over"] + xy_args]
end
end
|
#build_copy_command(input_path, output_path) ⇒ Object
247
248
249
250
251
252
253
254
255
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 247
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
208
209
210
211
212
213
214
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 208
def build_crop_command(input_path, output_path)
crop_op = find_op(:crop)
geo = crop_geometry(crop_op)
return build_smartcrop_command(geo, input_path, output_path) if geo[:smartcrop]
build_extract_command(geo, input_path, output_path)
end
|
#build_extract_command(geo, input_path, output_path) ⇒ Object
223
224
225
226
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 223
def build_extract_command(geo, input_path, output_path)
["vips", "extract_area", input_path, output_path,
geo[:x].to_s, geo[:y].to_s, geo[:width].to_s, geo[: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).
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 260
def build_format_spec(output_path)
format_op = find_op(:format)
quality_op = find_op(: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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 187
def build_resize_command(input_path, output_path)
resize_op = find_op(: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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 170
def build_sequential_crop_resize(input_path, output_path)
if svg?(input_path)
resize_op = find_op(: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 = temp_path(input_path, "tmp_crop.jpg")
[build_crop_command(input_path, temp),
build_resize_command(temp, output_path),
[:cleanup, temp]]
end
end
|
#build_smartcrop_command(geo, input_path, output_path) ⇒ Object
216
217
218
219
220
221
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 216
def build_smartcrop_command(geo, input_path, output_path)
interestingness = smartcrop_interestingness(geo[:keep])
["vips", "smartcrop", input_path, output_path,
geo[:width].to_s, geo[: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.
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 234
def build_svg_crop_pipeline(input_path, output_path)
crop_op = find_op(:crop)
geo = crop_geometry(crop_op)
crop_width = geo[:width] || 1000
crop_height = geo[:height] || 1000
format_spec = build_format_spec(output_path)
interestingness = smartcrop_interestingness(geo[:keep])
[["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.
57
58
59
60
61
62
63
64
65
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 57
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]].
26
27
28
29
30
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 26
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
75
76
77
78
79
80
81
82
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 75
def build_watermark_pipeline(input_path, output_path, has_crop, has_resize)
temp = temp_path(input_path, "tmp_base.jpg")
commands, base_path = build_base_pipeline(input_path, temp, 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, base_path)
commands
end
|
#cleanup_paths(commands, input_path, temp_path, base_path) ⇒ Object
100
101
102
103
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 100
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
51
52
53
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 51
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_command(command) ⇒ Object
Execute a command array via Open3.capture3 (no shell invocation).
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 283
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
42
43
44
45
46
47
48
49
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 42
def operation_flags
{
crop: op?(:crop),
resize: op?(:resize),
watermark: op?(:watermark),
alpha: op?(: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.
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 130
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
|
#run_command(cmd) ⇒ Object
19
20
21
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 19
def run_command(cmd)
execute_command(cmd)
end
|
#select_operation_builder(flags) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 32
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
|
#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.
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/jekyll-imgflow/providers/libvips.rb', line 149
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
|