10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/banhpho/cli.rb', line 10
def compress(dir)
abort("Error: command pngquant is not found, please download it from https://pngquant.org/ and put it in your global path") unless command?("pngquant")
abort("Error: argument '#{dir}' is not a valid directory") unless File.directory?(dir)
originalSize = 0
newSize = 0
imgs = []
compressInDir(dir) do |f|
imgs << f
originalSize += File.size(f)
end
bar = ProgressBar.new(imgs.size)
imgs.each do |f|
%x(pngquant --force --ext .png --skip-if-larger -- #{f})
bar.increment!
newSize += File.size(f)
end
count = imgs.size
if count > 0
rows = []
rows << ['Number of files', count]
rows << ['Original size', prettyFileSize(originalSize)]
rows << ['New size', prettyFileSize(newSize)]
rows << ['Compression ratio', "#{(1 - newSize.to_f / originalSize).round(4) * 100}%"]
report = Terminal::Table.new :title => 'Task completed', :rows => rows
puts report
end
end
|