grosser-smusher 0.3.3 → 0.3.4
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.markdown +15 -9
- data/VERSION.yml +4 -0
- data/lib/smusher.rb +12 -11
- data/spec/images/ad.gif +0 -0
- data/spec/out/ad.gif +0 -0
- data/spec/smusher_spec.rb +28 -17
- data/spec/spec_helper.rb +4 -10
- metadata +29 -30
- data/Rakefile +0 -21
- data/smusher.gemspec +0 -106
data/README.markdown
CHANGED
@@ -11,15 +11,17 @@ Solution
|
|
11
11
|
|
12
12
|
Install
|
13
13
|
=======
|
14
|
-
install ruby + rubygems
|
14
|
+
install ruby + rubygems
|
15
|
+
sudo gem install httpclient
|
15
16
|
sudo gem install grosser-smusher --source http://gems.github.com/
|
16
17
|
|
17
18
|
Usage
|
18
19
|
=====
|
19
20
|
Optimize a single image or a whole folder in the cloud.
|
20
21
|
|
21
|
-
converting gif
|
22
|
-
|
22
|
+
converting gif-s to png-s:
|
23
|
+
|
24
|
+
- called with a folder gif-s will not be converted
|
23
25
|
- called on a singe .gif, it will be converted if it is optimizeable
|
24
26
|
|
25
27
|
Usage:
|
@@ -39,13 +41,13 @@ or is empty will not be saved.
|
|
39
41
|
Example
|
40
42
|
======
|
41
43
|
smusher /apps/ts/public/images
|
42
|
-
|
44
|
+
smushing /apps/rs/public/images/social/facebook_icon.png
|
43
45
|
2887 -> 132 = 4%
|
44
46
|
|
45
|
-
|
47
|
+
smushing /apps/rs/public/images/social/myspace_icon.png
|
46
48
|
3136 -> 282 = 8%
|
47
49
|
|
48
|
-
|
50
|
+
smushing /apps/rs/public/images/dvd/dvd_1.png
|
49
51
|
5045 -> 4 = 0%
|
50
52
|
reverted!
|
51
53
|
...
|
@@ -53,11 +55,15 @@ Example
|
|
53
55
|
TODO
|
54
56
|
====
|
55
57
|
- only optimize 'new' images -> save time when doing on each deploy
|
56
|
-
-
|
57
|
-
-
|
58
|
+
- support wildcars like `smusher images/*.png` ?
|
59
|
+
- convert gifs to png, even if the new size is the same, for consistency (atm only those which get smaller are converted)
|
60
|
+
|
61
|
+
ALTERNATIVES
|
62
|
+
============
|
63
|
+
If you want to lossless reduce images and minify css + js, try [reduce](http://github.com/grosser/reduce).
|
58
64
|
|
59
65
|
Author
|
60
66
|
======
|
61
67
|
Michael Grosser
|
62
68
|
grosser.michael@gmail.com
|
63
|
-
Hereby placed under public domain, do what you want, just do not hold me accountable...
|
69
|
+
Hereby placed under public domain, do what you want, just do not hold me accountable...
|
data/VERSION.yml
ADDED
data/lib/smusher.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
require 'json'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'httpclient'
|
4
6
|
|
5
7
|
module Smusher
|
6
8
|
extend self
|
@@ -13,7 +15,7 @@ module Smusher
|
|
13
15
|
check_options(options)
|
14
16
|
puts "THIS FILE IS EMPTY!!! #{file}" and return if size(file).zero?
|
15
17
|
success = false
|
16
|
-
|
18
|
+
|
17
19
|
with_logging(file,options[:quiet]) do
|
18
20
|
write_optimized_data(file)
|
19
21
|
success = true
|
@@ -44,14 +46,14 @@ private
|
|
44
46
|
|
45
47
|
def write_optimized_data(file)
|
46
48
|
optimized = optimized_image_data_for(file)
|
47
|
-
|
49
|
+
|
48
50
|
raise "Error: got larger" if size(file) < optimized.size
|
49
51
|
raise "Error: empty file downloaded" if optimized.size < MINIMUM_IMAGE_SIZE
|
50
52
|
raise "cannot be optimized further" if size(file) == optimized.size
|
51
|
-
|
53
|
+
|
52
54
|
File.open(file,'w') {|f| f.puts optimized}
|
53
55
|
end
|
54
|
-
|
56
|
+
|
55
57
|
def sanitize_folder(folder)
|
56
58
|
folder.sub(%r[/$],'')#remove tailing slash
|
57
59
|
end
|
@@ -63,18 +65,18 @@ private
|
|
63
65
|
images.map! {|ext| "#{folder}/**/*.#{ext}"}
|
64
66
|
FileList[*images]
|
65
67
|
end
|
66
|
-
|
68
|
+
|
67
69
|
def size(file)
|
68
70
|
File.exist?(file) ? File.size(file) : 0
|
69
71
|
end
|
70
72
|
|
71
73
|
def with_logging(file,quiet)
|
72
74
|
puts "smushing #{file}" unless quiet
|
73
|
-
|
75
|
+
|
74
76
|
before = size(file)
|
75
77
|
begin; yield; rescue; puts $! unless quiet; end
|
76
78
|
after = size(file)
|
77
|
-
|
79
|
+
|
78
80
|
unless quiet
|
79
81
|
result = "#{(100*after)/before}%"
|
80
82
|
puts "#{before} -> #{after}".ljust(40) + " = #{result}"
|
@@ -83,11 +85,10 @@ private
|
|
83
85
|
end
|
84
86
|
|
85
87
|
def optimized_image_data_for(file)
|
86
|
-
|
87
|
-
response = JSON.parse(`curl -F files[]=@#{file} http://smush.it/ws.php -s`)
|
88
|
+
response = JSON.parse((HTTPClient.post 'http://smush.it/ws.php', { 'files[]' => File.new(file) }).body.content)
|
88
89
|
raise "smush.it: #{response['error']}" if response['error']
|
89
90
|
path = response['dest']
|
90
91
|
raise "no dest path found" unless path
|
91
|
-
|
92
|
+
open("http://smush.it/#{path}") { |source| source.read() }
|
92
93
|
end
|
93
|
-
end
|
94
|
+
end
|
data/spec/images/ad.gif
ADDED
Binary file
|
data/spec/out/ad.gif
ADDED
Binary file
|
data/spec/smusher_spec.rb
CHANGED
@@ -9,17 +9,18 @@ describe :smusher do
|
|
9
9
|
def size
|
10
10
|
File.size(@file)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
before do
|
14
14
|
#prepare output folder
|
15
15
|
@out = File.join(ROOT,'out')
|
16
16
|
FileUtils.rm_r @out, :force=>true
|
17
17
|
FileUtils.mkdir @out
|
18
18
|
copy 'people.jpg'
|
19
|
-
|
19
|
+
copy 'ad.gif'
|
20
|
+
|
20
21
|
@file = File.join(@out,'people.jpg')
|
21
22
|
end
|
22
|
-
|
23
|
+
|
23
24
|
describe :optimize_image do
|
24
25
|
it "stores the image in an reduced size" do
|
25
26
|
original_size = size
|
@@ -47,18 +48,28 @@ describe :smusher do
|
|
47
48
|
Smusher.optimize_image(@file)
|
48
49
|
size.should == original_size
|
49
50
|
end
|
50
|
-
|
51
|
+
|
51
52
|
describe "gif handling" do
|
52
53
|
before do
|
53
54
|
copy 'logo.gif'
|
54
55
|
@file = File.join(@out,'logo.gif')
|
55
56
|
@file_png = File.join(@out,'logo.png')
|
56
57
|
end
|
58
|
+
|
59
|
+
pending_it "converts gifs to png even if they have the same size" do
|
60
|
+
copy 'ad.gif'
|
61
|
+
file = File.join(@out,'ad.gif')
|
62
|
+
original_size = size
|
63
|
+
Smusher.optimize_image(file)
|
64
|
+
File.size(File.join(@out,'ad.png')).should == original_size
|
65
|
+
end
|
66
|
+
|
57
67
|
it "stores converted .gifs in .png files" do
|
58
68
|
Smusher.optimize_image(@file)
|
59
69
|
File.exist?(@file).should == false
|
60
70
|
File.exist?(@file_png).should == true
|
61
71
|
end
|
72
|
+
|
62
73
|
it "does not rename gifs, if optimizing failed" do
|
63
74
|
Smusher.expects(:optimized_image_data_for).returns File.read(@file)
|
64
75
|
Smusher.optimize_image(@file)
|
@@ -66,7 +77,7 @@ describe :smusher do
|
|
66
77
|
File.exist?(@file_png).should == false
|
67
78
|
end
|
68
79
|
end
|
69
|
-
|
80
|
+
|
70
81
|
describe 'options' do
|
71
82
|
it "does not produce output when :quiet is given" do
|
72
83
|
$stdout.expects(:write).never
|
@@ -78,7 +89,7 @@ describe :smusher do
|
|
78
89
|
end
|
79
90
|
end
|
80
91
|
end
|
81
|
-
|
92
|
+
|
82
93
|
describe :optimize_images_in_folder do
|
83
94
|
before do
|
84
95
|
FileUtils.rm @file
|
@@ -87,9 +98,9 @@ describe :smusher do
|
|
87
98
|
copy image_name
|
88
99
|
@files << File.join(@out,image_name)
|
89
100
|
end
|
90
|
-
@before = @files.map {|f|File.size(f)}
|
101
|
+
@before = @files.map {|f|File.size(f)}
|
91
102
|
end
|
92
|
-
|
103
|
+
|
93
104
|
it "optimizes all images" do
|
94
105
|
Smusher.optimize_images_in_folder(@out)
|
95
106
|
new_sizes = @files.map {|f|File.size(f)}
|
@@ -108,17 +119,17 @@ describe :smusher do
|
|
108
119
|
File.exist?(File.join(@out,'logo.png')).should == true
|
109
120
|
end
|
110
121
|
end
|
111
|
-
|
122
|
+
|
112
123
|
describe :sanitize_folder do
|
113
124
|
it "cleans a folders trailing slash" do
|
114
125
|
Smusher.send(:sanitize_folder,"xx/").should == 'xx'
|
115
126
|
end
|
116
|
-
|
127
|
+
|
117
128
|
it "does not clean if there is no trailing slash" do
|
118
129
|
Smusher.send(:sanitize_folder,"/x/ccx").should == '/x/ccx'
|
119
130
|
end
|
120
131
|
end
|
121
|
-
|
132
|
+
|
122
133
|
describe :images_in_folder do
|
123
134
|
it "finds all non-gif images" do
|
124
135
|
folder = File.join(ROOT,'images')
|
@@ -126,22 +137,22 @@ describe :smusher do
|
|
126
137
|
result = Smusher.send(:images_in_folder,folder)
|
127
138
|
(all+result).uniq.size.should == all.size
|
128
139
|
end
|
129
|
-
|
140
|
+
|
130
141
|
it "finds nothing if folder is empty" do
|
131
142
|
Smusher.send(:images_in_folder,File.join(ROOT,'empty')).should == []
|
132
143
|
end
|
133
144
|
end
|
134
|
-
|
145
|
+
|
135
146
|
describe :size do
|
136
147
|
it "find the size of a file" do
|
137
148
|
Smusher.send(:size,@file).should == File.size(@file)
|
138
149
|
end
|
139
|
-
|
150
|
+
|
140
151
|
it "returns 0 for missing file" do
|
141
152
|
Smusher.send(:size,File.join(ROOT,'xxxx','dssdfsddfs')).should == 0
|
142
153
|
end
|
143
154
|
end
|
144
|
-
|
155
|
+
|
145
156
|
describe :logging do
|
146
157
|
it "yields" do
|
147
158
|
val = 0
|
@@ -149,7 +160,7 @@ describe :smusher do
|
|
149
160
|
val.should == 1
|
150
161
|
end
|
151
162
|
end
|
152
|
-
|
163
|
+
|
153
164
|
describe :optimized_image_data_for do
|
154
165
|
it "loads the reduced image" do
|
155
166
|
original = File.join(ROOT,'images','add.png')
|
@@ -158,4 +169,4 @@ describe :smusher do
|
|
158
169
|
received.should == reduced
|
159
170
|
end
|
160
171
|
end
|
161
|
-
end
|
172
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,15 +11,9 @@ Spec::Runner.configure do |config|
|
|
11
11
|
end
|
12
12
|
|
13
13
|
|
14
|
-
# ----
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
module Test
|
19
|
-
module Unit
|
20
|
-
def self.run?
|
21
|
-
true
|
22
|
-
end
|
23
|
-
end
|
14
|
+
# ---- Helpers
|
15
|
+
def pending_it(text,&block)
|
16
|
+
it text do
|
17
|
+
pending(&block)
|
24
18
|
end
|
25
19
|
end
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grosser-smusher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
|
-
cert_chain:
|
11
|
-
|
12
|
-
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-13 00:00:00 -07:00
|
13
|
+
default_executable: smusher
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rake
|
@@ -32,8 +33,8 @@ dependencies:
|
|
32
33
|
version: "0"
|
33
34
|
version:
|
34
35
|
- !ruby/object:Gem::Dependency
|
35
|
-
name:
|
36
|
-
type: :
|
36
|
+
name: httpclient
|
37
|
+
type: :runtime
|
37
38
|
version_requirement:
|
38
39
|
version_requirements: !ruby/object:Gem::Requirement
|
39
40
|
requirements:
|
@@ -41,44 +42,42 @@ dependencies:
|
|
41
42
|
- !ruby/object:Gem::Version
|
42
43
|
version: "0"
|
43
44
|
version:
|
44
|
-
description:
|
45
|
+
description:
|
45
46
|
email: grosser.michael@gmail.com
|
46
47
|
executables:
|
47
48
|
- smusher
|
48
49
|
extensions: []
|
49
50
|
|
50
|
-
extra_rdoc_files:
|
51
|
-
|
52
|
-
- lib/smusher.rb
|
53
|
-
- README.markdown
|
51
|
+
extra_rdoc_files: []
|
52
|
+
|
54
53
|
files:
|
54
|
+
- VERSION.yml
|
55
|
+
- README.markdown
|
55
56
|
- bin/smusher
|
56
57
|
- lib/smusher.rb
|
57
|
-
-
|
58
|
-
- spec/
|
59
|
-
- spec/
|
60
|
-
- spec/
|
58
|
+
- spec/out
|
59
|
+
- spec/out/ad.gif
|
60
|
+
- spec/out/people.jpg
|
61
|
+
- spec/empty
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
- spec/smusher_spec.rb
|
64
|
+
- spec/images
|
61
65
|
- spec/images/logo.gif
|
62
|
-
- spec/images/people.jpg
|
63
|
-
- spec/images/add.png
|
64
66
|
- spec/images/woman.jpeg
|
67
|
+
- spec/images/add.png
|
68
|
+
- spec/images/drink_empty.png
|
69
|
+
- spec/images/ad.gif
|
70
|
+
- spec/images/people.jpg
|
65
71
|
- spec/images/water.JPG
|
66
|
-
- spec/
|
67
|
-
- spec/
|
68
|
-
- spec/
|
69
|
-
- Manifest
|
70
|
-
- Rakefile
|
71
|
-
- smusher.gemspec
|
72
|
+
- spec/reduced
|
73
|
+
- spec/reduced/fam.png
|
74
|
+
- spec/reduced/add.png
|
72
75
|
has_rdoc: true
|
73
76
|
homepage: http://github.com/grosser/smusher
|
74
77
|
post_install_message:
|
75
78
|
rdoc_options:
|
76
|
-
- --line-numbers
|
77
79
|
- --inline-source
|
78
|
-
- --
|
79
|
-
- Smusher
|
80
|
-
- --main
|
81
|
-
- README.markdown
|
80
|
+
- --charset=UTF-8
|
82
81
|
require_paths:
|
83
82
|
- lib
|
84
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -91,11 +90,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
90
|
requirements:
|
92
91
|
- - ">="
|
93
92
|
- !ruby/object:Gem::Version
|
94
|
-
version: "
|
93
|
+
version: "0"
|
95
94
|
version:
|
96
95
|
requirements: []
|
97
96
|
|
98
|
-
rubyforge_project:
|
97
|
+
rubyforge_project:
|
99
98
|
rubygems_version: 1.2.0
|
100
99
|
signing_key:
|
101
100
|
specification_version: 2
|
data/Rakefile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
desc "Run all specs in spec directory"
|
2
|
-
task :test do |t|
|
3
|
-
require 'spec'
|
4
|
-
options = "--colour --format progress --loadby --reverse"
|
5
|
-
files = FileList['spec/**/*_spec.rb']
|
6
|
-
system("spec #{options} #{files}")
|
7
|
-
end
|
8
|
-
|
9
|
-
#Gemspec
|
10
|
-
require 'echoe'
|
11
|
-
project_name = 'smusher'
|
12
|
-
Echoe.new(project_name , '0.3.3') do |p|
|
13
|
-
p.description = "Automatic Lossless Reduction Of All Your Images"
|
14
|
-
p.url = "http://github.com/grosser/#{project_name}"
|
15
|
-
p.author = "Michael Grosser"
|
16
|
-
p.email = "grosser.michael@gmail.com"
|
17
|
-
p.ignore_pattern = ["nbproject/*", "nbproject/*/*"]
|
18
|
-
p.dependencies = %w[rake json]
|
19
|
-
end
|
20
|
-
|
21
|
-
task :update_gemspec => [:manifest, :build_gemspec]
|
data/smusher.gemspec
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
|
2
|
-
# Gem::Specification for Smusher-0.3.3
|
3
|
-
# Originally generated by Echoe
|
4
|
-
|
5
|
-
--- !ruby/object:Gem::Specification
|
6
|
-
name: smusher
|
7
|
-
version: !ruby/object:Gem::Version
|
8
|
-
version: 0.3.3
|
9
|
-
platform: ruby
|
10
|
-
authors:
|
11
|
-
- Michael Grosser
|
12
|
-
autorequire:
|
13
|
-
bindir: bin
|
14
|
-
|
15
|
-
date: 2009-01-30 00:00:00 +01:00
|
16
|
-
default_executable:
|
17
|
-
dependencies:
|
18
|
-
- !ruby/object:Gem::Dependency
|
19
|
-
name: rake
|
20
|
-
type: :runtime
|
21
|
-
version_requirement:
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: "0"
|
27
|
-
version:
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: json
|
30
|
-
type: :runtime
|
31
|
-
version_requirement:
|
32
|
-
version_requirements: !ruby/object:Gem::Requirement
|
33
|
-
requirements:
|
34
|
-
- - ">="
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: "0"
|
37
|
-
version:
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: echoe
|
40
|
-
type: :development
|
41
|
-
version_requirement:
|
42
|
-
version_requirements: !ruby/object:Gem::Requirement
|
43
|
-
requirements:
|
44
|
-
- - ">="
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: "0"
|
47
|
-
version:
|
48
|
-
description: Automatic Lossless Reduction Of All Your Images
|
49
|
-
email: grosser.michael@gmail.com
|
50
|
-
executables:
|
51
|
-
- smusher
|
52
|
-
extensions: []
|
53
|
-
|
54
|
-
extra_rdoc_files:
|
55
|
-
- bin/smusher
|
56
|
-
- lib/smusher.rb
|
57
|
-
- README.markdown
|
58
|
-
files:
|
59
|
-
- bin/smusher
|
60
|
-
- lib/smusher.rb
|
61
|
-
- README.markdown
|
62
|
-
- spec/reduced/fam.png
|
63
|
-
- spec/reduced/add.png
|
64
|
-
- spec/images/drink_empty.png
|
65
|
-
- spec/images/logo.gif
|
66
|
-
- spec/images/people.jpg
|
67
|
-
- spec/images/add.png
|
68
|
-
- spec/images/woman.jpeg
|
69
|
-
- spec/images/water.JPG
|
70
|
-
- spec/smusher_spec.rb
|
71
|
-
- spec/out/people.jpg
|
72
|
-
- spec/spec_helper.rb
|
73
|
-
- Manifest
|
74
|
-
- Rakefile
|
75
|
-
- smusher.gemspec
|
76
|
-
has_rdoc: true
|
77
|
-
homepage: http://github.com/grosser/smusher
|
78
|
-
post_install_message:
|
79
|
-
rdoc_options:
|
80
|
-
- --line-numbers
|
81
|
-
- --inline-source
|
82
|
-
- --title
|
83
|
-
- Smusher
|
84
|
-
- --main
|
85
|
-
- README.markdown
|
86
|
-
require_paths:
|
87
|
-
- lib
|
88
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
-
requirements:
|
90
|
-
- - ">="
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
version: "0"
|
93
|
-
version:
|
94
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
-
requirements:
|
96
|
-
- - ">="
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
version: "1.2"
|
99
|
-
version:
|
100
|
-
requirements: []
|
101
|
-
|
102
|
-
rubyforge_project: smusher
|
103
|
-
rubygems_version: 1.3.1
|
104
|
-
specification_version: 2
|
105
|
-
summary: Automatic Lossless Reduction Of All Your Images
|
106
|
-
test_files: []
|