Class: JekyllImgFlow::Providers::Sharp

Inherits:
BaseProvider show all
Defined in:
lib/jekyll-imgflow/providers/sharp.rb

Overview

Sharp 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

Methods inherited from BaseProvider

#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

Constructor Details

This class inherits a constructor from JekyllImgFlow::Providers::BaseProvider

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 11

def available?
  # Check if sharp CLI is available
  _, _, status = Open3.capture3("which", "sharp")
  status.success?
end

#build_composite_command(base_path, output_path) ⇒ Object



68
69
70
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
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 68

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]

  gravity = translate_position(position)

  command_parts = ["sharp", "-i", base_path.shellescape,
                   "-o", output_path.shellescape]

  format_op = @operations.find { |op| op[:type] == :format }
  quality_op = @operations.find { |op| op[:type] == :quality }
  command_parts += ["-f", format_op[:format]] if format_op
  command_parts += ["-q#{quality_op[:quality]}"] if quality_op

  if opacity && opacity < 1.0
    wm_temp = watermark_path.gsub(/\.[^.]+$/, ".tmp_wm.png")
    alpha_value = (opacity * 255).round
    temp_cmd = ["sharp", "-i", watermark_path.shellescape,
                "-o", wm_temp.shellescape,
                "ensureAlpha", alpha_value.to_s].join(" ")
    command_parts += ["composite", wm_temp.shellescape,
                      "--gravity", gravity, "--blend", "over"]
    "#{temp_cmd} && #{command_parts.join(' ')} " \
      "&& rm -f #{wm_temp.shellescape}"
  else
    command_parts += ["composite", watermark_path.shellescape,
                      "--gravity", gravity, "--blend", "over"]
    command_parts.join(" ")
  end
end

#build_copy_command(input_path, output_path) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 194

def build_copy_command(input_path, output_path)
  # sharp -i input.jpg -o output.jpg -f format -q quality
  command_parts = ["sharp", "-i", input_path.shellescape, "-o", output_path.shellescape]

  format_op = @operations.find { |op| op[:type] == :format }
  quality_op = @operations.find { |op| op[:type] == :quality }
  alpha_op = @operations.find { |op| op[:type] == :alpha_opacity }

  command_parts += ["-f", format_op[:format]] if format_op
  command_parts += ["-q#{quality_op[:quality]}"] if quality_op
  if alpha_op
    alpha_value = (alpha_op[:opacity] * 255).round
    command_parts += ["alpha", "{alpha:#{alpha_value}}"]
  end

  command_parts.join(" ")
end

#build_crop_command(input_path, output_path) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 154

def build_crop_command(input_path, output_path)
  crop_op = @operations.find { |op| op[:type] == :crop }
  opts = crop_op[:options] || {}
  params = crop_op[:params] || {}

  # Check if we should use smartcrop (when keep parameter is specified)
  # JPT keep options: attention (default), entropy, center
  # Check both options (from CropTag) and params (from OperationProcessor)
  keep = opts[:keep] || params[:keep] || params[:position]

  if crop_op[:ratio] && keep && %w[attention entropy center
                                   centre].include?(keep.to_s)
    # Use smartcrop for intelligent cropping (Sharp uses libvips backend)
    crop_width = opts[:calculated_width]
    crop_height = opts[:calculated_height]

    # Map keep parameter to libvips interestingness
    interestingness = case keep.to_s
                      when "entropy" then "entropy"
                      when "center", "centre" then "centre"
                      else "attention" # default
                      end

    # sharp -i input.jpg -o output.jpg smartcrop width height --interesting=attention
    ["sharp", "-i", input_path.shellescape, "-o", output_path.shellescape,
     "smartcrop", crop_width.to_s, crop_height.to_s,
     "--interesting=#{interestingness}"].join(" ")
  else
    # Use basic extract for manual cropping or when no keep parameter
    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]

    # sharp -i input.jpg -o output.jpg extract top left width height
    ["sharp", "-i", input_path.shellescape, "-o", output_path.shellescape,
     "extract", crop_y.to_s, crop_x.to_s, crop_width.to_s, crop_height.to_s].join(" ")
  end
end

#build_resize_command(input_path, output_path) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 126

def build_resize_command(input_path, output_path)
  resize_op = @operations.find { |op| op[:type] == :resize }

  # sharp -i input.jpg -o output.jpg resize width [height] -f format -q quality
  command_parts = ["sharp", "-i", input_path.shellescape, "-o", output_path.shellescape]

  # Add resize
  command_parts += if resize_op[:height]
                     ["resize", resize_op[:width].to_s, resize_op[:height].to_s]
                   else
                     ["resize", resize_op[:width].to_s]
                   end

  # Add format, quality, alpha
  format_op = @operations.find { |op| op[:type] == :format }
  quality_op = @operations.find { |op| op[:type] == :quality }
  alpha_op = @operations.find { |op| op[:type] == :alpha_opacity }

  command_parts += ["-f", format_op[:format]] if format_op
  command_parts += ["-q#{quality_op[:quality]}"] if quality_op
  if alpha_op
    alpha_value = (alpha_op[:opacity] * 255).round
    command_parts += ["alpha", "{alpha:#{alpha_value}}"]
  end

  command_parts.join(" ")
end

#build_sequential_crop_resize(input_path, output_path) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 112

def build_sequential_crop_resize(input_path, output_path)
  # Build temp file path for intermediate crop result
  temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_crop.jpg")

  # First command: crop only
  crop_cmd = build_crop_command(input_path, temp_path)

  # Second command: resize + format + quality + alpha (all together)
  resize_cmd = build_resize_command(temp_path, output_path)

  # Combine with && and cleanup temp file
  "#{crop_cmd} && #{resize_cmd} && rm -f #{temp_path.shellescape}"
end

#build_sharp_command(input_path, output_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 28

def build_sharp_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 }

  if has_watermark
    build_watermark_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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 46

def build_watermark_pipeline(input_path, output_path, has_crop, has_resize)
  temp_path = input_path.gsub(/\.[^.]+$/, ".tmp_base.jpg")
  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

  commands << build_composite_command(base_path, output_path)
  commands << "rm -f #{temp_path.shellescape}" unless base_path == input_path
  commands.join(" && ")
end

#execute(input_path, output_path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 17

def execute(input_path, output_path)
  return if @operations.empty?

  # Build Sharp command
  command = build_sharp_command(input_path, output_path)
  execute_command(command)

  reset_operations
  output_path
end

#translate_position(position) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/jekyll-imgflow/providers/sharp.rb', line 101

def translate_position(position)
  case position.to_s
  when "northwest" then "northwest"
  when "northeast" then "northeast"
  when "southwest" then "southwest"
  when "southeast" then "southeast"
  when "center" then "center"
  else position.to_s
  end
end