jekyll-image-optim 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/image_optim.rb +101 -0
  3. metadata +73 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a7e3a1f3f196f12ab47e90b8906d08d53a9c8eef1a17a4621196459d0bd53a23
4
+ data.tar.gz: 59019ce9484c743d5da45ca74c5c4f190535055053260fa4c47886343df0a531
5
+ SHA512:
6
+ metadata.gz: efaf08f42010fba69e61c4a098bbfd309c48fbd157ff378f911cb5ca5b050e2a9826b8b8877ae9cb84324da9392887d5a73f677f6785289da4d7f92d5dd49a58
7
+ data.tar.gz: 4ba4a0aff9454336552b4f98ee46a6e5ef1a2afbf4a1da6f397da468db41a3d2eb62b387a7cbac510d7e92e83fd023556fdd10100057ebe29f740048d1e43f03
@@ -0,0 +1,101 @@
1
+ require "image_optim"
2
+ require 'yaml'
3
+
4
+ module Jekyll
5
+
6
+ class ImageOptimGenerator < Generator
7
+ safe true
8
+
9
+ ###########################################################################
10
+ # Entry point for the plugin.
11
+ def generate(site)
12
+ # Read configuration. Defaults first, then overrides from _config.yml.
13
+ config = YAML::load_file "_config.yml"
14
+ config = config["image_optim"] || {}
15
+ @config = default_options.merge! config
16
+
17
+ # Initialize the ImageOptim library, which does the heavy lifting.
18
+ @image_optim = ImageOptim.new(
19
+ {
20
+ :allow_lossy => true, #Allow worker, it is always lossy (defaults to false)
21
+ :svgo => false,
22
+ :pngout => false,
23
+ :verbose => false,
24
+ :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)
25
+ :jpegrecompress => {:quality => 0}, #JPEG quality preset: 0 - low, 1 - medium, 2 - high, 3 - veryhigh (defaults to 3)
26
+ :jpegoptim => {:max_quality => 50}
27
+ })
28
+
29
+ # Read the cache file, if it exists.
30
+ @last_update = YAML::load_file @config["cache_file"] if File.file? @config["cache_file"]
31
+ @last_update ||= {}
32
+
33
+ # Create the originals directory.
34
+ FileUtils.mkdir_p @config["archive_dir"]
35
+
36
+ # Iterate over all images, optimizing as necessary.
37
+ Dir.glob(@config["image_glob"]) { |image| analyze image }
38
+
39
+ # Save modifications back to the cache file.
40
+ File.open(@config["cache_file"], "w") { |file| file.write @last_update.to_yaml }
41
+ end
42
+
43
+ ###########################################################################
44
+ # Native settings for the plugin.
45
+ # Override with corresonding entries to _config.yml under "image_optim"
46
+ #
47
+ # Example:
48
+ #
49
+ # [_config.yml]
50
+ # image_optim:
51
+ # archive_dir: "_my_original_images"
52
+ # cache_file: "_custom_file.yml"
53
+ # image_glob: "webroot/images/*.png"
54
+ def default_options
55
+ {
56
+ # Where do we store archival copies of the originals?
57
+ "archive_dir" => "_image_optim_archive",
58
+ # Where do we store our cache file?
59
+ "cache_file" => "_image_optim_cache.yml",
60
+ # What images do we search for?
61
+ "image_glob" => "images/**/*.{gif,jpg,jpeg,png}",
62
+ }
63
+ end
64
+
65
+ ###########################################################################
66
+ # Determine whether or not optimization needs to occur.
67
+ def analyze(image)
68
+ if @last_update.has_key? image
69
+ # If we've touched the image before, but it's been modified, optimize.
70
+ optimize image if @last_update[image] != File.mtime(image)
71
+ else
72
+ # If the image is new, optimize.
73
+ optimize image
74
+ end
75
+ end
76
+
77
+ ###########################################################################
78
+ # In-place image optimization per the ImageOptim library.
79
+ def optimize(image)
80
+ puts "Optimizing #{image}"
81
+ FileUtils.copy image, archival_filename(image)
82
+ @image_optim.optimize_image! image
83
+ @last_update[image] = File.mtime image
84
+ end
85
+
86
+ ###########################################################################
87
+ # Adds the date/time of archival as well as the MD5 digest of the original
88
+ # source file.
89
+ def archival_filename(image)
90
+ ext = File.extname(image)
91
+ "%s/%s-%s-%s%s" % [
92
+ @config["archive_dir"],
93
+ File.basename(image, ext),
94
+ DateTime.now.strftime("%Y-%m-%d-%H-%M-%S"),
95
+ Digest::MD5.file(image).hexdigest,
96
+ ext,
97
+ ]
98
+ end
99
+
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-image-optim
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Areeb Khan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: image_optim
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.26'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.26'
27
+ - !ruby/object:Gem::Dependency
28
+ name: image_optim_pack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ description: Plugin to compress images. Configuration and usage instructions can be
42
+ found at https://github.com/chrisanthropic/image_optim-jekyll-plugin
43
+ email:
44
+ - areebk@protonmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - image_optim.rb
50
+ homepage: https://github.com/chrisanthropic/image_optim-jekyll-plugin
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.0.3
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A simple Jekyll plugin to optimize images using image_optim
73
+ test_files: []