Module: Jekyll::Filters::Dzsave

Defined in:
lib/jekyll/filters/dzsave.rb

Overview

Liquid filter for use in templates

Instance Method Summary collapse

Instance Method Details

#dzsave(input, layout = 'google', tile_size = 256) ⇒ Object

Generates an image pyramid



10
11
12
13
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
# File 'lib/jekyll/filters/dzsave.rb', line 10

def dzsave(input, layout = 'google', tile_size = 256)
  return input unless input

  site = @context.registers[:site]
  path = site.in_source_dir(input)

  unless ::File.exist?(path)
    Jekyll.logger.warn "File doesn't exist #{input}"
    return input
  end

  dir = ::File.dirname(input)
  previous_tiles = []

  Dir.chdir(site.source) do
    previous_tiles = Dir.glob(::File.join(dir, '*', '**', '*.jpg'))
  end

  image = Vips::Image.new_from_file(path, access: :sequential)
  image.dzsave(dir, layout: layout, tile_size: tile_size)

  Dir.chdir(site.source) do
    (Dir.glob(::File.join(dir, '*', '**', '*.jpg')) - previous_tiles).each do |tile|
      site.static_files << Jekyll::StaticFile.new(site, site.source, ::File.dirname(tile), ::File.basename(tile))
    end
  end

  "#{dir}/{z}/{y}/{x}.jpg"
rescue Vips::Error => e
  Jekyll.logger.warn "Failed to process #{input}: #{e.message}"
  input
end