Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
emmmile authored and sverrirs committed Dec 2, 2017
1 parent 9a25c0d commit 302359e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
13 changes: 5 additions & 8 deletions lib/jekyll-webp/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
7 changes: 4 additions & 3 deletions lib/jekyll-webp/webpExec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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
Expand All @@ -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
Expand Down
29 changes: 13 additions & 16 deletions lib/jekyll-webp/webpGenerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 302359e

Please sign in to comment.