Class: JekyllImgFlow::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-imgflow/config.rb

Constant Summary collapse

DEFAULT_ORIGINALS =

Sensible defaults — users only need to configure originals and output (or even nothing at all). All other values fall back to these.

"assets/images/originals"
DEFAULT_OUTPUT =
"assets/images/optimized"
DEFAULT_INPUT_FORMATS =
%w[jpg jpeg png webp avif gif tiff tif svg].freeze
DEFAULT_FORMATS =

Output format priority: avif > webp > png > jpg (fallback). The browser picks the first format it supports, so avif is served to modern browsers, webp as a fallback for avif, png for transparency, and jpg as the universal fallback.

%w[avif webp png jpg].freeze
DEFAULT_SIZES =
{ "sm" => 400, "md" => 800, "lg" => 1200, "xl" => 2000 }.freeze
DEFAULT_QUALITY =
85
DEFAULT_BACKEND_PRIORITY =

Ordered by speed (see docs/providers.md benchmark): sharp (14s) → libvips (22s) → imagemagick (31s) → imgproxy (31s) → weserv (30s) → flyimg

%w[sharp libvips imagemagick imgproxy weserv flyimg].freeze
DEFAULT_FALLBACK_FORMAT =

Format used for the fallback in elements. All other formats in formats get tags for browsers that support them.

"jpg"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Config

Returns a new instance of Config.



29
30
31
32
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
# File 'lib/jekyll-imgflow/config.rb', line 29

def initialize(site)
  shared = site.config["shared_images_configs"] || {}
  overrides = site.config["imgflow"] || {}
  cfg = shared.merge(overrides)

  @site             = site
  @originals        = cfg["originals"] || DEFAULT_ORIGINALS
  @output           = cfg["output"] || DEFAULT_OUTPUT
  @input_formats    = cfg["input_formats"] || DEFAULT_INPUT_FORMATS
  @sizes            = cfg["sizes"] || DEFAULT_SIZES
  @formats          = cfg["formats"] || DEFAULT_FORMATS
  @quality          = cfg["quality"] || DEFAULT_QUALITY
  @backend_priority = cfg["backend_priority"] || DEFAULT_BACKEND_PRIORITY
  @fallback_format  = cfg["fallback_format"] || DEFAULT_FALLBACK_FORMAT

  @imgproxy_url = cfg["imgproxy_url"]
  @image_compressor_url = cfg["image_compressor_url"]
  @weserv_url       = cfg["weserv_url"]
  @flyimg_url       = cfg["flyimg_url"]
  @optimize_qualities = cfg["optimize_qualities"] || {
    "low" => 30,
    "medium" => 50, # Uses default quality
    "high" => 85, # Uses default quality
    "maximum" => 95,
    "default" => 75
  }

  validate_backend_priority!
end

Instance Attribute Details

#backend_priorityObject (readonly)

Returns the value of attribute backend_priority.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def backend_priority
  @backend_priority
end

#fallback_formatObject (readonly)

Returns the value of attribute fallback_format.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def fallback_format
  @fallback_format
end

#flyimg_urlObject (readonly)

Returns the value of attribute flyimg_url.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def flyimg_url
  @flyimg_url
end

#formatsObject (readonly)

Returns the value of attribute formats.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def formats
  @formats
end

#image_compressor_urlObject (readonly)

Returns the value of attribute image_compressor_url.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def image_compressor_url
  @image_compressor_url
end

#imgproxy_urlObject (readonly)

Returns the value of attribute imgproxy_url.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def imgproxy_url
  @imgproxy_url
end

#input_formatsObject (readonly)

Returns the value of attribute input_formats.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def input_formats
  @input_formats
end

#optimize_qualitiesObject (readonly)

Returns the value of attribute optimize_qualities.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def optimize_qualities
  @optimize_qualities
end

#originalsObject (readonly)

Returns the value of attribute originals.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def originals
  @originals
end

#outputObject (readonly)

Returns the value of attribute output.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def output
  @output
end

#qualityObject (readonly)

Returns the value of attribute quality.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def quality
  @quality
end

#siteObject (readonly)

Returns the value of attribute site.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def site
  @site
end

#sizesObject (readonly)

Returns the value of attribute sizes.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def sizes
  @sizes
end

#weserv_urlObject (readonly)

Returns the value of attribute weserv_url.



24
25
26
# File 'lib/jekyll-imgflow/config.rb', line 24

def weserv_url
  @weserv_url
end

Instance Method Details

#cache_dirObject



59
60
61
# File 'lib/jekyll-imgflow/config.rb', line 59

def cache_dir
  merged_config["cache_dir"] || ".cache/imgflow"
end

#determine_version_type(params) ⇒ Symbol

Determine if params represent a default or specialized version

Parameters:

  • params (Hash)

    Operation parameters

Returns:

  • (Symbol)

    :default or :specialized



87
88
89
90
91
92
93
94
95
# File 'lib/jekyll-imgflow/config.rb', line 87

def determine_version_type(params)
  if params[:width] && @sizes.value?(params[:width]) &&
     (!params[:format] || @formats.include?(params[:format])) &&
     (!params[:quality] || params[:quality] == @quality)
    :default
  else
    :specialized
  end
end

#docker_configObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jekyll-imgflow/config.rb', line 63

def docker_config
  {
    enabled: merged_config.key?("imgproxy_url") ||
      merged_config.key?("weserv_url") ||
      merged_config.key?("flyimg_url") ||
      merged_config.key?("image_compressor_url"),
    imgproxy_url: @imgproxy_url,
    weserv_url: @weserv_url,
    flyimg_url: @flyimg_url,
    image_compressor_url: @image_compressor_url
  }
end

#docker_enabledObject



76
77
78
# File 'lib/jekyll-imgflow/config.rb', line 76

def docker_enabled
  docker_config[:enabled]
end

#normalized_input_formatsObject

Get normalized format lists



123
124
125
# File 'lib/jekyll-imgflow/config.rb', line 123

def normalized_input_formats
  @input_formats.map(&:downcase)
end

#normalized_output_formatsObject



127
128
129
# File 'lib/jekyll-imgflow/config.rb', line 127

def normalized_output_formats
  @formats.map(&:downcase)
end

#sharp_urlObject



80
81
82
# File 'lib/jekyll-imgflow/config.rb', line 80

def sharp_url
  merged_config["sharp_url"]
end

#supported_input_format?(format) ⇒ Boolean

Format validation methods

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/jekyll-imgflow/config.rb', line 98

def supported_input_format?(format)
  normalized_format = format.to_s.downcase.delete(".")
  @input_formats.map(&:downcase).include?(normalized_format)
end

#supported_output_format?(format) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/jekyll-imgflow/config.rb', line 103

def supported_output_format?(format)
  normalized_format = format.to_s.downcase.delete(".")
  @formats.map(&:downcase).include?(normalized_format)
end

#validate_input_format!(format) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
112
113
# File 'lib/jekyll-imgflow/config.rb', line 108

def validate_input_format!(format)
  return if supported_input_format?(format)

  raise ArgumentError,
        "Unsupported input format '#{format}'. Supported formats: #{@input_formats.join(', ')}"
end

#validate_output_format!(format) ⇒ Object

Raises:

  • (ArgumentError)


115
116
117
118
119
120
# File 'lib/jekyll-imgflow/config.rb', line 115

def validate_output_format!(format)
  return if supported_output_format?(format)

  raise ArgumentError,
        "Unsupported output format '#{format}'. Supported formats: #{@formats.join(', ')}"
end