Class: Jekyll::ImageOptimGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-image-optim.rb

Instance Method Summary collapse

Instance Method Details

#analyze(image) ⇒ Object

Determine whether or not optimization needs to occur.



67
68
69
70
71
72
73
74
75
# File 'lib/jekyll-image-optim.rb', line 67

def analyze(image)
  if @last_update.has_key? image
    # If we've touched the image before, but it's been modified, optimize.
    optimize image if @last_update[image] != File.mtime(image)
  else
    # If the image is new, optimize.
    optimize image
  end
end

#archival_filename(image) ⇒ Object

Adds the date/time of archival as well as the MD5 digest of the original source file.



89
90
91
92
93
94
95
96
97
98
# File 'lib/jekyll-image-optim.rb', line 89

def archival_filename(image)
  ext = File.extname(image)
  "%s/%s-%s-%s%s" % [
    @config["archive_dir"],
    File.basename(image, ext),
    DateTime.now.strftime("%Y-%m-%d-%H-%M-%S"),
    Digest::MD5.file(image).hexdigest,
    ext,
  ]
end

#default_optionsObject

Native settings for the plugin. Override with corresonding entries to _config.yml under “image_optim”

Example:

[_config.yml]
image_optim:
  archive_dir: "_my_original_images"
  cache_file: "_custom_file.yml"
  image_glob: "webroot/images/*.png"


54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-image-optim.rb', line 54

def default_options
  {
    # Where do we store archival copies of the originals?
    "archive_dir" => "_image_optim_archive",
    # Where do we store our cache file?
    "cache_file" => "_image_optim_cache.yml",
    # What images do we search for?
    "image_glob" => "images/**/*.{gif,jpg,jpeg,png}",
  }
end

#generate(site) ⇒ Object

Entry point for the plugin.



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-image-optim.rb', line 11

def generate(site)
  # Read configuration. Defaults first, then overrides from _config.yml.
  config = YAML::load_file "_config.yml"
  config = config["image_optim"] || {}
  @config = default_options.merge! config

  # Initialize the ImageOptim library, which does the heavy lifting.
  @image_optim = ImageOptim.new(
    {
      :allow_lossy => true,  #Allow worker, it is always lossy (defaults to false)
      :svgo => false,
      :pngout => false,
      :verbose => false,
      :pngquant => {:quality => 70..85},  #min..max - don't save below min, use less colors below max (both in range 0..100; in yaml - !ruby/range 0..100), ignored in default/lossless mode (defaults to 100..100, 0..100 in lossy mode)
      :jpegrecompress => {:quality => 0},   #JPEG quality preset: 0 - low, 1 - medium, 2 - high, 3 - veryhigh (defaults to 3)
      :jpegoptim => {:max_quality => 85}
    })

  # Read the cache file, if it exists.
  @last_update = YAML::load_file @config["cache_file"] if File.file? @config["cache_file"]
  @last_update ||= {}

  # Create the originals directory.
  FileUtils.mkdir_p @config["archive_dir"]

  # Iterate over all images, optimizing as necessary.
  Dir.glob(@config["image_glob"]) { |image| analyze image }

  # Save modifications back to the cache file.
  File.open(@config["cache_file"], "w") { |file| file.write @last_update.to_yaml }
end

#optimize(image) ⇒ Object

In-place image optimization per the ImageOptim library.



79
80
81
82
83
84
# File 'lib/jekyll-image-optim.rb', line 79

def optimize(image)
  puts "Optimizing #{image}"
  FileUtils.copy image, archival_filename(image)
  @image_optim.optimize_image! image
  @last_update[image] = File.mtime image
end