Class: JekyllAutoThumbnails::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-auto-thumbnails/generator.rb

Overview

Thumbnail generation via ImageMagick

Instance Method Summary collapse

Constructor Details

#initialize(config, site_source) ⇒ Generator

Initialize generator

Parameters:

  • config (Configuration)

    configuration

  • site_source (String)

    Jekyll site source directory



13
14
15
16
# File 'lib/jekyll-auto-thumbnails/generator.rb', line 13

def initialize(config, site_source)
  @config = config
  @site_source = site_source
end

Instance Method Details

#build_thumbnail_filename(basename, digest, width, height, ext) ⇒ String

Build thumbnail filename

Parameters:

  • basename (String)

    image basename (no extension)

  • digest (String)

    6-char MD5 digest

  • width (Integer, nil)

    width

  • height (Integer, nil)

    height

  • ext (String)

    file extension

Returns:

  • (String)

    thumbnail filename



74
75
76
77
78
# File 'lib/jekyll-auto-thumbnails/generator.rb', line 74

def build_thumbnail_filename(basename, digest, width, height, ext)
  width_str = width || ""
  height_str = height || ""
  "#{basename}_thumb-#{digest}-#{width_str}x#{height_str}#{ext}"
end

#generate(url, width, height) ⇒ String?

Generate thumbnail (with caching)

Parameters:

  • url (String)

    image URL

  • width (Integer, nil)

    target width

  • height (Integer, nil)

    target height

Returns:

  • (String, nil)

    path to cached thumbnail or nil if failed



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
58
59
60
61
62
63
64
# File 'lib/jekyll-auto-thumbnails/generator.rb', line 31

def generate(url, width, height)
  # Resolve source file
  source_path = UrlResolver.to_filesystem_path(url, @site_source)
  return nil unless source_path && File.exist?(source_path)

  # Compute digest
  digest = DigestCalculator.short_digest(source_path)

  # Build thumbnail filename
  basename = File.basename(source_path, File.extname(source_path))
  ext = File.extname(source_path)
  thumb_filename = build_thumbnail_filename(basename, digest, width, height, ext)

  # Check cache
  cached_path = File.join(@config.cache_dir, thumb_filename)
  return cached_path if File.exist?(cached_path)

  # Generate
  FileUtils.mkdir_p(@config.cache_dir)
  success = shell_generate(source_path, cached_path, width, height)

  return nil unless success

  # Check if thumbnail is larger than original
  if File.size(cached_path) > File.size(source_path)
    Jekyll.logger.warn "AutoThumbnails:",
                       "Thumbnail larger than original (#{File.size(cached_path)} > #{File.size(source_path)}), " \
                       "deleting #{cached_path}"
    FileUtils.rm_f(cached_path)
    return nil
  end

  cached_path
end

#imagemagick_available?Boolean

Check if ImageMagick is available (cross-platform)

Returns:

  • (Boolean)

    true if ImageMagick (v6 or v7) is available



21
22
23
# File 'lib/jekyll-auto-thumbnails/generator.rb', line 21

def imagemagick_available?
  ImageMagickWrapper.available?
end