From 7b929ce8fae2a5ff8fd0ddb90c7415960cbbb852 Mon Sep 17 00:00:00 2001 From: STAR_ZERO Date: Sun, 23 Jun 2013 12:14:24 +0900 Subject: [PATCH] Supports batch file in the directory --- bin/tinypng | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/bin/tinypng b/bin/tinypng index 8503ca9..a0cb09f 100755 --- a/bin/tinypng +++ b/bin/tinypng @@ -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] @@ -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)