Skip to content

Commit 302359e

Browse files
emmmilesverrirs
authored andcommitted
fixes after review
1 parent 9a25c0d commit 302359e

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

lib/jekyll-webp/defaults.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ module Webp
66
DEFAULT = {
77
'enabled' => false,
88

9-
# The flags to pass to the webp binary. For a list of valid parameters check here:
10-
# https://developers.google.com/speed/webp/docs/cwebp#options
11-
'flags' => "-q 75",
12-
13-
# For best lossy compression use
14-
# 'flags' => "-q 100 -m 6 -pass 10 -af",
9+
# The quality of the webp conversion 0 to 100 (where 100 is least lossy)
10+
'quality' => 75,
1511

16-
# For best lossless compression use
17-
# 'flags' => "-q 100 -lossless -z 9",
12+
# Other flags to pass to the webp binary. For a list of valid parameters check here:
13+
# https://developers.google.com/speed/webp/docs/cwebp#options
14+
'flags' => "-m 4 -pass 4 -af",
1815

1916
# List of directories containing images to optimize, Nested directories will not be checked
2017
'img_dir' => ["/img"],

lib/jekyll-webp/webpExec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class WebpExec
99
# Runs the WebP executable for the given input parameters
1010
# the function detects the OS platform and architecture automatically
1111
#
12-
def self.run(flags, input_file, output_file)
12+
def self.run(quality, flags, input_file, output_file)
1313

1414
# What is the path to the execs inside the gem? perhaps just bin/?
1515
bin_path = "bin/"
@@ -27,7 +27,7 @@ def self.run(flags, input_file, output_file)
2727
full_path = File.join(gem_root, bin_path, exe_name)
2828

2929
# Construct the full program call
30-
cmd = "\"#{full_path}\" -quiet -mt #{flags} \"#{input_file}\" -o \"#{output_file}\""
30+
cmd = "\"#{full_path}\" -quiet -mt -q #{quality.to_s} #{flags} \"#{input_file}\" -o \"#{output_file}\""
3131

3232
# Execute the command
3333
exit_code = 0
@@ -41,7 +41,8 @@ def self.run(flags, input_file, output_file)
4141
end
4242

4343
if exit_code != 0
44-
Jekyll.logger.error("WebP:","cwebp returned #{exit_code} with error #{error}")
44+
Jekyll.logger.error("WebP:","Conversion for image #{input_file} failed, no webp version could be created for this image")
45+
Jekyll.logger.debug("WebP:","cwebp returned #{exit_code} with error #{error}")
4546
end
4647

4748
# Return any captured return value

lib/jekyll-webp/webpGenerator.rb

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,25 @@ def generate(site)
6969
FileUtils::mkdir_p(imgdir_destination + imgfile_relative_path)
7070
outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, outfile_filename)
7171

72-
# Keep the webp file from being cleaned by Jekyll
73-
site.static_files << WebpFile.new(site,
74-
site.dest,
75-
File.join(imgdir, imgfile_relative_path),
76-
outfile_filename)
77-
7872
# Check if the file already has a webp alternative?
7973
# If we're force rebuilding all webp files then ignore the check
8074
# also check the modified time on the files to ensure that the webp file
8175
# is newer than the source file, if not then regenerate
82-
next if !@config['regenerate'] && File.file?(outfile_fullpath_webp) &&
83-
File.mtime(outfile_fullpath_webp) > File.mtime(imgfile)
84-
85-
if( File.file?(outfile_fullpath_webp) &&
86-
File.mtime(outfile_fullpath_webp) <= File.mtime(imgfile) )
76+
if @config['regenerate'] || !File.file?(outfile_fullpath_webp) ||
77+
File.mtime(outfile_fullpath_webp) <= File.mtime(imgfile)
8778
Jekyll.logger.info "WebP:", "Change to source image file #{imgfile} detected, regenerating WebP"
88-
end
8979

90-
# Generate the file
91-
WebpExec.run(@config['flags'], imgfile, outfile_fullpath_webp)
92-
file_count += 1
93-
80+
# Generate the file
81+
WebpExec.run(@config['quality'], @config['flags'], imgfile, outfile_fullpath_webp)
82+
file_count += 1
83+
end
84+
if File.file?(outfile_fullpath_webp)
85+
# Keep the webp file from being cleaned by Jekyll
86+
site.static_files << WebpFile.new(site,
87+
site.dest,
88+
File.join(imgdir, imgfile_relative_path),
89+
outfile_filename)
90+
end
9491
end # dir.foreach
9592
end # img_dir
9693

0 commit comments

Comments
 (0)