Automatic Lossless Reduction Of All Your Websites Images

NOTE: If you are on windows you can leave now.

  • LOSSLESS size reduction (10-97% size reduction) in the cloud
  • optmizes all images(jpg+png) from a given folder

Install:
sudo gem install smusher
#if you do not have get ruby + rubygems first
See Install instructions

Usage:
smusher /my_app/public/images

smusher LOCAL_FOLDER or IMAGE

Output:
smushing /my_app/public/images/cart_small.png
3322 -> 537                              = 16%

smushing /my_app/public/images/pro_icon.png
3022 -> 260                              = 8%

smushing /my_app/public/images/message.png
2898 -> 138                              = 4%

smushing /my_app/public/images/logo.png
6799 -> 9677                             = 142%
reverted!

Protection
As you see images that get larger or empty or cannot be processed are reverted.

Numbers for Humans – Humanize for Numeric

number_with_precision(result.round(2)) — BE GONE!

Usage
1.humanize == “1”
1000000.humanize == “1.000.000”
1000.12345.humanize == “1.000,12”

Install

#config/initializers/numeric_humanize.rb
class Numeric
  def humanize(rounding=2,delimiter=',',separator='.')
    value = respond_to?(:round_with_precision) ? round(rounding) : self
    
    #see number with delimeter
    parts = value.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
    parts.join separator
  end
end

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)

Fixing GetText Gem

Update:Fast Gettext gem has resolved all major problems of GetText 😉

Update: gettext 1.92.0 has fixed this issue, update if you can…

Just got a surprising:
NoMethodError (undefined method `file_exists?’ for File:Class):
gettext/rails.rb:281

281c281
<       return render_file_without_locale(localized_path, use_full_path, local_assigns) if file_exists?(localized_path)
---
>       return render_file_without_locale(localized_path, use_full_path, local_assigns) if (respond_to?(:finder) ? finder.file_exists?(localized_path) : file_exists?(localized_path))

put it in a gettext.patch and then:

sudo patch /usr/lib/ruby/gems/1.8/gems/gettext-1.91.0/lib/gettext/rails.rb < gettext.patch

Cause of failure: file_exists?(=cached checking if a file exists) was moved to finder in Rails 2.1
Alternatively one could monkeypatch rails to make it work for Rails 2.1

Beware of Dir.mkdir Permission Handling

Took me 1 hours to figure out Dir.mkdir’s permission handling is broken ?

micha@ubuntu:/home/data/test$ ruby -e "Dir.mkdir 'wtf',0777"
micha@ubuntu:/home/data/test$ ls -al
drwxr-xr-x 2 micha   micha 4096 2008-02-24 13:07 wtf
micha@ubuntu:/home/data/test$ ruby -e "File.chmod 0777,'wtf'"
micha@ubuntu:/home/data/test$ ls -al
drwxrwxrwx 2 micha   micha 4096 2008-02-24 13:07 wtf

Dir: 0777 = drwxr-xr-x
File: 0777 = drwxrwxrwx
WTF?

And to not make it a short post, i add some recursive directory creating 😀

  #simple recursive dir creation
  FileUtils.mkdir_p dir

  #create a directory recursive and set rights/group
  def self.mkdir_r path, permission=0770,group_id=false
    path.split('/').inject(path =~ /^\// ? '/':'') do |root,part|
      path = root+'/'+part
      unless File.exist? path
        Dir.mkdir(path, permission)
        #hack, mkdir does not set the permisson right !?
        File.chmod permission,path
        File.chown(-1,group_id,path) if group_id
      end
      path
    end
  end