-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_jpg.rb
43 lines (31 loc) · 1.15 KB
/
compress_jpg.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
require_relative 'platform'
require 'find'
require 'colored'
require 'win32console' if PLATFORM_IS_WINDOWS
base_dir = ARGV[0]
puts "Finding jpg 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\/\\]+(\/|\\){1})([\-\.\w\b]+)\.jpg\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}.jpg"
compressed = system("jpegtran -copy none -optimize -perfect -outfile #{new_path} #{path}")
if compressed
new_size = FileTest.size(new_path)
compress_percentage = (1 - (new_size / original_size.to_f)) * 100.0
puts " ( original_size: #{original_size.to_s.red}, new_size: #{new_size.to_s.green}, %compressed: #{("%.2f" % compress_percentage).green.bold}%)"
kb_saved += original_size - new_size
else
puts "Error: #{$?}"
end
end
end
puts "Saved #{kb_saved / 1000.0} KB"
else
puts "Path not found or it isn't a directory"
end