Skip to content

Extended the library with new features #20

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

Open
wants to merge 7 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
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
[![Gem Version](https://img.shields.io/gem/v/jekyll-webp.svg)](https://rubygems.org/gems/jekyll-webp)
[![Gem](https://img.shields.io/gem/dt/jekyll-webp.svg)](https://rubygems.org/gems/jekyll-webp)
[![Join the chat at https://gitter.im/jekyll-webp/Lobby](https://badges.gitter.im/jekyll-webp/Lobby.svg)](https://gitter.im/jekyll-webp/Lobby)
[![Dependency Status](https://gemnasium.com/badges/github.com/sverrirs/jekyll-webp.svg)](https://gemnasium.com/github.com/sverrirs/jekyll-webp)
[![Code Climate](https://codeclimate.com/github/sverrirs/jekyll-webp/badges/gpa.svg)](https://codeclimate.com/github/sverrirs/jekyll-webp)
[![security](https://hakiri.io/github/sverrirs/jekyll-webp/master.svg)](https://hakiri.io/github/sverrirs/jekyll-webp/master)

# WebP Generator for Jekyll
# WebP Generator for Jekyll (Extended)
WebP Image Generator for Jekyll Sites can automatically generate WebP images for all images on your static site and serve them when possible. View on [rubygems.org](https://rubygems.org/gems/jekyll-webp).

> Read more about this tool on my blog at <a href="https://blog.sverrirs.com/2016/06/webp-generator-for-jekyll-sites.html" target="_blank">blog.sverrirs.com</a>
**This repo is an extended version of it's fork [jekyll-webp](https://github.com/sverrirs/jekyll-webp)**

## Installation
## Additional Features
- Generate thumbnails in webp format
- Generate small images (.5x) in webp format
- Specifiy output sub directory for webp images

## Additional Dependencies
- fastimage

## Installation instructions
```
group :jekyll_plugins do
gem 'jekyll-webp', git: 'https://github.com/Rohithzr/jekyll-webp-ext', branch: 'extend'
end
```

## Original installation instructions

```
gem install jekyll-webp
Expand Down Expand Up @@ -63,6 +71,22 @@ webp:
# append '.webp' to filename after original extension rather than replacing it.
# Default transforms `image.png` to `image.webp`, while changing to true transforms `image.png` to `image.png.webp`
append_ext: false

##### Extended Features start
# Use a different output subdirectory
# e.g. value of "/optimized" will create files at "/source/optimized" instead of "/source"
output_img_sub_dir: "/optimized" # default ""

# Generate thumbnails
thumbs: true

# Thumbnails sub directory
thumbs_dir: "/thumbs"

# generate .5x images
generate_50p: true

##### Extended Features end
############################################################
```

Expand Down
4 changes: 4 additions & 0 deletions jekyll-webp.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8
require_relative 'lib/jekyll-webp/version'
require "date"

Gem::Specification.new do |spec|
spec.name = "jekyll-webp"
Expand All @@ -19,8 +20,11 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^spec/})
spec.require_paths = ["lib"]

spec.add_dependency "fastimage", '>= 2.0.0'

spec.add_development_dependency "jekyll", "~> 3.0"
spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake", "~> 1.5"
spec.add_development_dependency "minitest", '~> 5.4', '>= 5.4.3'

end
15 changes: 14 additions & 1 deletion lib/jekyll-webp/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,20 @@ module Webp

# List of files or directories to explicitly include
# e.g. single files outside of the main image directories
'include' => []
'include' => [],

# Use a different output subdirectory
# e.g. value of "/optimized" will create files at "/source/optimized" instead of "/source"
'output_img_sub_dir' => "",

# Generate thumbnails
'thumbs' => false,

# Thumbnails sub directory
'thumbs_dir' => "/thumbs",

# generate .5x images
'generate_50p' => false
}

end # module Webp
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-webp/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Jekyll
module Webp
VERSION = "1.0.0"
VERSION = "1.1.0"
# When modifying remember to issue a new tag command in git before committing, then push the new tag
# git tag -a v1.0.0 -m "Gem v1.0.0"
# git push origin --tags
Expand Down
6 changes: 4 additions & 2 deletions lib/jekyll-webp/webpExec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'open3'
require 'fastimage'

module Jekyll
module Webp
Expand Down Expand Up @@ -41,12 +42,13 @@ def self.run(quality, flags, input_file, output_file, webp_bin_fullpath)
stdin.close # we don't pass any input to the process
output = stdout.gets
error = stderr.gets

exit_code = wait_thr.value
end

if exit_code != 0
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}")
# Jekyll.logger.error("WebP:","Conversion for image #{input_file} failed, no webp version could be created for this image")
Jekyll.logger.error("WebP:","cwebp returned #{exit_code} with error #{error}")
end

# Return any captured return value
Expand Down
48 changes: 45 additions & 3 deletions lib/jekyll-webp/webpGenerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ def generate(site)

# Counting the number of files generated
file_count = 0
thumb_count = 0

# Iterate through every image in each of the image folders and create a webp image
# if one has not been created already for that image.
for imgdir in @config['img_dir']
imgdir_source = File.join(site.source, imgdir)
imgdir_destination = File.join(site.dest, imgdir)
FileUtils::mkdir_p(imgdir_destination)
if @config['output_img_sub_dir'] != ""
FileUtils::mkdir_p(File.join(imgdir_destination, @config['output_img_sub_dir']))
end
if @config['thumbs']
FileUtils::mkdir_p(File.join(imgdir_destination, @config['thumbs_dir']))
end
Jekyll.logger.info "WebP:","Processing #{imgdir_source}"

# handle only jpg, jpeg, png and gif
Expand All @@ -80,8 +87,13 @@ def generate(site)
file_noext = File.basename(imgfile, file_ext)
file_noext + ".webp"
end

small_outfile_filename = File.basename(imgfile, file_ext) + "-small" + ".webp"

FileUtils::mkdir_p(imgdir_destination + imgfile_relative_path)
outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, outfile_filename)
outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, @config['output_img_sub_dir'], outfile_filename)
small_outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, @config['output_img_sub_dir'], small_outfile_filename)
thumb_outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, @config['thumbs_dir'], outfile_filename)

# Check if the file already has a webp alternative?
# If we're force rebuilding all webp files then ignore the check
Expand All @@ -94,18 +106,48 @@ def generate(site)
# Generate the file
WebpExec.run(@config['quality'], @config['flags'], imgfile, outfile_fullpath_webp, @config['webp_path'])
file_count += 1

if @config['generate_50p']
# Get the image size
Jekyll.logger.info "WebP:", "Generating small image file #{outfile_filename}"
image_size = FastImage.size(imgfile, :raise_on_failure=>true, :timeout=>2.0)
h_width = image_size[0] / 2
size_flags = "-resize #{h_width} 0" + " " + @config['flags']
WebpExec.run(@config['quality'], size_flags, imgfile, small_outfile_fullpath_webp, @config['webp_path'])
end

# Generate the thumbnails
if @config['thumbs']
Jekyll.logger.info "WebP:", "Generating thumbnail for #{outfile_filename} in #{@config['thumbs_dir']}"
thumb_flags = "-resize 400 0" + " " + @config['flags']
WebpExec.run(@config['quality'], thumb_flags, imgfile, thumb_outfile_fullpath_webp, @config['webp_path'])
thumb_count += 1
end
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),
File.join(imgdir, imgfile_relative_path, @config['output_img_sub_dir']),
outfile_filename)
if @config['thumbs']
site.static_files << WebpFile.new(site,
site.dest,
File.join(imgdir, imgfile_relative_path, @config['thumbs_dir']),
outfile_filename)
end
if @config['generate_50p']
site.static_files << WebpFile.new(site,
site.dest,
File.join(imgdir, imgfile_relative_path, @config['output_img_sub_dir']),
small_outfile_filename)
end
end
end # dir.foreach
end # img_dir

Jekyll.logger.info "WebP:","Generator Complete: #{file_count} file(s) generated"
Jekyll.logger.info "WebP:","Generator Complete: #{file_count} file(s) generated #{thumb_count} thumbnail(s) generated"

end #function generate

Expand Down