Automatically reduce image sizes with smushit

UPDATE: lossless image size reduction of whole folders

smushit.com reduces image sizes dramatically, lossless!
Great for logos/icons…, but does not work for gifs!

I put together a small script to automate smushing (as long as i cannot find a popper API).

Usage
ruby smush.rb http://my.file.com/123.png where/to/save.png

Install
requires ruby and rubygems

sudo gem install json

Store this into smush.rb

optional(if this task is to full/gets blocked):
cange the &task=89266837334214400 to something else or use random

#smush.rb
def store_smushed_image(url,file)
  File.open(file,'w') do |f|
    f.puts smushed_image_data_for(url)
  end
end

def smushed_image_data_for(url)
  require 'cgi'
  url = CGI.escape url
  
  require 'net/http'
  require 'rubygems'
  require 'json'
  
  http = Net::HTTP.new('smushit.com')
  path = "/ws.php?img=#{url}&task=89266837334214400&id=paste2"
  
  resp, data = http.get(path, nil)
  raise "oops #{resp}" unless resp.is_a? Net::HTTPOK
  
  path = "/#{JSON.parse(data)['dest']}"
  resp, data = http.get(path, nil)
  data
end

#http://smushit.com/ws.php?img=http%3A%2F%2Fwww.famfamfam.com%2Flab%2Ficons%2Fsilk%2Ficons%2Fdrink_empty.png&task=89266837334214400&id=paste2
url = ARGV[0] || "http://www.famfamfam.com/lab/icons/silk/icons/drink_empty.png"
file = ARGV[1] || 'out.png'
store_smushed_image(url,file)

2 thoughts on “Automatically reduce image sizes with smushit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s