Class: JekyllImgFlow::Providers::Weserv

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

Overview

Weserv provider implementation using the standardized tag interface

Constant Summary collapse

TIMEOUT =
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/weserv.rb', line 14

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

  check_http_service(@config.weserv_url)
end

#build_combined_weserv_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
# File 'lib/jekyll-imgflow/providers/weserv.rb', line 33

def build_combined_weserv_url(input_path)
  raise "weserv_url not set" unless @config.weserv_url

  encoded_url = encode_file_url(input_path)
  params = ["url=#{URI.encode_www_form_component(encoded_url)}"]

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

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

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

      if operation[:ratio] && keep && SMARTCROP_POSITIONS.include?(keep.to_s)
        # Use smartcrop (WeServ supports smartcrop)
        crop_width = opts[:calculated_width]
        crop_height = opts[:calculated_height]

        # Add smartcrop parameters according to WeServ docs
        params << "crop=#{crop_width}x#{crop_height}"
        params << "a=smart" # smart attention

        # Map keep parameter to WeServ attention type
        case keep.to_s
        when "attention" then params << "a=attention"
        when "entropy" then params << "a=entropy"
        when "center", "centre" then params << "a=center"
        end
      else
        # Use basic cropping
        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
        params << "cx=#{crop_x}&cy=#{crop_y}&cw=#{crop_width}&ch=#{crop_height}"
      end

    when :quality
      weserv_quality = translate_quality_to_weserv(operation[:quality])
      params << "q=#{weserv_quality}"

    when :format
      # Assume validated input from tags
      params << "output=#{operation[:format]}"

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

      # Weserv watermark parameters
      params << "wm=#{encode_file_url(watermark_path)}"
      params << "wmpos=#{translate_weserv_position(position)}"
      params << "wmo=#{opacity}" if opacity

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

  query_string = params.join("&")
  "#{@config.weserv_url}/?#{query_string}"
end

#execute(input_path, output_path) ⇒ Object



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

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

  # Build single Weserv URL with all operations combined
  url = build_combined_weserv_url(input_path)

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