nanoc-image-compressor 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,16 +1,13 @@
1
1
  # Nanoc Image Compressor
2
2
 
3
- A [nanoc](http://nanoc.stoneship.org/) filter that compresses `jpg` and `png` images losslessly.
3
+ [![Build Status](https://secure.travis-ci.org/jingoro/nanoc-image-compressor.png?branch=master)](http://travis-ci.org/jingoro/nanoc-image-compressor)
4
4
 
5
- ## Dependencies
6
-
7
- This gem uses the [sprockets-image_compressor](https://github.com/botandrose/sprockets-image_compressor)
8
- gem which depends on [pngcrush](http://pmt.sourceforge.net/pngcrush/) and
9
- [jpegoptim](http://www.kokkonen.net/tjko/projects.html) being installed.
10
- As a fall back, the gem includes 32-bit and 64-bit binaries for Linux.
5
+ A [nanoc](http://nanoc.stoneship.org/) filter that compresses `gif`, `jpg` and `png` images losslessly.
11
6
 
12
7
  ## Installation
13
8
 
9
+ ### 1. Install the gem
10
+
14
11
  Add this line to your site's `Gemfile`:
15
12
 
16
13
  gem 'nanoc-image-compressor'
@@ -23,7 +20,16 @@ Or install it yourself as:
23
20
 
24
21
  $ gem install nanoc-image-compressor
25
22
 
26
- Then, add this line to your site's `lib/default.rb`:
23
+ ### 2. Install the binaries
24
+
25
+ This gem uses the [image\_optim](https://github.com/toy/image_optim)
26
+ gem which depends on binaries being installed (`advpng`, `gifsicle`, `jpegoptim`, `jpegtran`,
27
+ `optipng`, `pngcrush`, `pngout`). See the [image\_optim README](https://github.com/toy/image_optim)
28
+ for instructions on how to install these binaries.
29
+
30
+ ### 3. Add a `require` statement
31
+
32
+ Add this line to your site's `lib/default.rb`:
27
33
 
28
34
  require 'nanoc/filters/image_compressor'
29
35
 
@@ -34,3 +40,12 @@ Add a filter within a `compile` block in your site's `Rules`:
34
40
  compile '/images/*/' do
35
41
  filter :image_compressor if item.binary?
36
42
  end
43
+
44
+ Any options will be passed to `image_optim`:
45
+
46
+ compile '/images/*/' do
47
+ if item.binary?
48
+ # we don't have pngout on our system
49
+ filter :image_compressor, :pngout => false
50
+ end
51
+ end
@@ -1,12 +1,12 @@
1
1
  require 'fileutils'
2
+ require 'image_optim'
2
3
  require 'nanoc'
3
- require 'sprockets/image_compressor'
4
4
 
5
5
  module Nanoc
6
6
  module Filters
7
7
  class ImageCompressor < Nanoc::Filter
8
8
 
9
- VERSION = '0.0.1'
9
+ VERSION = '0.1.0'
10
10
 
11
11
  identifier :image_compressor
12
12
  type :binary
@@ -14,25 +14,13 @@ module Nanoc
14
14
  # Compresses the content with Sprockets::ImageCompressor.
15
15
  #
16
16
  # @param [String] filename The filename to compress
17
- # @option params [String] :type ('auto') png, jpg or auto
17
+ # @param [Hash] params Passed through to ImageOptim.new
18
18
  # @return [void]
19
19
  def run(filename, params={})
20
- type = (params[:type] || 'auto').to_s.downcase
21
- type = filetype_guess(filename) if type == 'auto'
22
- content = File.read(filename)
23
- compressor = case type
24
- when 'png'
25
- content = Sprockets::ImageCompressor::PngCompressor.new.compress(content)
26
- when 'jpg', 'jpeg'
27
- content = Sprockets::ImageCompressor::JpgCompressor.new.compress(content)
28
- end
29
- File.open(output_filename, 'wb') { |f| f.write content }
30
- end
31
-
32
- private
33
-
34
- def filetype_guess(filename)
35
- filename.sub(/^.+\.([a-z]+)$/i, '\1').downcase
20
+ io = ImageOptim.new(params.merge(:threads => false))
21
+ result = io.optimize_image(filename)
22
+ path = result ? result.to_s : filename
23
+ FileUtils.cp path, output_filename
36
24
  end
37
25
 
38
26
  end
@@ -4,7 +4,7 @@ Gem::Specification.new do |gem|
4
4
  gem.authors = ["John Nishinaga"]
5
5
  gem.email = ["jingoro@casa-z.org"]
6
6
  gem.description = %q{Nanoc image compressor filter}
7
- gem.summary = %q{A nanoc filter that compresses jpg and png images losslessly}
7
+ gem.summary = %q{A nanoc filter that compresses gif, jpg and png images losslessly}
8
8
  gem.homepage = "https://github.com/jingoro/nanoc-image-compressor"
9
9
 
10
10
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -12,10 +12,10 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
13
  gem.name = "nanoc-image-compressor"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = '0.0.1'
16
-
17
- gem.add_dependency 'nanoc', '>= 3.3.0'
18
- gem.add_dependency 'sprockets-image_compressor'
15
+ gem.version = '0.1.0'
16
+
17
+ gem.add_dependency 'nanoc', '>= 3.3.1'
18
+ gem.add_dependency 'image_optim'
19
19
  gem.add_development_dependency 'rake'
20
20
  gem.add_development_dependency 'rspec'
21
21
  gem.add_development_dependency 'yard'
@@ -1,8 +1,15 @@
1
1
  require 'tempfile'
2
+ require 'fileutils'
3
+
2
4
  require 'nanoc/filters/image_compressor'
3
5
 
4
6
  describe Nanoc::Filters::ImageCompressor do
5
7
 
8
+ let(:gif_path) { File.expand_path('../test.gif', __FILE__) }
9
+ let(:jpg_path) { File.expand_path('../test.jpg', __FILE__) }
10
+ let(:png_path) { File.expand_path('../test.png', __FILE__) }
11
+ let(:run_options) { { :pngout => false } }
12
+
6
13
  before do
7
14
  item = double('item')
8
15
  item.stub(:identifier) { '/test/' }
@@ -14,63 +21,50 @@ describe Nanoc::Filters::ImageCompressor do
14
21
 
15
22
  let(:extension) { path.sub(/^.+\.([a-z]+)$/, '\1') }
16
23
  let(:output_size) { File.size subject.output_filename }
17
-
18
- def self.it_should_compress_to_less_than(max_size)
19
24
 
20
- it 'should compress implicitly' do
21
- subject.run path
22
- output_size.should be < max_size
23
- end
24
-
25
- it 'should compress explicitly' do
26
- subject.run path, :type => extension
27
- output_size.should be < max_size
25
+ context 'jpg image' do
26
+ let(:path) { jpg_path }
27
+ it 'should compress' do
28
+ subject.run path, run_options
29
+ output_size.should be < 300
28
30
  end
29
-
30
31
  end
31
32
 
32
- def self.it_should_not_compress
33
-
34
- it 'should pass through implicitly' do
35
- subject.run path
36
- output_size.should == File.size(path)
37
- end
38
-
39
- it 'should pass through explicitly' do
40
- subject.run path, :type => extension
41
- output_size.should == File.size(path)
42
- end
43
-
44
- end
45
-
46
- context 'jpg image' do
47
- let(:path) { File.expand_path '../test.jpg', __FILE__ }
48
- it_should_compress_to_less_than 300
49
- end
50
-
51
33
  context 'jpeg image' do
52
34
  let(:path) do
53
35
  file = Tempfile.new(['test', '.jpeg'])
54
- file.write File.read(File.expand_path('../test.jpg', __FILE__))
55
36
  file.close
37
+ FileUtils.cp jpg_path, file.path
56
38
  file.path
57
39
  end
58
- it_should_compress_to_less_than 300
40
+ it 'should compress' do
41
+ subject.run path, run_options
42
+ output_size.should be < 300
43
+ end
59
44
  end
60
45
 
61
46
  context 'png image' do
62
- let(:path) { File.expand_path '../test.png', __FILE__ }
63
- it_should_compress_to_less_than 1900
47
+ let(:path) { png_path }
48
+ it 'should compress' do
49
+ subject.run path, run_options
50
+ output_size.should be < 1900
51
+ end
64
52
  end
65
-
53
+
66
54
  context 'gif image' do
67
- let(:path) { File.expand_path '../test.gif', __FILE__ }
68
- it_should_not_compress
55
+ let(:path) { gif_path }
56
+ it 'should compress' do
57
+ subject.run path, run_options
58
+ output_size.should be < 400
59
+ end
69
60
  end
70
61
 
71
62
  context 'text file' do
72
63
  let(:path) { __FILE__ }
73
- it_should_not_compress
64
+ it 'should pass through' do
65
+ subject.run path, run_options
66
+ output_size.should == File.size(__FILE__)
67
+ end
74
68
  end
75
69
 
76
70
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nanoc-image-compressor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Nishinaga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-13 00:00:00 Z
18
+ date: 2012-02-20 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: nanoc
@@ -25,16 +25,16 @@ dependencies:
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- hash: 11
28
+ hash: 9
29
29
  segments:
30
30
  - 3
31
31
  - 3
32
- - 0
33
- version: 3.3.0
32
+ - 1
33
+ version: 3.3.1
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
36
  - !ruby/object:Gem::Dependency
37
- name: sprockets-image_compressor
37
+ name: image_optim
38
38
  prerelease: false
39
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
@@ -159,7 +159,7 @@ rubyforge_project:
159
159
  rubygems_version: 1.8.10
160
160
  signing_key:
161
161
  specification_version: 3
162
- summary: A nanoc filter that compresses jpg and png images losslessly
162
+ summary: A nanoc filter that compresses gif, jpg and png images losslessly
163
163
  test_files:
164
164
  - spec/image_compressor_spec.rb
165
165
  - spec/test.gif