image_optimizer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.travis.yml +12 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +6 -0
- data/image_optimizer.gemspec +22 -0
- data/lib/image_optimizer/jpeg_optimizer.rb +38 -0
- data/lib/image_optimizer/png_optimizer.rb +38 -0
- data/lib/image_optimizer/version.rb +3 -0
- data/lib/image_optimizer.rb +16 -0
- data/spec/image_optimizer/jpeg_optimizer_spec.rb +20 -0
- data/spec/image_optimizer/png_optimizer_spec.rb +20 -0
- data/spec/image_optimizer/version_spec.rb +7 -0
- data/spec/image_optimizer_spec.rb +11 -0
- data/spec/spec_helper.rb +2 -0
- metadata +104 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Julian Tescher
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# ImageOptimizer
|
2
|
+
|
3
|
+
This gem allows you to simply optimize images via jpegoptim or OptiPNG.
|
4
|
+
|
5
|
+
Tested against ruby 1.8.7, 1.9.2, 1.9.3, ruby-head, jruby-18mode, jruby-19mode, jruby-head, rbx-18mode, rbx-19mode, and
|
6
|
+
ree
|
7
|
+
|
8
|
+
[![Build Status](https://secure.travis-ci.org/jtescher/image_optimizer.png)]
|
9
|
+
(http://travis-ci.org/jtescher/image_optimizer)
|
10
|
+
[![Dependency Status](https://gemnasium.com/jtescher/image_optimizer.png)](https://gemnasium.com/jtescher/image_optimizer)
|
11
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/jtescher/image_optimizer)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
##### This gem uses the following utilities for optimizing images:
|
16
|
+
|
17
|
+
1. jpegoptim, which can be installed from [freecode.com](http://freecode.com/projects/jpegoptim)
|
18
|
+
|
19
|
+
2. OptiPNG, which can be installed from [sourceforge.net](http://optipng.sourceforge.net/)
|
20
|
+
|
21
|
+
Or install the utilities via homebrew:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
$ brew install optipng jpegoptim
|
25
|
+
```
|
26
|
+
|
27
|
+
Then add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
gem 'image_optimizer'
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
$ bundle
|
35
|
+
```
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
```bash
|
39
|
+
$ gem install image_optimizer
|
40
|
+
```
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
#### Optimize PNG or GIF formats:
|
45
|
+
|
46
|
+
OptiPNG is a PNG optimizer that recompresses image files to a smaller size without losing any information and
|
47
|
+
performs PNG integrity checks and corrections.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
ImageOptimizer.new('path/to/file.png').optimize
|
51
|
+
```
|
52
|
+
|
53
|
+
#### Optimize JPEG formats:
|
54
|
+
|
55
|
+
jpegoptim provides lossless optimization for JPEG files based on optimizing the Huffman tables.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
ImageOptimizer.new('path/to/file.jpg').optimize
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'image_optimizer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "image_optimizer"
|
8
|
+
gem.version = ImageOptimizer::VERSION
|
9
|
+
gem.authors = ["Julian Tescher"]
|
10
|
+
gem.email = ["jatescher@gmail.com"]
|
11
|
+
gem.description = %q{A simple image optimizer}
|
12
|
+
gem.summary = %q{Simply optimize images via jpegoptim or OptiPNG}
|
13
|
+
gem.homepage = "https://github.com/jtescher/image_optimizer"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class ImageOptimizer
|
2
|
+
class JPEGOptimizer
|
3
|
+
attr_reader :path
|
4
|
+
|
5
|
+
def initialize(path)
|
6
|
+
@path = path
|
7
|
+
end
|
8
|
+
|
9
|
+
def optimize
|
10
|
+
return unless jpeg_format?
|
11
|
+
|
12
|
+
if jpeg_optimizer_present?
|
13
|
+
optimize_jpeg
|
14
|
+
else
|
15
|
+
warn 'Attempting to optimize a jpeg without jpegoptim installed. Skipping...'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def jpeg_format?
|
22
|
+
['jpeg', 'jpg'].include? extension(path)
|
23
|
+
end
|
24
|
+
|
25
|
+
def extension(path)
|
26
|
+
path.split(".").last.downcase
|
27
|
+
end
|
28
|
+
|
29
|
+
def optimize_jpeg
|
30
|
+
system "jpegoptim -f --strip-all #{path}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def jpeg_optimizer_present?
|
34
|
+
`which jpegoptim` && $?.success?
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class ImageOptimizer
|
2
|
+
class PNGOptimizer
|
3
|
+
attr_reader :path
|
4
|
+
|
5
|
+
def initialize(path)
|
6
|
+
@path = path
|
7
|
+
end
|
8
|
+
|
9
|
+
def optimize
|
10
|
+
return unless png_format?
|
11
|
+
|
12
|
+
if png_optimizer_present?
|
13
|
+
optimize_png
|
14
|
+
else
|
15
|
+
warn 'Attempting to optimize a png without optipng installed. Skipping...'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def png_format?
|
22
|
+
['png', 'gif'].include? extension(path)
|
23
|
+
end
|
24
|
+
|
25
|
+
def extension(path)
|
26
|
+
path.split(".").last.downcase
|
27
|
+
end
|
28
|
+
|
29
|
+
def optimize_png
|
30
|
+
system "optipng -o7 #{path}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def png_optimizer_present?
|
34
|
+
`which optipng` && $?.success?
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "image_optimizer/version"
|
2
|
+
require "image_optimizer/jpeg_optimizer"
|
3
|
+
require "image_optimizer/png_optimizer"
|
4
|
+
|
5
|
+
class ImageOptimizer
|
6
|
+
attr_reader :path
|
7
|
+
|
8
|
+
def initialize(path)
|
9
|
+
@path = path
|
10
|
+
end
|
11
|
+
|
12
|
+
def optimize
|
13
|
+
JPEGOptimizer.new(path).optimize
|
14
|
+
PNGOptimizer.new(path).optimize
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ImageOptimizer::JPEGOptimizer do
|
4
|
+
describe '#optimize' do
|
5
|
+
let(:file_path) { '/path/to/file.jpg' }
|
6
|
+
let(:jpeg_optimizer) { ImageOptimizer::JPEGOptimizer.new(file_path) }
|
7
|
+
|
8
|
+
it 'optimizes the jpeg' do
|
9
|
+
jpeg_optimizer.stub(jpeg_optimizer_present?: true)
|
10
|
+
jpeg_optimizer.should_receive(:system).with("jpegoptim -f --strip-all #{jpeg_optimizer.path}")
|
11
|
+
jpeg_optimizer.optimize
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'warns the user if the jpeg optimizing utility is not installed' do
|
15
|
+
jpeg_optimizer.stub(jpeg_optimizer_present?: false)
|
16
|
+
jpeg_optimizer.should_receive(:warn).with('Attempting to optimize a jpeg without jpegoptim installed. Skipping...')
|
17
|
+
jpeg_optimizer.optimize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ImageOptimizer::PNGOptimizer do
|
4
|
+
describe '#optimize' do
|
5
|
+
let(:file_path) { '/path/to/file.png' }
|
6
|
+
let(:png_optimizer) { ImageOptimizer::PNGOptimizer.new(file_path) }
|
7
|
+
|
8
|
+
it 'optimizes the png' do
|
9
|
+
png_optimizer.stub(png_optimizer_present?: true)
|
10
|
+
png_optimizer.should_receive(:system).with("optipng -o7 #{png_optimizer.path}")
|
11
|
+
png_optimizer.optimize
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'warns the user if the png optimizing utility is not installed' do
|
15
|
+
png_optimizer.stub(png_optimizer_present?: false)
|
16
|
+
png_optimizer.should_receive(:warn).with('Attempting to optimize a png without optipng installed. Skipping...')
|
17
|
+
png_optimizer.optimize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ImageOptimizer do
|
4
|
+
describe "#optimize" do
|
5
|
+
it 'delegates to jpeg and png optimizers' do
|
6
|
+
ImageOptimizer::JPEGOptimizer.any_instance.should_receive(:optimize)
|
7
|
+
ImageOptimizer::PNGOptimizer.any_instance.should_receive(:optimize)
|
8
|
+
ImageOptimizer.new('/path/to/image.jpg').optimize
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image_optimizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Julian Tescher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A simple image optimizer
|
47
|
+
email:
|
48
|
+
- jatescher@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .travis.yml
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- image_optimizer.gemspec
|
60
|
+
- lib/image_optimizer.rb
|
61
|
+
- lib/image_optimizer/jpeg_optimizer.rb
|
62
|
+
- lib/image_optimizer/png_optimizer.rb
|
63
|
+
- lib/image_optimizer/version.rb
|
64
|
+
- spec/image_optimizer/jpeg_optimizer_spec.rb
|
65
|
+
- spec/image_optimizer/png_optimizer_spec.rb
|
66
|
+
- spec/image_optimizer/version_spec.rb
|
67
|
+
- spec/image_optimizer_spec.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
homepage: https://github.com/jtescher/image_optimizer
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
hash: 914025074646364729
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
hash: 914025074646364729
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.24
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Simply optimize images via jpegoptim or OptiPNG
|
99
|
+
test_files:
|
100
|
+
- spec/image_optimizer/jpeg_optimizer_spec.rb
|
101
|
+
- spec/image_optimizer/png_optimizer_spec.rb
|
102
|
+
- spec/image_optimizer/version_spec.rb
|
103
|
+
- spec/image_optimizer_spec.rb
|
104
|
+
- spec/spec_helper.rb
|