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.

Comments
  1. BillFebruary 18, 2008 @ 04:52 PM

    Hey, thanks for this post. I found it quite useful. I’m having one issue though. MiniMagick works fine from irb, but not from my rails app. No errors. Nothing in the log. It just craps out and causes my action to fail. Have you ever seen that? Any ideas what the cause could be? Thanks!

  2. BillFebruary 18, 2008 @ 04:56 PM

    Actually, I was able to find the error. It happens on MiniMagick::Image.from_file

    ImageMagick command (identify ”/tmp/minimagick54242-0.jpg”) failed: Error Given 32512

  3. BillFebruary 18, 2008 @ 09:12 PM

    So I figured out my problem on my own. I didn’t have any of the image libraries installed, i.e. libpng, libjpeg, etc. Basically for each image type you want to support, you need to install the image library BEFORE building the ImageMagick stuff. Granted, if you install the ImageMagick binaries, you might not have this problem.

  4. Shanti BrafordFebruary 18, 2008 @ 09:52 PM

    Hey Bill – not sure I follow exactly what you did, but glad you got it working eventually!

    ImageMagick can be a nasty one when it comes to the dependencies / compiling / etc.

  5. BillFebruary 20, 2008 @ 04:59 PM

    Basically I didn’t have any of the image processing libraries installed where ImageMagick could find them. I’m writing up a blog post on it so if you want to check my site at some point to check it out, feel free!