Class: JekyllImgFlow::Providers::Flyimg

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

Overview

Flyimg 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/flyimg.rb', line 14

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

  check_http_service(@config.flyimg_url)
end

#build_combined_flyimg_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
119
120
121
122
123
124
125
126
# File 'lib/jekyll-imgflow/providers/flyimg.rb', line 33

def build_combined_flyimg_url(input_path)
  raise "flyimg_url not set" unless @config.flyimg_url

  encoded_url = encode_file_url(input_path)
  options = []

  # Add all operations to single options string
  @operations.each do |operation|
    case operation[:type]
    when :resize
      # Tags now provide complete calculated values.
      # c_1 forces the image to fill the exact width x height area.
      # pns_0 allows upscaling when the source is smaller than the target
      # (Flyimg's pns/preserve-natural-size defaults to 1, blocking enlarge).
      options << "w_#{operation[:width]}"
      options << "pns_0"
      if operation[:height]
        options << "h_#{operation[:height]}"
        options << "c_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 (Flyimg's smc option picks the best crop area)
        crop_width = opts[:calculated_width]
        crop_height = opts[:calculated_height]

        options << "w_#{crop_width}"
        options << "h_#{crop_height}"
        options << "c_1"
        options << "smc_1"
      else
        # Use basic cropping via extract (top-left / bottom-right 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
        options << "e_1"
        options << "p1x_#{crop_x}"
        options << "p1y_#{crop_y}"
        options << "p2x_#{crop_x + crop_width}"
        options << "p2y_#{crop_y + crop_height}"
      end

    when :quality
      flyimg_quality = translate_quality_to_flyimg(operation[:quality])
      options << "q_#{flyimg_quality}"

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

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

      # Flyimg watermark parameters
      encoded_watermark = encode_file_url(watermark_path)
      position_param = translate_flyimg_position(position)
      opacity_param = (opacity * 100).round
      options << "wm_#{encoded_watermark}_#{position_param}_#{opacity_param}"

    when :alpha_opacity
      opacity = operation[:opacity]
      options << "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
    options << "o_#{map_format(input_ext)}" unless input_ext.empty?
  end

  # Build combined URL: base/upload/options/options,.../encoded_url
  options_string = options.join(",")
  "#{@config.flyimg_url}/upload/#{options_string}/#{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/flyimg.rb', line 21

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

  # Build single Flyimg URL with all operations combined
  url = build_combined_flyimg_url(input_path)

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