squeezem 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +8 -1
- data/lib/squeezem.rb +59 -11
- metadata +4 -4
data/README
CHANGED
@@ -2,9 +2,16 @@ List pngs which are bigger than they need to be. Can optionally
|
|
2
2
|
compress them, but is designed mainly to keep an eye on a tree of
|
3
3
|
images to make sure they stay in good shape.
|
4
4
|
|
5
|
+
Prerequisites
|
6
|
+
|
7
|
+
At least one of pngcrush or jpegran.
|
8
|
+
|
9
|
+
On Mac:
|
10
|
+
|
11
|
+
sudo port install pngcrush jpeg
|
12
|
+
|
5
13
|
Install
|
6
14
|
|
7
|
-
install pngcrush
|
8
15
|
sudo gem install squeezem
|
9
16
|
|
10
17
|
Usage
|
data/lib/squeezem.rb
CHANGED
@@ -7,6 +7,7 @@ require 'fileutils'
|
|
7
7
|
class Squeezem
|
8
8
|
def initialize(options)
|
9
9
|
@options = options
|
10
|
+
find_helpers
|
10
11
|
@total_bytes = 0
|
11
12
|
@saved_bytes = 0
|
12
13
|
@files_seen = 0
|
@@ -25,6 +26,22 @@ class Squeezem
|
|
25
26
|
}
|
26
27
|
end
|
27
28
|
|
29
|
+
def find_helpers
|
30
|
+
@helpers = {}
|
31
|
+
@helpers[:png] = 1 if command_exists?("pngcrush")
|
32
|
+
@helpers[:jpg] = 1 if command_exists?("jpegtran")
|
33
|
+
if @helpers.size == 0
|
34
|
+
$stderr.puts "Can't find any helpers - please install at least one of pngcrush or jpegtran!"
|
35
|
+
exit 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def command_exists?(command)
|
40
|
+
system("which #{command} >/dev/null 2>/dev/null")
|
41
|
+
return false if $?.exitstatus == 127
|
42
|
+
return true
|
43
|
+
end
|
44
|
+
|
28
45
|
def make_output_dir
|
29
46
|
begin
|
30
47
|
Dir.mktmpdir
|
@@ -38,7 +55,11 @@ class Squeezem
|
|
38
55
|
end
|
39
56
|
|
40
57
|
def squeeze(path)
|
41
|
-
|
58
|
+
file_type = get_file_type(path)
|
59
|
+
unless valid_file_type?(file_type)
|
60
|
+
@files_ignored += 1
|
61
|
+
return
|
62
|
+
end
|
42
63
|
@files_seen += 1
|
43
64
|
@path = path
|
44
65
|
@size = File.size(path)
|
@@ -52,10 +73,7 @@ class Squeezem
|
|
52
73
|
end
|
53
74
|
end
|
54
75
|
end
|
55
|
-
output =
|
56
|
-
Open3.popen3('pngcrush', '-quiet', '-rem', 'alla', '-reduce', '-brute', path, @output_path) do |stdin, stdout, stderr|
|
57
|
-
output = stdout.read
|
58
|
-
end
|
76
|
+
output = process_file(path, file_type)
|
59
77
|
if File.exist?(@output_path)
|
60
78
|
new_size = File.size(@output_path)
|
61
79
|
if new_size == 0
|
@@ -64,19 +82,49 @@ class Squeezem
|
|
64
82
|
end
|
65
83
|
saving = @size - new_size
|
66
84
|
record_saving(saving)
|
67
|
-
@files[canonical_path] = OpenStruct.new(:size => @size, :saving => saving)
|
68
85
|
keep_or_remove_output
|
86
|
+
if @options.squeezem
|
87
|
+
cache_saving = 0
|
88
|
+
cache_size = new_size
|
89
|
+
else
|
90
|
+
cache_saving = saving
|
91
|
+
cache_size = @size
|
92
|
+
end
|
93
|
+
@files[canonical_path] = OpenStruct.new(:size => cache_size, :saving => cache_saving)
|
69
94
|
else
|
70
95
|
$stderr.puts "Error processing #{path}:", output
|
71
96
|
end
|
72
97
|
end
|
73
98
|
|
74
|
-
def
|
75
|
-
|
76
|
-
|
77
|
-
return
|
99
|
+
def get_file_type(path)
|
100
|
+
extension = File.extname(path).sub('.', '')
|
101
|
+
if extension.empty?
|
102
|
+
return nil
|
103
|
+
else
|
104
|
+
return extension.to_sym
|
78
105
|
end
|
79
|
-
|
106
|
+
end
|
107
|
+
|
108
|
+
def valid_file_type?(type)
|
109
|
+
@helpers[type]
|
110
|
+
end
|
111
|
+
|
112
|
+
def process_file(path, file_type)
|
113
|
+
output = ''
|
114
|
+
case file_type
|
115
|
+
when :png
|
116
|
+
Open3.popen3('pngcrush', '-quiet', '-rem', 'alla', '-reduce', '-brute', path, @output_path) do |stdin, stdout, stderr|
|
117
|
+
output = stdout.read
|
118
|
+
end
|
119
|
+
when :jpg
|
120
|
+
File.open(@output_path, "w") do |out|
|
121
|
+
Open3.popen3('jpegtran', '-copy', 'none', '-optimize', '-perfect', path) do |stdin, stdout, stderr|
|
122
|
+
out.write(stdout.read)
|
123
|
+
output = stderr.read
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
return output
|
80
128
|
end
|
81
129
|
|
82
130
|
def keep_or_remove_output
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: squeezem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Steve Woodcock
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-24 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|