image_optim_rake 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84728f800392a61350da662ec41f49e6bec4da69
4
+ data.tar.gz: df42c824ced1eac1184341b0fcb432161d990403
5
+ SHA512:
6
+ metadata.gz: 0519fa8492aa4732c0f617379ec510dfb9704a9af91b060e75ecc950e553cdf8c5fae6d831e425ecd9ef0b55292695f549354916a6810519c7e73ba712232d3f
7
+ data.tar.gz: 3681ca06e36be77f62905249b047183b20aed6c46aa921fa4105b4780ee849f82fa3e8d8bb7cc8019ffa80ddfc22e27da20d8f6c3647e89d5c616ccf0145259c
@@ -0,0 +1 @@
1
+ .rvmrc
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ - jruby-19mode
6
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,106 @@
1
+ # image_optim_rake
2
+
3
+ [![Build Status](https://api.travis-ci.org/jnbt/image_optim_rake.png?branch=master)](https://travis-ci.org/jnbt/image_optim_rake)
4
+
5
+ A simple wrapper around [image_optim](https://github.com/toy/image_optim) to batch minify all images in one or more directories.
6
+
7
+ Designed to work with Rails and per default recursively looks for JPGs and PNGs in `app\assets\images` and `public`.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'image_optim_rake'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install image_optim_rake
22
+
23
+
24
+ ## Binary installation
25
+
26
+ As above mentions this gem uses image_optim, which itself relies on some binary tools. These must be present at the system, but `pngout` is disabled here per default.
27
+
28
+ ### Linux - Debian/Ubuntu
29
+
30
+ sudo apt-get install -y advancecomp gifsicle jpegoptim libjpeg-progs optipng pngcrush
31
+
32
+ ### Linux - RHEL/Fedora/Centos
33
+
34
+ sudo yum install -y advancecomp gifsicle libjpeg optipng
35
+
36
+ You will also need to install `jpegoptim` and `pngcrush` from source:
37
+
38
+ #### jpegoptim
39
+
40
+ cd /tmp
41
+ curl -O http://www.kokkonen.net/tjko/src/jpegoptim-1.2.4.tar.gz
42
+ tar zxf jpegoptim-1.2.4.tar.gz
43
+ cd jpegoptim-1.2.4
44
+ ./configure && make && make install
45
+
46
+ #### pngcrush
47
+
48
+ cd /tmp
49
+ curl -O http://iweb.dl.sourceforge.net/project/pmt/pngcrush/1.7.43/pngcrush-1.7.43.tar.gz
50
+ tar zxf pngcrush-1.7.43.tar.gz
51
+ cd pngcrush-1.7.43
52
+ make && cp -f pngcrush /usr/local/bin
53
+
54
+ ### OS X: Macports
55
+
56
+ sudo port install advancecomp gifsicle jpegoptim jpeg optipng pngcrush
57
+
58
+ ### OS X: Brew
59
+
60
+ brew install advancecomp gifsicle jpegoptim jpeg optipng pngcrush
61
+
62
+ ### pngout installation (optional)
63
+
64
+ You can install `pngout` by downloading and installing the [binary versions](http://www.jonof.id.au/kenutils).
65
+
66
+ _Note: pngout is free to use even in commercial soft, but you can not redistribute, repackage or reuse it without consent and agreement of creator. [license](http://advsys.net/ken/utils.htm#pngoutkziplicense)_
67
+
68
+ **Copied from:** [https://github.com/toy/image_optim/](https://github.com/toy/image_optim/)
69
+
70
+ ## Usage
71
+
72
+ Change to your project directory and run:
73
+
74
+ $ rake image_optim:minify
75
+
76
+ You can define the directories where images will be searched **recursively** by setting a `dirs` argument:
77
+
78
+ $ rake image_optim:minify dirs=app/assets/images,public,lib/assets/images,vendor/assets/images
79
+
80
+ ## Configuration
81
+
82
+ You can modify the configuration which is passed to image_optim:
83
+
84
+ ImageOptimRake::ImageOptimTask.config = {
85
+ :threads => true,
86
+ :pngout => false
87
+ }
88
+
89
+ You can also modify the default paths:
90
+
91
+ ImageOptimTask.dirs = ["app/assets/images", "public"]
92
+
93
+ A good place would be an initializer in a Rails environment.
94
+
95
+ ## Test
96
+
97
+ $ rake test
98
+
99
+ ## Contributing
100
+
101
+ 1. Fork it
102
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
103
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
104
+ 4. Push to the branch (`git push origin my-new-feature`)
105
+ 5. Create new Pull Request
106
+
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "test"
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.verbose = true
11
+ end
12
+
13
+ require 'image_optim_rake/image_optim_task'
14
+ ImageOptimRake::ImageOptimTask.new
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "image_optim_rake/version"
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "image_optim_rake"
6
+ gem.version = ImageOptimRake::VERSION
7
+ gem.authors = ["Jonas Thiel"]
8
+ gem.email = ["jonasthiel@googlemail.com"]
9
+ gem.homepage = "https://github.com/jnbt/image_optim_rake"
10
+ gem.summary = %q{Provide a little helper command to minify images}
11
+ gem.description = %q{Provide a little helper command to minify images}
12
+
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ gem.require_paths = ["lib"]
17
+
18
+ gem.add_dependency "image_optim", "~> 0.7.3"
19
+ gem.add_dependency "rake"
20
+ gem.add_dependency "progressbar", "~> 0.20.0"
21
+
22
+ gem.add_development_dependency "minitest"
23
+ gem.add_development_dependency "testem"
24
+ end
@@ -0,0 +1,4 @@
1
+ module ImageOptimRake
2
+ require "image_optim_rake/image_optim_task"
3
+ require "image_optim_rake/railtie" if defined?(Rails)
4
+ end
@@ -0,0 +1,16 @@
1
+ module ImageOptimRake
2
+ class Image < Struct.new(:path, :original_size, :compressed_size)
3
+ def initialize(path)
4
+ self.path = path
5
+ self.original_size = open(path, "rb").size
6
+ end
7
+
8
+ def reload_size!
9
+ self.compressed_size = open(path, "rb").size
10
+ end
11
+
12
+ def percentage
13
+ (original_size - compressed_size) == 0 ? 0.0 : (original_size - compressed_size).to_f / original_size.to_f * 100.0
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+ require 'image_optim_rake/minifier'
4
+
5
+ module ImageOptimRake
6
+ class ImageOptimTask < ::Rake::TaskLib
7
+ class << self
8
+ attr_accessor :dirs, :config
9
+ end
10
+
11
+ attr_accessor :name
12
+
13
+ def initialize(name = :image_optim)
14
+ @name = name
15
+ yield self if block_given?
16
+ define
17
+ end
18
+
19
+ def define
20
+ namespace name do
21
+ desc "Minify all images found under /app/assets/images and /public/images"
22
+ task :minify do
23
+ dirs = ENV["dirs"] ? ENV["dirs"].split(",") : self.class.dirs
24
+ Minifier.new(dirs, self.class.config).process!
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ ImageOptimTask.dirs = ["app/assets/images", "public"]
31
+ # Configure with :threads => true leads to auto-detection
32
+ ImageOptimTask.config = {
33
+ :threads => true,
34
+ :pngout => false
35
+ }
36
+ end
@@ -0,0 +1,69 @@
1
+ require "image_optim_rake/image"
2
+ require "image_optim"
3
+ require "progressbar"
4
+
5
+ module ImageOptimRake
6
+ class Minifier
7
+ attr_reader :dirs, :total_count, :total_size, :config
8
+
9
+ def initialize(dirs, config)
10
+ @dirs = Hash[dirs.map{|d| [d, []]}]
11
+ @total_count = 0
12
+ @total_size = 0
13
+ @config = config
14
+ end
15
+
16
+ def process!
17
+ collect_images
18
+ log_images
19
+ process_images!
20
+ log_results
21
+ end
22
+
23
+ private
24
+
25
+ def collect_images
26
+ dirs.each do |dir, images|
27
+ Dir.glob(File.join(dir,"**","*.{jpg,jpeg,png}")) do |image_file|
28
+ image = Image.new(image_file)
29
+ images << image
30
+ @total_size += image.original_size
31
+ @total_count += 1
32
+ end
33
+ end
34
+ end
35
+
36
+ def process_images!
37
+ progress = ::ProgressBar.new("Miniying", total_size)
38
+ processor = ::ImageOptim.new(config)
39
+ dirs.values.flatten.each do |image_file|
40
+ processor.optimize_image!(image_file.path)
41
+ image_file.reload_size!
42
+ progress.inc
43
+ end
44
+ progress.finish
45
+ end
46
+
47
+ def log_images
48
+ puts "Minifying total #{total_count} images at: "
49
+ dirs.each do |dir, images|
50
+ puts "- #{dir}: #{images.size} file(s)"
51
+ end
52
+ puts ""
53
+ puts "Grab a coffee now. This may take a while."
54
+ puts ""
55
+ end
56
+
57
+ def log_results
58
+ puts ""
59
+ puts "Finished minifying images of total original size: %.2d KB" % (total_size / 1024)
60
+ puts ""
61
+ new_total_size = dirs.values.flatten.map(&:compressed_size).reduce(&:+).to_f
62
+ percentage = total_size - new_total_size == 0 ? 0.0 : (total_size - new_total_size).to_f / total_size.to_f * 100.0
63
+ puts "New total size: %.2d KB (%2.2f%%):" % [new_total_size / 1024, percentage]
64
+ dirs.values.flatten.each do |image|
65
+ puts "- #{image.path}: %2.2d%%" % image.percentage
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module ImageOptimRake
4
+ class Railtie < ::Rails::Railtie
5
+ railtie_name :image_optim_rake
6
+
7
+ rake_tasks do
8
+ ImageOptimTask.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ImageOptimRake
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "minitest/autorun"
2
+ require "testem"
3
+
4
+ require "image_optim_rake"
@@ -0,0 +1,24 @@
1
+ require "helper"
2
+
3
+ class ImageTest < Testem
4
+
5
+ let(:image){ ImageOptimRake::Image.new("test/fixtures/unprocessed.jpg")}
6
+
7
+ test "load from image path" do
8
+ assert_equal "test/fixtures/unprocessed.jpg", image.path
9
+ assert_equal 66388, image.original_size
10
+ assert_equal nil, image.compressed_size
11
+ end
12
+
13
+ test "reload_size!" do
14
+ assert_equal nil, image.compressed_size
15
+ image.reload_size!
16
+ assert_equal 66388, image.compressed_size
17
+ end
18
+
19
+ test "percentage" do
20
+ image.original_size = 100
21
+ image.compressed_size = 50
22
+ assert_equal 50, image.percentage
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ require "helper"
2
+
3
+ class MinifierTest < Testem
4
+ let(:dirs){ ["test/fixtures/"] }
5
+
6
+ test "initializes from dirs and config" do
7
+ minifier = ImageOptimRake::Minifier.new(dirs, :foo => :bar)
8
+ expected = {"test/fixtures/" => []}
9
+ assert_equal expected, minifier.dirs
10
+ expected = {:foo => :bar}
11
+ assert_equal expected, minifier.config
12
+ assert_equal 0, minifier.total_size
13
+ assert_equal 0, minifier.total_count
14
+ end
15
+
16
+ test "process!" do
17
+ sizes = {}
18
+
19
+ Dir.mktmpdir("image_optim_rake") do |dir|
20
+ FileUtils.cp_r dirs[0], dir
21
+
22
+ Dir.glob(File.join(dir, "**/*.{jpg,png}")).each do |file|
23
+ sizes[file] = open(file, "rb").size
24
+ end
25
+
26
+ minifier = ImageOptimRake::Minifier.new([dir], :threads => true)
27
+ minifier.process!
28
+
29
+ Dir.glob(File.join(dir, "**/*.{jpg,png}")).each do |file|
30
+ assert sizes[file] > open(file, "rb").size, "should have reduced the file size"
31
+ end
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_optim_rake
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Thiel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: image_optim
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: progressbar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.20.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.20.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: testem
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Provide a little helper command to minify images
84
+ email:
85
+ - jonasthiel@googlemail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - image_optim_rake.gemspec
96
+ - lib/image_optim_rake.rb
97
+ - lib/image_optim_rake/image.rb
98
+ - lib/image_optim_rake/image_optim_task.rb
99
+ - lib/image_optim_rake/minifier.rb
100
+ - lib/image_optim_rake/railtie.rb
101
+ - lib/image_optim_rake/version.rb
102
+ - test/fixtures/unprocessed.jpg
103
+ - test/fixtures/unprocessed.png
104
+ - test/helper.rb
105
+ - test/units/image_test.rb
106
+ - test/units/minifier_test.rb
107
+ homepage: https://github.com/jnbt/image_optim_rake
108
+ licenses: []
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.0
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Provide a little helper command to minify images
130
+ test_files:
131
+ - test/fixtures/unprocessed.jpg
132
+ - test/fixtures/unprocessed.png
133
+ - test/helper.rb
134
+ - test/units/image_test.rb
135
+ - test/units/minifier_test.rb