Class: JekyllImgFlow::Providers::Flyimg
- Inherits:
-
BaseProvider
- Object
- BaseProvider
- JekyllImgFlow::Providers::Flyimg
- 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
Instance Method Summary collapse
- #available? ⇒ Boolean
- #build_combined_flyimg_url(input_path) ⇒ Object
- #execute(input_path, output_path) ⇒ Object
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
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) = [] # 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). << "w_#{operation[:width]}" << "pns_0" if operation[:height] << "h_#{operation[:height]}" << "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] << "w_#{crop_width}" << "h_#{crop_height}" << "c_1" << "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 << "e_1" << "p1x_#{crop_x}" << "p1y_#{crop_y}" << "p2x_#{crop_x + crop_width}" << "p2y_#{crop_y + crop_height}" end when :quality flyimg_quality = translate_quality_to_flyimg(operation[:quality]) << "q_#{flyimg_quality}" when :format # Assume validated input from tags << "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 << "wm_#{encoded_watermark}_#{position_param}_#{opacity_param}" when :alpha_opacity opacity = operation[:opacity] << "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 << "o_#{map_format(input_ext)}" unless input_ext.empty? end # Build combined URL: base/upload/options/options,.../encoded_url = .join(",") "#{@config.flyimg_url}/upload/#{}/#{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 |