-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtinify
executable file
·46 lines (41 loc) · 1.31 KB
/
tinify
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
#!/usr/bin/env ruby
require 'optparse'
require 'tinify'
Tinify.key = "hLrYlp3sHGqVqHkoODtopYZyAQWUnoho"
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: tinify [options]"
opts.on("-h", "--height HEIGHT", OptionParser::DecimalInteger, "Set image height") do |height|
options[:height] = height
options[:width] = 9999 unless options[:width]
end
opts.on("-w", "--width WIDTH", OptionParser::DecimalInteger, "Set image width") do |width|
options[:width] = width
options[:height] = 9999 unless options[:height]
end
opts.on("-?", "--help", "Prints this help") do
puts opts
exit
end
end.parse!
ARGV.each { |file|
print "Processing file: #{file}... "
STDOUT.flush
current_file_size = File.new(file).size
source = Tinify.from_file(file)
if options[:width] || options[:height]
# puts "... resizing"
source = source.resize(
method: "fit",
width: options[:width],
height: options[:height]
)
end
source.to_file("#{file}~~")
new_file_size = File.new("#{file}~~").size
percent_savings = (current_file_size-new_file_size)*100/current_file_size
puts "savings #{percent_savings}% (#{current_file_size-new_file_size} bytes)"
File.rename(file, "#{file}~")
File.rename("#{file}~~", file)
}
puts "Compressions this month: #{Tinify.compression_count}"