Skip to content

Instantly share code, notes, and snippets.

@sffc
Last active July 12, 2020 09:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sffc/a828267913591159ab1cc3e1bfde20d7 to your computer and use it in GitHub Desktop.
Save sffc/a828267913591159ab1cc3e1bfde20d7 to your computer and use it in GitHub Desktop.
Drop-in extension for image optimization during a Jekyll build. Save jekyll_image_optim.rb in your _plugins directory and make the stated changes to your Gemfile and _config.yml. https://blog.sffc.xyz/post/182420136145/optimized-images-in-jekyll
jekyll_image_optim:
image_optim:
allow_lossy: true
advpng:
level: 2
optipng:
level: 2
# Enable these if you want extra optimization:
pngout: false
svgo: false
gem "image_optim", "~> 0.25"
gem "image_optim_pack", "~> 0.5.1"
# Copyright 2018 Shane F. Carr
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require "digest"
require "fileutils"
require "image_optim"
require "yaml"
Jekyll::Hooks.register :site, :pre_render do |site|
# Load configuration options
config = YAML::load_file(File.join(site.source, "_config.yml"))
config = config["jekyll_image_optim"] || {}
image_optim_options = config["image_optim"] || {}
cache_relative_dir = config["cache_dir"] || ".jekyll-image-optim-cache"
cache_dir = File.join(site.source, cache_relative_dir)
out_dir = File.join(cache_dir, "out")
index_yaml_path = File.join(cache_dir, "index.yml")
FileUtils.mkdir_p(cache_dir)
FileUtils.mkdir_p(out_dir)
FileUtils.touch(index_yaml_path)
cache_index = YAML::load_file(index_yaml_path) || {}
# Exclude the cache directory from the jekyll watch
site.exclude << cache_relative_dir
# Process the images
image_optim = ImageOptim.new(image_optim_options)
site.static_files.map! do |static_file|
if not image_optim.optimizable?(static_file.path)
# Not an image file; no change necessary
next static_file
end
relative_path = static_file.relative_path
new_path = File.join(out_dir, relative_path)
cc = cache_index[relative_path]
image_md5 = Digest::MD5.file(static_file.path).hexdigest
if not cc or not File.exists?(new_path) or cc[:md5] != image_md5 or cc[:options] != image_optim_options
# Need to compute the optimized image
puts "Optimizing image: #{relative_path}"
FileUtils.mkdir_p(File.dirname(new_path))
FileUtils.cp(static_file.path, new_path)
image_optim.optimize_image!(new_path)
cache_index[relative_path] = { md5: image_md5, options: image_optim_options }
# Save the cache index file (do this for every image to allow restart after an interrupt)
File.open(index_yaml_path, "w") { |f| f.write(cache_index.to_yaml) }
end
# Replace the Jekyll::StaticFile to point to the new location
next Jekyll::StaticFile.new(site, out_dir, File.dirname(relative_path), File.basename(relative_path))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment