Class: Jekyll::ResponsiveImage::ResizeHandler

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/jekyll/responsive_image/resize_handler.rb

Instance Method Summary collapse

Methods included from Utils

#image_hash, #symbolize_keys

Instance Method Details

#ensure_output_dir_exists!(dir) ⇒ Object



46
47
48
49
50
51
# File 'lib/jekyll/responsive_image/resize_handler.rb', line 46

def ensure_output_dir_exists!(dir)
  unless Dir.exists?(dir)
    Jekyll.logger.info "Creating output directory #{dir}"
    FileUtils.mkdir_p(dir)
  end
end

#format_output_path(format, path, width, height) ⇒ Object



37
38
39
40
# File 'lib/jekyll/responsive_image/resize_handler.rb', line 37

def format_output_path(format, path, width, height)
  params = symbolize_keys(image_hash(path, width, height))
  format % params
end

#needs_resizing?(img, width) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/jekyll/responsive_image/resize_handler.rb', line 42

def needs_resizing?(img, width)
  img.columns > width
end

#resize_image(img, config) ⇒ Object



6
7
8
9
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
# File 'lib/jekyll/responsive_image/resize_handler.rb', line 6

def resize_image(img, config)
  resized = []

  config['sizes'].each do |size|
    width = size['width']
    ratio = width.to_f / img.columns.to_f
    height = (img.rows.to_f * ratio).round

    next unless needs_resizing?(img, width)

    filepath = format_output_path(config['output_path_format'], img.filename, width, height)
    resized.push(image_hash(filepath, width, height))

    # Don't resize images more than once
    next if File.exists?(filepath)

    ensure_output_dir_exists!(File.dirname(filepath))

    Jekyll.logger.info "Generating #{filepath}"

    i = img.scale(ratio)
    i.write(filepath) do |f|
      f.quality = size['quality'] || config['default_quality']
    end

    i.destroy!
  end

  resized
end