Created
January 26, 2016 08:58
-
-
Save cheerfulstoic/7707311c97b6fb440d80 to your computer and use it in GitHub Desktop.
TinyPNG script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'dotenv' | |
Dotenv.load | |
require 'tinify' | |
require 'json' | |
Tinify.key = ENV['TINYPNG_API_KEY'] | |
JSON_STORE = 'tinied.json' | |
tinied = File.exist?(JSON_STORE) ? JSON.load(File.read(JSON_STORE)) : [] | |
ARGV.each do |path| | |
if tinied.include?(path) | |
puts "Already shrunk #{path}" | |
else | |
puts "Shrinking #{path}" | |
size_before = File.size(path) | |
source = Tinify.from_file(path) | |
source.to_file(path) | |
size_after = File.size(path) | |
tinied << path | |
puts "Shrunk from #{size_before} bytes to #{size_after} bytes (#{(100.0 * size_after / size_before).round(2)}%)" | |
end | |
end | |
File.open(JSON_STORE, 'w') { |f| f << tinied.to_json } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment