From 302359e15a2ddfe5963e7b1ebcb10326f313daa9 Mon Sep 17 00:00:00 2001 From: Emilio Del Tessandoro Date: Sat, 25 Nov 2017 00:02:41 +0100 Subject: [PATCH] fixes after review --- lib/jekyll-webp/defaults.rb | 13 +++++-------- lib/jekyll-webp/webpExec.rb | 7 ++++--- lib/jekyll-webp/webpGenerator.rb | 29 +++++++++++++---------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/lib/jekyll-webp/defaults.rb b/lib/jekyll-webp/defaults.rb index 9e7d22f..43c11e8 100644 --- a/lib/jekyll-webp/defaults.rb +++ b/lib/jekyll-webp/defaults.rb @@ -6,15 +6,12 @@ module Webp DEFAULT = { 'enabled' => false, - # The flags to pass to the webp binary. For a list of valid parameters check here: - # https://developers.google.com/speed/webp/docs/cwebp#options - 'flags' => "-q 75", - - # For best lossy compression use - # 'flags' => "-q 100 -m 6 -pass 10 -af", + # The quality of the webp conversion 0 to 100 (where 100 is least lossy) + 'quality' => 75, - # For best lossless compression use - # 'flags' => "-q 100 -lossless -z 9", + # Other flags to pass to the webp binary. For a list of valid parameters check here: + # https://developers.google.com/speed/webp/docs/cwebp#options + 'flags' => "-m 4 -pass 4 -af", # List of directories containing images to optimize, Nested directories will not be checked 'img_dir' => ["/img"], diff --git a/lib/jekyll-webp/webpExec.rb b/lib/jekyll-webp/webpExec.rb index 7d6c84b..de09635 100644 --- a/lib/jekyll-webp/webpExec.rb +++ b/lib/jekyll-webp/webpExec.rb @@ -9,7 +9,7 @@ class WebpExec # Runs the WebP executable for the given input parameters # the function detects the OS platform and architecture automatically # - def self.run(flags, input_file, output_file) + def self.run(quality, flags, input_file, output_file) # What is the path to the execs inside the gem? perhaps just bin/? bin_path = "bin/" @@ -27,7 +27,7 @@ def self.run(flags, input_file, output_file) full_path = File.join(gem_root, bin_path, exe_name) # Construct the full program call - cmd = "\"#{full_path}\" -quiet -mt #{flags} \"#{input_file}\" -o \"#{output_file}\"" + cmd = "\"#{full_path}\" -quiet -mt -q #{quality.to_s} #{flags} \"#{input_file}\" -o \"#{output_file}\"" # Execute the command exit_code = 0 @@ -41,7 +41,8 @@ def self.run(flags, input_file, output_file) end if exit_code != 0 - Jekyll.logger.error("WebP:","cwebp returned #{exit_code} with error #{error}") + Jekyll.logger.error("WebP:","Conversion for image #{input_file} failed, no webp version could be created for this image") + Jekyll.logger.debug("WebP:","cwebp returned #{exit_code} with error #{error}") end # Return any captured return value diff --git a/lib/jekyll-webp/webpGenerator.rb b/lib/jekyll-webp/webpGenerator.rb index e1528ac..c17c432 100644 --- a/lib/jekyll-webp/webpGenerator.rb +++ b/lib/jekyll-webp/webpGenerator.rb @@ -69,28 +69,25 @@ def generate(site) FileUtils::mkdir_p(imgdir_destination + imgfile_relative_path) outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, outfile_filename) - # Keep the webp file from being cleaned by Jekyll - site.static_files << WebpFile.new(site, - site.dest, - File.join(imgdir, imgfile_relative_path), - outfile_filename) - # Check if the file already has a webp alternative? # If we're force rebuilding all webp files then ignore the check # also check the modified time on the files to ensure that the webp file # is newer than the source file, if not then regenerate - next if !@config['regenerate'] && File.file?(outfile_fullpath_webp) && - File.mtime(outfile_fullpath_webp) > File.mtime(imgfile) - - if( File.file?(outfile_fullpath_webp) && - File.mtime(outfile_fullpath_webp) <= File.mtime(imgfile) ) + if @config['regenerate'] || !File.file?(outfile_fullpath_webp) || + File.mtime(outfile_fullpath_webp) <= File.mtime(imgfile) Jekyll.logger.info "WebP:", "Change to source image file #{imgfile} detected, regenerating WebP" - end - # Generate the file - WebpExec.run(@config['flags'], imgfile, outfile_fullpath_webp) - file_count += 1 - + # Generate the file + WebpExec.run(@config['quality'], @config['flags'], imgfile, outfile_fullpath_webp) + file_count += 1 + end + if File.file?(outfile_fullpath_webp) + # Keep the webp file from being cleaned by Jekyll + site.static_files << WebpFile.new(site, + site.dest, + File.join(imgdir, imgfile_relative_path), + outfile_filename) + end end # dir.foreach end # img_dir