Class: JekyllImgFlow::Providers::Imgproxy

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

Overview

Imgproxy provider implementation using the standardized tag interface

Constant Summary collapse

TIMEOUT =

seconds

10

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)


14
15
16
17
18
19
# File 'lib/jekyll-imgflow/providers/imgproxy.rb', line 14

def available?
  # Check if imgproxy service is running
  return false unless @config.respond_to?(:imgproxy_url) && @config.imgproxy_url

  check_http_service(@config.imgproxy_url)
end

#build_combined_imgproxy_url(input_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jekyll-imgflow/providers/imgproxy.rb', line 33

def build_combined_imgproxy_url(input_path)
  raise "imgproxy_url not set" unless @config.imgproxy_url

  encoded_url = encode_file_url(input_path)
  operations = []

  # Add all operations to single URL path
  @operations.each do |operation|
    case operation[:type]
    when :resize
      # Tags now provide complete calculated values
      operations << if operation[:height]
                      "rs:fill:#{operation[:width]}:#{operation[:height]}:1"
                    else
                      "rs:fit:#{operation[:width]}::1"
                    end

    when :crop
      opts = operation[:options]
      params = operation[:params] || {}

      # Check for keep parameter (smartcrop support)
      keep = opts[:keep] || params[:keep] || params[:position]

      if operation[:ratio] && keep && SMARTCROP_POSITIONS.include?(keep.to_s)
        # Use smartcrop with gravity:sm (libvips smart crop)
        crop_width = opts[:calculated_width]
        crop_height = opts[:calculated_height]

        operations << "g:sm"
        operations << "c:#{crop_width}:#{crop_height}"
      else
        # Use basic cropping with explicit coordinates
        if operation[:ratio]
          crop_x = opts[:calculated_x]
          crop_y = opts[:calculated_y]
          crop_width = opts[:calculated_width]
          crop_height = opts[:calculated_height]
        else
          crop_x = opts[:x]
          crop_y = opts[:y]
          crop_width = opts[:width]
          crop_height = opts[:height]
        end
        # imgproxy crop format: c:width:height:gravity
        # Use nowe (north-west) gravity with x/y offsets for pixel-precise cropping
        operations << "g:nowe:#{crop_x}:#{crop_y}"
        operations << "c:#{crop_width}:#{crop_height}"
      end

    when :quality
      imgproxy_quality = translate_quality_to_imgproxy(operation[:quality])
      operations << "q:#{imgproxy_quality}"

    when :format
      # Assume validated input from tags
      operations << "f:#{operation[:format]}"

    when :watermark
      watermark_path = operation[:watermark_path]
      position = operation[:options][:position]
      operation[:options][:opacity]

      # Imgproxy watermark parameters
      encoded_watermark = encode_file_url(watermark_path)
      position_param = translate_imgproxy_position(position)
      operations << "wm:#{encoded_watermark}:#{position_param}:10:10"

    when :alpha_opacity
      opacity = operation[:opacity]
      operations << "a:#{(opacity * 255).round}"
    end
  end

  # Preserve format when no explicit format operation is requested.
  # Without this, chained HTTP requests (e.g. quality-only steps after a
  # prior format conversion) would lose the previously converted format.
  unless @operations.any? { |op| op[:type] == :format }
    input_ext = File.extname(input_path).delete(".").downcase
    operations << "f:#{input_ext}" unless input_ext.empty?
  end

  # Build combined URL: base/insecure/operations/operations/.../plain/encoded_url
  operations_path = operations.join("/")
  "#{@config.imgproxy_url}/insecure/#{operations_path}/plain/#{encoded_url}"
end

#execute(input_path, output_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll-imgflow/providers/imgproxy.rb', line 21

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

  # Build single Imgproxy URL with all operations combined
  url = build_combined_imgproxy_url(input_path)

  # Fetch the result in one request
  fetch_and_save(url, output_path)
  reset_operations
  output_path
end