Skip to content

Instantly share code, notes, and snippets.

@arlimus
Created August 21, 2013 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arlimus/6297400 to your computer and use it in GitHub Desktop.
Save arlimus/6297400 to your computer and use it in GitHub Desktop.
Minify images (jpg/png) in the current folder to smaller sizes (up to ~350k). Example run: > minify -- converting with quality 70, resize 100% 134 (25).png -- converting with quality 70, resize 100% -- converting with quality 70, resize 84% 144 .jpeg > ll minified -rw-r--r-- 1 zhaery users 130K 21. Aug 19:33 134 (25).png.jpg -rw-r--r-- 1 zhaery …
#!/usr/bin/env ruby
require 'fileutils'
threshold = 350e3
$output = "minified"
$quality = 70
FileUtils.mkdir_p $output
def conv org, factor = 1.0
puts "-- converting with quality #{$quality}, resize #{(factor*100).round}%"
`convert "#{org}" -quality #{$quality} -resize #{(factor*100).round}% "#{$output}/#{org}.jpg"`
end
Dir["*"].find_all do |c|
c.downcase =~ /\.(jpg|png|jpeg)$/
end.map do |c|
[c,File::stat(c)]
end.find_all do |c,s|
s.file? and s.size > threshold
end.each do |c,s|
conv c
rs = File::stat "#{$output}/#{c}.jpg"
if rs.size > threshold
factor = rs.size / threshold
normf = 1 / Math::sqrt(factor)
conv c, normf
end
puts "\e[32;1m#{c}\e[0m"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment