-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_png.rb
58 lines (42 loc) · 1.42 KB
/
compress_png.rb
1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# encoding: UTF8
require_relative 'platform'
require 'find'
require 'colored'
require 'win32console' if ContentTasks::PLATFORM_IS_WINDOWS
base_dir = ARGV[0]
puts "Finding png files in #{base_dir}"
if File.exists?(base_dir) && File.directory?(base_dir)
kb_saved = 0
Find.find(base_dir) do |path|
if path =~ /\A([\-\.\:\w\s\b\/\\]+(\/|\\))([\.\-\w\b]+)\.png\z/
relative_path = path.slice(base_dir.length..-1)
print "Compressing #{relative_path.yellow}"
original_size = FileTest.size(path)
file_name = $3
dir_path = $1
new_path = "#{dir_path}#{file_name}.png-new"
compressed = system("pngcrush -q -rem alla -brute -e .png-new \"#{path}\"")
if compressed
new_size = FileTest.size(new_path)
compress_percentage = (1 - (new_size / original_size.to_f)) * 100.0
print " ( original_size: #{original_size.to_s.red}, new_size: #{new_size.to_s.green}, %compressed: #{("%.2f" % compress_percentage).green.bold}%)"
if original_size > new_size
kb_saved += original_size - new_size
deleted = File.delete(path) == 1
if deleted
File.rename(new_path, path)
puts "\u2713".green
end
else
puts "\u2715".red
end
else
puts "#{$?}".red
end
end
end
kb_saved /= 1000.0
puts "Saved %.2f KB" % kb_saved
else
puts "Path not found or it isn't a directory"
end