diff --git a/bin/filthyApple-gif2webp b/bin/filthyApple-gif2webp new file mode 100644 index 0000000..d310550 Binary files /dev/null and b/bin/filthyApple-gif2webp differ diff --git a/bin/linux-gif2webp b/bin/linux-gif2webp new file mode 100644 index 0000000..ea2a286 Binary files /dev/null and b/bin/linux-gif2webp differ diff --git a/bin/win-x64-gif2webp.exe b/bin/win-x64-gif2webp.exe new file mode 100644 index 0000000..397fa6f Binary files /dev/null and b/bin/win-x64-gif2webp.exe differ diff --git a/bin/win-x86-gif2webp.exe b/bin/win-x86-gif2webp.exe new file mode 100644 index 0000000..4b83ab3 Binary files /dev/null and b/bin/win-x86-gif2webp.exe differ diff --git a/lib/jekyll-webp/defaults.rb b/lib/jekyll-webp/defaults.rb index e079e9e..2887686 100644 --- a/lib/jekyll-webp/defaults.rb +++ b/lib/jekyll-webp/defaults.rb @@ -13,21 +13,16 @@ module Webp # https://developers.google.com/speed/webp/docs/cwebp#options 'flags' => "-m 4 -pass 4 -af", - # List of directories containing images to optimize, Nested directories only be checked if `nested` is true - 'img_dir' => ["/img"], + # Alternative flags for gifs + 'gif_flags' => "-m 4 -lossy", - # Whether to search in nested directories or not - 'nested' => false, + # List of directories containing images to optimize, Nested directories will not be checked + 'img_dir' => ["/img"], # add ".gif" to the format list to generate webp for animated gifs as well 'formats' => [".jpeg", ".jpg", ".png", ".tiff"], - # append .webp to existing extension instead of replacing it - # (Enables more efficient nginx rules. - # See http://www.lazutkin.com/blog/2014/02/23/serve-files-with-nginx-conditionally/) - 'append_ext' => false, - - # File extensions for animated gif files + # File extensions for animated gif files 'gifs' => [".gif"], # Set to true to always regenerate existing webp files @@ -41,7 +36,7 @@ module Webp # e.g. custom or hand generated webp conversion files 'exclude' => [], - # List of files or directories to explicitly include + # List of files or directories to explicitly include # e.g. single files outside of the main image directories 'include' => [] } diff --git a/lib/jekyll-webp/webpExec.rb b/lib/jekyll-webp/webpExec.rb index de09635..f286649 100644 --- a/lib/jekyll-webp/webpExec.rb +++ b/lib/jekyll-webp/webpExec.rb @@ -10,7 +10,6 @@ class WebpExec # the function detects the OS platform and architecture automatically # 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,8 +26,7 @@ def self.run(quality, 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 -q #{quality.to_s} #{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 error = "" @@ -53,22 +51,38 @@ def self.run(quality, flags, input_file, output_file) # Returns the correct executable name depending on the OS platform and OS architecture # def self.exe_name - if OS.mac? - return "osx-cwebp" - elsif OS.windows? - if OS.x32? - return "win-x86-cwebp.exe" + if $is_gif + if OS.mac? + return "filthyApple-gif2webp" + elsif OS.windows? + if OS.x32? + return "win-x86-gif2webp.exe" + else + return "win-x64-gif2webp.exe" + end + elsif OS.unix? || OS.linux? + return "linux-gif2webp" else - return "win-x64-cwebp.exe" + raise ArgumentError.new("OS platform could not be identified (gem can only be run on linux,osx or windows)") end - elsif OS.unix? || OS.linux? - if OS.x32? - return "linux-x86-cwebp" + else + if OS.mac? + return "osx-cwebp" + elsif OS.windows? + if OS.x32? + return "win-x86-cwebp.exe" + else + return "win-x64-cwebp.exe" + end + elsif OS.unix? || OS.linux? + if OS.x32? + return "linux-x86-cwebp" + else + return "linux-x64-cwebp" + end else - return "linux-x64-cwebp" + raise ArgumentError.new("OS platform could not be identified (gem can only be run on linux,osx or windows)") end - else - raise ArgumentError.new("OS platform could not be identified (gem can only be run on linux,osx or windows)") end end #function exe_name diff --git a/lib/jekyll-webp/webpGenerator.rb b/lib/jekyll-webp/webpGenerator.rb index 7548d6e..dd28f18 100644 --- a/lib/jekyll-webp/webpGenerator.rb +++ b/lib/jekyll-webp/webpGenerator.rb @@ -5,7 +5,7 @@ module Jekyll module Webp # - # A static file to hold the generated webp image after generation + # A static file to hold the generated webp image after generation # so that Jekyll will copy it into the site output directory class WebpFile < StaticFile def write(dest) @@ -40,16 +40,6 @@ def generate(site) # If the site destination directory has not yet been created then create it now. Otherwise, we cannot write our file there. Dir::mkdir(site.dest) if !File.directory? site.dest - # If nesting is enabled, get all the nested directories too - if @config['nested'] - newdir = [] - for imgdir in @config['img_dir'] - # Get every directory below (and including) imgdir, recursively - newdir.concat(Dir.glob(imgdir + "/**/")) - end - @config['img_dir'] = newdir - end - # Counting the number of files generated file_count = 0 @@ -60,11 +50,11 @@ def generate(site) imgdir_destination = File.join(site.dest, imgdir) FileUtils::mkdir_p(imgdir_destination) Jekyll.logger.info "WebP:","Processing #{imgdir_source}" - + # handle only jpg, jpeg, png and gif for imgfile in Dir[imgdir_source + "**/*.*"] imgfile_relative_path = File.dirname(imgfile.sub(imgdir_source, "")) - + # Skip empty stuff file_ext = File.extname(imgfile).downcase @@ -72,14 +62,10 @@ def generate(site) next if !@config['formats'].include? file_ext # TODO: Do an exclude check - + # Create the output file path - outfile_filename = if @config['append_ext'] - File.basename(imgfile) + '.webp' - else - file_noext = File.basename(imgfile, file_ext) - file_noext + ".webp" - end + file_noext = File.basename(imgfile, file_ext) + outfile_filename = file_noext+ ".webp" FileUtils::mkdir_p(imgdir_destination + imgfile_relative_path) outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, outfile_filename) @@ -92,7 +78,12 @@ def generate(site) Jekyll.logger.info "WebP:", "Change to source image file #{imgfile} detected, regenerating WebP" # Generate the file - WebpExec.run(@config['quality'], @config['flags'], imgfile, outfile_fullpath_webp) + $is_gif = imgfile.match(/\.gif/) + if $is_gif + WebpExec.run(@config['quality'], @config['gif_flags'], imgfile, outfile_fullpath_webp) + else + WebpExec.run(@config['quality'], @config['flags'], imgfile, outfile_fullpath_webp) + end file_count += 1 end if File.file?(outfile_fullpath_webp) @@ -110,6 +101,6 @@ def generate(site) end #function generate end #class WebPGenerator - + end #module Webp end #module Jekyll