Class: Bridgetown::ImagePipeline::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown/image_pipeline/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, output_root:) ⇒ Processor

Returns a new instance of Processor.



9
10
11
12
# File 'lib/bridgetown/image_pipeline/processor.rb', line 9

def initialize(config:, output_root:)
  @config = config
  @output_root = output_root
end

Instance Method Details

#process(source_path, basename:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bridgetown/image_pipeline/processor.rb', line 14

def process(source_path, basename:)
  image = Vips::Image.new_from_file(source_path)
  source_width  = image.width
  source_height = image.height
  original_ext  = File.extname(source_path).downcase.delete(".")
  original_ext  = "jpg" if original_ext == "jpeg"
  original_fmt  = original_ext.to_sym

  variants = []

  @config.widths.each do |target_width|
    next if target_width > source_width

    @config.formats.each do |fmt|
      variants << build_variant(source_path, basename, target_width, fmt)
    end

    # Generate a fallback variant in the source's own format so the
    # <img src> always has something to point at, but skip it when
    # the configured formats already cover the source format —
    # otherwise variants ends up with duplicates (e.g. webp source
    # with formats=[:webp] would emit each width twice).
    next if @config.formats.include?(original_fmt)

    variants << build_variant(source_path, basename, target_width, original_fmt)
  end

  {
    width: source_width,
    height: source_height,
    variants: variants
  }
end