Created
October 19, 2011 11:10
-
-
Save meleyal/1297996 to your computer and use it in GitHub Desktop.
Simple png + jpg optimization script using optipng + jpegtran
This file contains 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/ruby | |
# Instructions | |
# - Install optipng + libjpeg (http://mxcl.github.com/homebrew/) | |
# - Put this file in a directory of images | |
# - Open in TextMate + run it (cmd+r) | |
# Config | |
dir = File.dirname(__FILE__) | |
optipng = "/usr/local/bin/optipng" | |
jpegtran = "/usr/local/bin/jpegtran" | |
png = ".png" | |
jpg = ".jpg" | |
suffix = "-opt" | |
replace = false | |
# Optimize PNGs | |
p "Optimizing .pngs in #{dir}" | |
Dir.glob(dir + "/**/*#{png}").each do |f| | |
d = File.dirname(f) | |
n = "#{File.basename(f,File.extname(f))}#{suffix}#{png}" | |
o = "#{d}/#{n}" | |
if replace | |
p "- #{f}" | |
`#{optipng} #{f}` | |
else | |
p "- #{f} > #{o}" | |
`#{optipng} #{f} -out #{o}` | |
end | |
end | |
# Optimize JPGs | |
p "Optimizing .jpgs in #{dir}" | |
Dir.glob(dir + "/**/*#{jpg}").each do |f| | |
d = File.dirname(f) | |
n = "#{File.basename(f,File.extname(f))}#{suffix}#{jpg}" | |
o = "#{d}/#{n}" | |
if replace | |
p "- #{f}" | |
`#{jpegtran} -optimize #{f}` | |
else | |
p "- #{f} > #{o}" | |
`#{jpegtran} -optimize -outfile #{o} #{f}` | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment