image-optimizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "yaop", ">= 0.1.0"
5
+ gem "jpegtran", ">= 0.1.0"
6
+ gem "optipng", ">= 0.2.0"
7
+ gem "jpegoptim", ">= 0.2.0"
8
+ gem "mini_magick", ">= 3.2.1"
9
+ gem "unix-whereis", ">= 0.1.0"
10
+ gem "options-hash", ">= 0.1.0"
11
+
12
+ # Add dependencies to develop your gem here.
13
+ # Include everything needed to run rake, tests, features, etc.
14
+ group :development do
15
+ gem "bundler", "~> 1.0.14"
16
+ gem "jeweler", "~> 1.6.0"
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ command-builder (0.1.0)
6
+ hash-utils (>= 0.7.0)
7
+ git (1.2.5)
8
+ hash-utils (0.15.0)
9
+ hashie (1.0.0)
10
+ jeweler (1.6.0)
11
+ bundler (~> 1.0.0)
12
+ git (>= 1.2.5)
13
+ rake
14
+ jpegoptim (0.2.0)
15
+ command-builder (>= 0.1.0)
16
+ pipe-run (>= 0.2.0)
17
+ unix-whereis (>= 0.1.0)
18
+ jpegtran (0.1.0)
19
+ command-builder (>= 0.1.0)
20
+ hash-utils (>= 0.15.0)
21
+ lookup-hash (>= 0.2.0)
22
+ pipe-run (>= 0.2.1)
23
+ unix-whereis (>= 0.1.0)
24
+ lookup-hash (0.2.0)
25
+ hash-utils (>= 0.11.0)
26
+ mini_magick (3.2.1)
27
+ subexec (~> 0.0.4)
28
+ multitype-introspection (0.1.2)
29
+ options-hash (0.1.0)
30
+ hashie (>= 1.0.0)
31
+ optipng (0.2.0)
32
+ command-builder (>= 0.1.0)
33
+ pipe-run (>= 0.2.0)
34
+ unix-whereis (>= 0.1.0)
35
+ pipe-run (0.2.1)
36
+ rake (0.9.0)
37
+ subexec (0.0.4)
38
+ types (0.1.3)
39
+ abstract (>= 1.0.0)
40
+ multitype-introspection (>= 0.1.0)
41
+ unix-whereis (0.1.0)
42
+ command-builder (>= 0.1.0)
43
+ hash-utils (>= 0.9.0)
44
+ pipe-run (>= 0.1.0)
45
+ yaop (0.1.0)
46
+ hash-utils (>= 0.1.0)
47
+ types (>= 0.1.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ bundler (~> 1.0.14)
54
+ jeweler (~> 1.6.0)
55
+ jpegoptim (>= 0.2.0)
56
+ jpegtran (>= 0.1.0)
57
+ mini_magick (>= 3.2.1)
58
+ options-hash (>= 0.1.0)
59
+ optipng (>= 0.2.0)
60
+ unix-whereis (>= 0.1.0)
61
+ yaop (>= 0.1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ Image Optimizer
2
+ ===============
3
+
4
+ **Image Optimizer** optimizes given JPEG and PNG images or images in
5
+ given folder. Performs the following for PNG:
6
+
7
+ 1. rewrites PNG file using `convert`,
8
+ 2. optimizes it using `optipng`.
9
+
10
+ And for JPEG:
11
+
12
+ 1. strips all unnecessary metadata using `jpegoptim`,
13
+ 2. optimizes using `jpegtran`.
14
+
15
+ ## Usage
16
+
17
+ Simply both for files or whole directories:
18
+
19
+ ImageOptimizer::optimize("./some-dir", :strip => false, :level => 7) do |file|
20
+ p file # prints out target filename
21
+ end
22
+
23
+ Where `:strip` option indicates it should strip all metadata from
24
+ JPEG files. Default is `true`. `:level` indicates level of
25
+ PNG optimization. Default is `7`.
26
+
27
+ For *command line* usage see help for the `image-optimizer` command.
28
+
29
+ ## Requirements
30
+
31
+ Following software for full functionality is necessary:
32
+
33
+ * [UNIX][2]-like system (*required*),
34
+ * [Ruby][1] (*required*),
35
+ * [whereis][3] (*required*),
36
+ * [ImageMagick][8],
37
+ * [Jpegtran][5],
38
+ * [Jpegoptim][6],
39
+ * [OptiPNG][7].
40
+
41
+ Steps which requires non-strictly required components will be silently
42
+ ignored if these components will not be available.
43
+
44
+ Contributing
45
+ ------------
46
+
47
+ 1. Fork it.
48
+ 2. Create a branch (`git checkout -b 20101220-my-change`).
49
+ 3. Commit your changes (`git commit -am "Added something"`).
50
+ 4. Push to the branch (`git push origin 20101220-my-change`).
51
+ 5. Create an [Issue][9] with a link to your branch.
52
+ 6. Enjoy a refreshing Diet Coke and wait.
53
+
54
+ Copyright
55
+ ---------
56
+
57
+ Copyright © 2011 [Martin Kozák][10]. See `LICENSE.txt` for
58
+ further details.
59
+
60
+ [1]: http://www.ruby-lang.org/
61
+ [2]: http://en.wikipedia.org/wiki/Unix
62
+ [3]: http://en.wikipedia.org/wiki/Linux
63
+ [4]: http://linux.die.net/man/1/whereis
64
+ [5]: http://linux.die.net/man/1/jpegtran
65
+ [6]: http://freshmeat.net/projects/jpegoptim/
66
+ [7]: http://optipng.sourceforge.net/
67
+ [8]: http://www.imagemagick.org/
68
+ [9]: http://github.com/martinkozak/image-optimizer/issues
69
+ [10]: http://www.martinkozak.net/
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+
14
+ require 'rake'
15
+ require 'jeweler'
16
+
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+
20
+ gem.name = "image-optimizer"
21
+ gem.homepage = "http://github.com/martinkozak/image-optimizer"
22
+ gem.license = "MIT"
23
+ gem.summary = "Optimizes given JPEG and PNG images or images in given folder."
24
+ gem.email = "martinkozak@martinkozak.net"
25
+ gem.authors = ["Martin Kozák"]
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,46 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
4
+
5
+ require "image-optimizer"
6
+ require "yaop"
7
+
8
+ # Parses command line
9
+ opts = YAOP::get do
10
+ option "--help"
11
+ type Boolean, false
12
+
13
+ option "--version"
14
+ type Boolean, false
15
+
16
+ option "--strip"
17
+ type Boolean, true
18
+
19
+ option "--level"
20
+ type Integer, 7
21
+ end
22
+
23
+ # Shows help or version
24
+ if opts.arguments["--help"]
25
+ puts "Usage: image-optimizer <arguments> [files or folders]*"
26
+ puts "\t--help display this help and exit"
27
+ puts "\t--version output version information and exit"
28
+ puts "\t--level 1-7, intensity of PNG optimization"
29
+ puts "\t--strip indicates it should strip all JPEG files metadata"
30
+ exit
31
+ elsif opts.arguments["--version"]
32
+ puts "image-optimizer 0.1.0"
33
+ puts "Copyright (C) 2011 Martin Kozák (martinkozak@martinkozak.net)"
34
+ puts "Optimizes given JPEG and PNG images or all images in given folders."
35
+ exit
36
+ end
37
+
38
+ # Optimizes
39
+ args = {
40
+ :strip => opts.arguments["--strip"],
41
+ :level => opts.arguments["--level"]
42
+ }
43
+
44
+ ImageOptimizer::optimize(opts.parameters, args) do |i|
45
+ puts i << "\n"
46
+ end
@@ -0,0 +1,73 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{image-optimizer}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Martin Kozák}]
12
+ s.date = %q{2011-05-29}
13
+ s.email = %q{martinkozak@martinkozak.net}
14
+ s.executables = [%q{image-optimizer}]
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/image-optimizer",
28
+ "image-optimizer.gemspec",
29
+ "lib/image-optimizer.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/martinkozak/image-optimizer}
32
+ s.licenses = [%q{MIT}]
33
+ s.require_paths = [%q{lib}]
34
+ s.rubygems_version = %q{1.8.4}
35
+ s.summary = %q{Optimizes given JPEG and PNG images or images in given folder.}
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_runtime_dependency(%q<yaop>, [">= 0.1.0"])
42
+ s.add_runtime_dependency(%q<jpegtran>, [">= 0.1.0"])
43
+ s.add_runtime_dependency(%q<optipng>, [">= 0.2.0"])
44
+ s.add_runtime_dependency(%q<jpegoptim>, [">= 0.2.0"])
45
+ s.add_runtime_dependency(%q<mini_magick>, [">= 3.2.1"])
46
+ s.add_runtime_dependency(%q<unix-whereis>, [">= 0.1.0"])
47
+ s.add_runtime_dependency(%q<options-hash>, [">= 0.1.0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.14"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
50
+ else
51
+ s.add_dependency(%q<yaop>, [">= 0.1.0"])
52
+ s.add_dependency(%q<jpegtran>, [">= 0.1.0"])
53
+ s.add_dependency(%q<optipng>, [">= 0.2.0"])
54
+ s.add_dependency(%q<jpegoptim>, [">= 0.2.0"])
55
+ s.add_dependency(%q<mini_magick>, [">= 3.2.1"])
56
+ s.add_dependency(%q<unix-whereis>, [">= 0.1.0"])
57
+ s.add_dependency(%q<options-hash>, [">= 0.1.0"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.14"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<yaop>, [">= 0.1.0"])
63
+ s.add_dependency(%q<jpegtran>, [">= 0.1.0"])
64
+ s.add_dependency(%q<optipng>, [">= 0.2.0"])
65
+ s.add_dependency(%q<jpegoptim>, [">= 0.2.0"])
66
+ s.add_dependency(%q<mini_magick>, [">= 3.2.1"])
67
+ s.add_dependency(%q<unix-whereis>, [">= 0.1.0"])
68
+ s.add_dependency(%q<options-hash>, [">= 0.1.0"])
69
+ s.add_dependency(%q<bundler>, ["~> 1.0.14"])
70
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
71
+ end
72
+ end
73
+
@@ -0,0 +1,111 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "jpegtran"
5
+ require "optipng"
6
+ require "jpegoptim"
7
+
8
+ require "mini_magick"
9
+ require "unix/whereis"
10
+ require "options-hash"
11
+
12
+ module ImageOptimizer
13
+
14
+ ##
15
+ # Optimizes given file or folder.
16
+ #
17
+ # If array given, go recursively deep to it. Yields paths to
18
+ # optimized files.
19
+ #
20
+ # @param [String, Array] item path or paths to files or directories
21
+ # @param [Hash] options options for optimizing
22
+ # @param [Proc] block block for giving back the results
23
+ # @option options [Boolean] :strip indicates it should strip metadata from JPEG files
24
+ # @option options [Integer] :level indicates level of PNG optimizations
25
+ #
26
+
27
+ def self.optimize(item, options = { }, &block)
28
+ if item.kind_of? Array
29
+ item.each do |i|
30
+ self::optimize(i, options, &block)
31
+ end
32
+ else
33
+ if File.file? item
34
+ self::optimize_file(item, options, &block)
35
+ elsif File.directory? item
36
+ self::optimize_directory(item, options, &block)
37
+ end
38
+ end
39
+ end
40
+
41
+ ##
42
+ # Optimizes given folder.
43
+ # Yields paths to optimized files.
44
+ #
45
+ # @param [String] item path to directory
46
+ # @param [Hash] options options for optimizing
47
+ # @param [Proc] block block for giving back the results
48
+ # @option options [Boolean] :strip indicates it should strip metadata from JPEG files
49
+ # @option options [Integer] :level indicates level of PNG optimizations
50
+ #
51
+
52
+ def self.optimize_directory(item, options = { }, &block)
53
+ Dir.open(item) do |dir|
54
+ dir.each do |i|
55
+ if i[0].chr != ?.
56
+ self::optimize(item + "/" + i, options, &block)
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ ##
63
+ # Optimizes given file.
64
+ # Yields paths to optimized files.
65
+ #
66
+ # @param [String] item path to file
67
+ # @param [Hash] options options for optimizing
68
+ # @param [Proc] block block for giving back the results
69
+ # @option options [Boolean] :strip indicates it should strip metadata from JPEG files
70
+ # @option options [Integer] :level indicates level of PNG optimizations
71
+ #
72
+
73
+ def self.optimize_file(path, options = { }, &block)
74
+ ext = File.extname(path)
75
+
76
+ # Loads options
77
+ opts = OptionsHash::get(options) [
78
+ :strip => true,
79
+ :level => 7
80
+ ]
81
+
82
+ case ext
83
+ when ".png"
84
+ if Whereis.available? :convert
85
+ begin
86
+ image = MiniMagick::Image.open(path)
87
+ image.write(path)
88
+ rescue MiniMagick::Error
89
+ # skips
90
+ end
91
+ end
92
+
93
+ if Optipng.available?
94
+ Optipng.optimize(path, { :level => opts[:level] })
95
+ end
96
+
97
+ block.call(path)
98
+ when ".jpg"
99
+ if Jpegoptim.available?
100
+ Jpegoptim.optimize(path, opts[:strip].true? ? { :strip => :all } : { })
101
+ end
102
+
103
+ if Jpegtran.available?
104
+ Jpegtran.optimize(path, { :optimize => true, :progressive => true })
105
+ end
106
+
107
+ block.call(path)
108
+ end
109
+ end
110
+
111
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image-optimizer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Martin Koz\xC3\xA1k"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-29 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: yaop
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: jpegtran
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: optipng
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 0.2.0
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: jpegoptim
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 0.2.0
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: mini_magick
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.2.1
67
+ type: :runtime
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: unix-whereis
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 0.1.0
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: options-hash
83
+ requirement: &id007 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 0.1.0
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: *id007
92
+ - !ruby/object:Gem::Dependency
93
+ name: bundler
94
+ requirement: &id008 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: 1.0.14
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *id008
103
+ - !ruby/object:Gem::Dependency
104
+ name: jeweler
105
+ requirement: &id009 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 1.6.0
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: *id009
114
+ description:
115
+ email: martinkozak@martinkozak.net
116
+ executables:
117
+ - image-optimizer
118
+ extensions: []
119
+
120
+ extra_rdoc_files:
121
+ - LICENSE.txt
122
+ - README.md
123
+ files:
124
+ - .document
125
+ - Gemfile
126
+ - Gemfile.lock
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - VERSION
131
+ - bin/image-optimizer
132
+ - image-optimizer.gemspec
133
+ - lib/image-optimizer.rb
134
+ homepage: http://github.com/martinkozak/image-optimizer
135
+ licenses:
136
+ - MIT
137
+ post_install_message:
138
+ rdoc_options: []
139
+
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: -1503019227960848104
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: "0"
157
+ requirements: []
158
+
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.4
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Optimizes given JPEG and PNG images or images in given folder.
164
+ test_files: []
165
+