Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gif functionality #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bin/filthyApple-gif2webp
Binary file not shown.
Binary file added bin/linux-gif2webp
Binary file not shown.
Binary file added bin/win-x64-gif2webp.exe
Binary file not shown.
Binary file added bin/win-x86-gif2webp.exe
Binary file not shown.
17 changes: 6 additions & 11 deletions lib/jekyll-webp/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' => []
}
Expand Down
44 changes: 29 additions & 15 deletions lib/jekyll-webp/webpExec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

Expand All @@ -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 = ""
Expand All @@ -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

Expand Down
35 changes: 13 additions & 22 deletions lib/jekyll-webp/webpGenerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -60,26 +50,22 @@ 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

# If the file is not one of the supported formats, exit early
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)

Expand All @@ -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)
Expand All @@ -110,6 +101,6 @@ def generate(site)
end #function generate

end #class WebPGenerator

end #module Webp
end #module Jekyll