Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions bin/tinypng
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

def usage
puts "tinypng input.png output.png"
puts "or"
puts "tinypng input/directory output/directory"
end

def shrink(input, output)
client = TinyPNG::Client.new
begin
image = client.shrink(File.open(input) { |file| file.read })
rescue TinyPNG::Exception => e
puts e
exit 1
end

FileUtils.cp(image.to_file, output)
end

input = ARGV[0]
Expand All @@ -14,14 +28,12 @@ end

$LOAD_PATH << File.expand_path(File.join(__FILE__, '../../lib'))
require 'tinypng'
require 'fileutils'

client = TinyPNG::Client.new
begin
image = client.shrink(File.open(input) { |file| file.read })
rescue TinyPNG::Exception => e
puts e
exit 1
if File::ftype(input) == 'directory'
Dir::glob(File.join(input, '/*.png')).each do |f|
shrink(f, File.join(output, File.basename(f)))
end
else
shrink(input, output)
end

require 'fileutils'
FileUtils.cp(image.to_file, output)