rails-webp 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +39 -0
- data/README.md +32 -4
- data/lib/rails/webp/converter.rb +67 -0
- data/lib/rails/webp/post_processor.rb +27 -0
- data/lib/rails/webp/railtie.rb +19 -0
- data/lib/rails/webp/version.rb +1 -1
- data/rails-webp-0.1.gem +0 -0
- metadata +6 -2
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71dc74349b769d210dfac65270ca98c18370085679b7dc65571a4561cf502b3a
|
4
|
+
data.tar.gz: ade953a26071ceab38f52137acb6188d2805d9641629fc7831f778666dff270f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e141952f99a18c8063373a4fc0480620856d857873ce7eb6c7e19b6cdfa6c707344cee1bed6e2c40404e9515febf5f62634dbfdc05d3db9965d0b505bf013ba
|
7
|
+
data.tar.gz: 960db331883e44ca712d049faabfdceaf35863018e5252fa5fe47c1d053f5ee62c254fb3efdc27b15578adc8ab9f43aebcb506ba17b9a933ae4dfe28918c0adb
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails-webp (0.1)
|
5
|
+
mini_magick
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
byebug (11.0.1)
|
11
|
+
diff-lcs (1.3)
|
12
|
+
mini_magick (4.9.3)
|
13
|
+
rake (10.5.0)
|
14
|
+
rspec (3.8.0)
|
15
|
+
rspec-core (~> 3.8.0)
|
16
|
+
rspec-expectations (~> 3.8.0)
|
17
|
+
rspec-mocks (~> 3.8.0)
|
18
|
+
rspec-core (3.8.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-expectations (3.8.3)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-mocks (3.8.0)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.8.0)
|
26
|
+
rspec-support (3.8.0)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
bundler (~> 2.0)
|
33
|
+
byebug
|
34
|
+
rails-webp!
|
35
|
+
rake (~> 10.0)
|
36
|
+
rspec (~> 3.0)
|
37
|
+
|
38
|
+
BUNDLED WITH
|
39
|
+
2.0.1
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# Rails::Webp
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Create webp copies of your image assets the easy way using [minimagick](https://github.com/minimagick/minimagick)!
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
7
|
+
Before installing, make sure you have [ImageMagick](https://imagemagick.org/script/download.php) installed on your host.
|
8
|
+
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
@@ -22,7 +22,35 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
This gem is intended to be used as a post processor step in your asset pipeline:
|
26
|
+
```
|
27
|
+
bundle exec rake assets:precompile
|
28
|
+
```
|
29
|
+
|
30
|
+
You can configure options passed to ImageMagick in an initializer:
|
31
|
+
|
32
|
+
`config/initializers/webp.rb`:
|
33
|
+
|
34
|
+
```
|
35
|
+
Rails::WebP.encode_options = { quality: 80, lossless: true, method: 6, alpha_filtering: 2, alpha_compression: 0, alpha_quality: 100 }
|
36
|
+
```
|
37
|
+
|
38
|
+
A comprehensive list of options can be found [here](https://imagemagick.org/script/webp.php).
|
39
|
+
|
40
|
+
**Note:** a digest is included in the processed webp image's file name. This digest is compared for each time compilation occurs.
|
41
|
+
By default, `rails-webp` will only process images that have a mismatched digest (AKA hash of source image contents).
|
42
|
+
If you wish to force webp processing:
|
43
|
+
|
44
|
+
```
|
45
|
+
Rails::WebP.force = true # default: false
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
## Motivations for this gem
|
50
|
+
|
51
|
+
In my experience, the existing gems for processing webp images in the Rails Asset Pipeline made questionable assumptions.
|
52
|
+
More importantly, I could not use the native libraries used by those gems in my team's acceptance/production environment.
|
53
|
+
ImageMagick was an easy choice for this reason because it's widely implemented, used, and understood (my opinion).
|
26
54
|
|
27
55
|
## Development
|
28
56
|
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'logger'
|
3
|
+
require 'mini_magick'
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
module WebP
|
7
|
+
class Converter
|
8
|
+
class << self
|
9
|
+
attr_reader :context
|
10
|
+
|
11
|
+
def process(input_path, data, context)
|
12
|
+
@context = context
|
13
|
+
app = Rails.application
|
14
|
+
prefix = app.config.assets.prefix
|
15
|
+
digest = data_digest(data)
|
16
|
+
webp_file = webp_file_name(data, digest)
|
17
|
+
output_path = Pathname.new(File.join(app.root, 'public', prefix, webp_file))
|
18
|
+
if WebP.force || !webp_file_exists?(digest, output_path)
|
19
|
+
FileUtils.mkdir_p(output_path.dirname) unless Dir.exists?(output_path.dirname)
|
20
|
+
convert_to_webp(input_path, output_path)
|
21
|
+
logger.info "Writing #{output_path}"
|
22
|
+
end
|
23
|
+
data
|
24
|
+
end
|
25
|
+
|
26
|
+
def convert_to_webp(input_path, output_path)
|
27
|
+
# Ex: convert wizard.png -quality 50 -define webp:lossless=true wizard.webp
|
28
|
+
MiniMagick::Tool::Convert.new do |convert|
|
29
|
+
convert << input_path
|
30
|
+
options = WebP.encode_options
|
31
|
+
convert << '-quality' << options[:quality]
|
32
|
+
options.except(:quality).each do |name, value|
|
33
|
+
convert << "-define" << "webp:#{name.to_s.dasherize}=#{value}"
|
34
|
+
end
|
35
|
+
convert << output_path
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def data_digest(data)
|
42
|
+
"-#{context.environment.digest_class.new.update(data).to_s}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def webp_file_name(data, digest)
|
46
|
+
file_name = context.logical_path # Original File name w/o extension
|
47
|
+
file_ext = context.pathname.extname # Original File extension
|
48
|
+
"#{file_name}#{digest}.webp" # WebP File fullname
|
49
|
+
end
|
50
|
+
|
51
|
+
def webp_file_exists?(digest, output_path)
|
52
|
+
File.exists?(output_path) && digest == output_path.to_s.split('-').last.split('.').first
|
53
|
+
end
|
54
|
+
|
55
|
+
def logger
|
56
|
+
if context && context.environment
|
57
|
+
context.environment.logger
|
58
|
+
else
|
59
|
+
logger = Logger.new($stderr)
|
60
|
+
logger.level = Logger::FATAL
|
61
|
+
logger
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Rails
|
2
|
+
module WebP
|
3
|
+
class PostProcessor
|
4
|
+
def initialize(filename, &block)
|
5
|
+
@filename = filename
|
6
|
+
@source = block.call
|
7
|
+
end
|
8
|
+
|
9
|
+
def render(context, empty_hash_wtf)
|
10
|
+
self.class.run(@filename, @source, context)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.run(filename, data, context)
|
14
|
+
Converter.process(filename, data, context)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.call(input)
|
18
|
+
filename = input[:filename]
|
19
|
+
source = input[:data]
|
20
|
+
context = input[:environment].context_class.new(input)
|
21
|
+
|
22
|
+
result = run(filename, source, context)
|
23
|
+
context.metadata.merge(data: result)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'sprockets/processing'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module WebP
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
extend Sprockets::Processing
|
7
|
+
|
8
|
+
initializer :webp, group: :all do |app|
|
9
|
+
app.config.assets.configure do |env|
|
10
|
+
env.register_mime_type 'image/jpeg', '.jpeg'
|
11
|
+
env.register_postprocessor 'image/jpeg', PostProcessor
|
12
|
+
|
13
|
+
env.register_mime_type 'image/png', '.png'
|
14
|
+
env.register_postprocessor 'image/png', PostProcessor
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/rails/webp/version.rb
CHANGED
data/rails-webp-0.1.gem
ADDED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-webp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jake Peterson
|
@@ -90,16 +90,20 @@ extra_rdoc_files: []
|
|
90
90
|
files:
|
91
91
|
- ".gitignore"
|
92
92
|
- ".rspec"
|
93
|
-
- ".travis.yml"
|
94
93
|
- CODE_OF_CONDUCT.md
|
95
94
|
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
96
|
- LICENSE.txt
|
97
97
|
- README.md
|
98
98
|
- Rakefile
|
99
99
|
- bin/console
|
100
100
|
- bin/setup
|
101
101
|
- lib/rails/webp.rb
|
102
|
+
- lib/rails/webp/converter.rb
|
103
|
+
- lib/rails/webp/post_processor.rb
|
104
|
+
- lib/rails/webp/railtie.rb
|
102
105
|
- lib/rails/webp/version.rb
|
106
|
+
- rails-webp-0.1.gem
|
103
107
|
- rails-webp.gemspec
|
104
108
|
homepage: https://github.com/jakenberg
|
105
109
|
licenses:
|