This repository has been archived by the owner on Jan 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
image_optim.rb
101 lines (88 loc) · 3.61 KB
/
image_optim.rb
1
2
3
4
5
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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require "image_optim"
require 'yaml'
module Jekyll
class ImageOptimGenerator < Generator
safe true
###########################################################################
# Entry point for the plugin.
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 => 50}
})
# 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
###########################################################################
# 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"
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
###########################################################################
# Determine whether or not optimization needs to occur.
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
###########################################################################
# In-place image optimization per the ImageOptim library.
def optimize(image)
puts "Optimizing #{image}"
FileUtils.copy image, archival_filename(image)
@image_optim.optimize_image! image
@last_update[image] = File.mtime image
end
###########################################################################
# Adds the date/time of archival as well as the MD5 digest of the original
# source file.
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
end
end