How to Install ImageMagick from Source on OS X

Posted on January 31, 2008

First grab the source:

wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz

Unarchive it:

tar xvzf ImageMagick.tar.gz

The old ./configure / make / sudo make install ritual:

cd ImageMagick-6.3.8
# Or whichever the current version is, of course.
./configure
make
sudo make install

You should be good to go. Lately I’ve been having luck with MiniMagick (all I need to do is crop/resize for this particular project).

Type this to make sure you can use ImageMagick from the command line at least:

convert -version

I love that ruby (and many scripting languages) make it so easily to “shell out” to scripts (as minimagick does). It really makes ruby performance alarmists look bad when shelling out to time-tested, battle-hardened C-based scripts is so easy, and works so well. (I’ve had success shelling out to the following in many apps: curl, imagemagick, wget, etc)

How to Use MiniMagick in your Rails App

Grab the gem:

sudo gem install mini_magick

Drop this in your config/environment.rb:

require 'rubygems'
gem 'mini_magick'
require 'mini_magick'

Example usage:

class Pic < ActiveRecord::Base

  # Where size is a string like '90x90', '300x200', etc
  def create_perfect_thumbnail(size)

    image = MiniMagick::Image.from_file(self.pic_path)
    height, width = image['height'].to_f, image['width'].to_f

    # FIRST SHAVE off some of the image to make it square
    if width < height
      shave = ((height - width)/2).round
      image.shave("0x#{shave}")
    else
      shave = ((width - height)/2).round
      image.shave("#{shave}x0")
    end

    image.resize(size)
    image.write(self.pic_path(size))

    # I had issues on my linux box with the pic not being readable by the web server,
    #   following the resize.  Set permissions o+r to fix this.    
    if RAILS_ENV == 'production' # Set permissions to o+r
      `chmod o+r #{self.pic_path(size)}`
    end

  end

  def pic_path(size)
    # Just an example -- I normally group pics by user_id under a public static dir.
    File.join(RAILS_ROOT, 'public', 'static', "#{size}_#{self.original_filename}") 
  end

end

That will shave off some of the pic, making a munged square from the original, before proceeding to make square thumbnails from that.

Fixing 'Address already in use' Errors in Mongrel

Posted on December 18, 2007

This problem was truly maddening. Some kind of rogue tcp process spawned by mongrel was not getting closed properly.

It was truly a ghost in the machine.

Doing a ‘ps aux | grep ruby’ or ‘ps aux | grep 3000’ yielded nothing. So it was darn neigh impossible to find a process to kill that was somehow still holding onto the socket. Attempting to start mongrel yielded the “Address already in use – bind(2) (Errno::EADDRINUSE)” error.

This link provided the answer.

If you normally run your mongrel(s) on port 3000, use the following snippet to kill em dead:

kill -HUP `lsof -t -i TCP:3000`

The following is the complete stack trace:

** Ruby version is not up-to-date; loading cgi_multipart_eof_fix
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
Exiting
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/tcphack.rb:12:in `initialize_without_backlog': Address already in use - bind(2) (Errno::EADDRINUSE)
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/tcphack.rb:12:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel.rb:92:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/configurator.rb:139:in `listener'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/bin/mongrel_rails:99:in `cloaker_'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/configurator.rb:50:in `initialize'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/bin/mongrel_rails:84:in `run'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/lib/mongrel/command.rb:212:in `run'
        from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.1/bin/mongrel_rails:281
         ... 6 levels...

Good luck!

Change Network Location via Commandline on OS X

Posted on October 10, 2007
If you use your MacBook on multiple network locations, this commandline shortcut is pretty handy:
$ scselect 'Home'
CurrentSet updated to 776E6BC6-B046-4D34-9465-7E9884A1E9DB (Home)
$ scselect 'Work'
...

Quick Hack: Recursively Remove .svn Directories

Posted on September 06, 2007

This snippet comes in handy when you’re copying folders between projects and inadvertently copy over .svn folders:

find . -type d -name .svn | xargs rm -rf

This will recursively find .svn directories in your current path and kill em dead!

And an alias for your ~/.bash_login file:

alias clean_svn="find . -type d -name .svn | xargs rm -rf"

HOWTO: SSH Seamlessly Between OS X and Remote Linux (No Password/phrase Prompt)

Posted on August 11, 2007

Note: security freaks, please do not read the following. It might make your eyes bleed. (or, feel free to propose an alternate solution in the comments)

The main potential security implication is:

If your mac is somehow compromised (an attacker gains access to your private keyfile located at ~/.ssh/id_dsa) then you will need to quickly kill access to that keypair on your remote servers.

Other than that, this setup is pretty slick if UR SSHN TO LOTS OF LEENUX BOXEN. Ahem.

Generate the Keypair Locally

On your OS X machine:

  ssh-keygen -b 1024 -t dsa

Just hit enter for all of the defaults, including a BLANK passphrase. (otherwise you would have to enter it each time you SSH’d to a remote box, thereby defeating this whole exercise, no?)

Ensure Remote Server has a ~/.ssh/ folder

  ssh deploy@remote.com
  mkdir ~/.ssh/

Next copy ~/.ssh/id_dsa.pub to the remote server. Example:

  scp ~/.ssh/id_dsa.pub deploy@remote.com:.ssh/authorized_keys

Try logging in sans password:

  ssh deploy@remote.com

It should now let you in without entering a password or passphrase!

Set .ssh Permissions on Remote Linux Server

On the remote linux box, do a:

  chmod 700 ~/.ssh
  chmod 600 ~/.ssh/*


Enabling this setup for other Linux Boxes

Do NOT overwrite your existing key pair (default locations) with ‘ssh-keygen’ on OS X because then all your other servers will not recognize the new key!

You can generate a new pair for each separate linux box you want to connect to, just follow the previous ssh-keygen instructions but place the output in a new location.

To use the same public/private keypair on each box (say it’s just you who’ll be admin’ing em):

  scp ~/.ssh/id_dsa.pub foo@another-server.com:.ssh/authorized_keys

Again, Try Logging in Sans Password

  ssh foo@another-server.com

Should now let you in without entering a password or passphrase!

You also might need to follow the directory setup/permissions guide as outlined above.