Ruby on Rails Image Manipulation with ImageMagick and Mini-Magick on OS X

I’m working on a Ruby on Rails project that requires users to be able to upload images. At the same time, we want to resize images so that they appear as we want them to on site. To do this, I found that the best solution would be to use the Ruby Gem Mini-Magick which uses ImageMagick. The benefits of using Mini-Magick is that its lighter weight that just using RMagick. So away I went…

I develop my Rails applications on OS X, so I thought I might have most of the stuff already that I’d need since XCode was installed. I had ImageMagick installed from a previous PHP project I had worked on, so I figured I’d just install the Ruby Gem Mini-Magick.. I did and hooked it up to my application. When running it though, it didn’t work. Bummer. I kept getting cryptic errors like:

ImageMagick command (identify "/tmp/minimagick99445-0.jpg") failed: Error Given 256

Great, what does that mean? So I did some sleuthing on Google and didn’t have much luck for about an hour or so. Then I came across this post at MattKing.org. How could I have missed this? A no brainer that you need to have the image libraries installed before compiling ImageMagick (and thus mini-magick).

So with out further ado, here are the steps you need to perform to get this to work!

Step One – Download Image Libraries

Before compiling and installing ImageMagick, you need to have all of the image libraries installed for the image types you want to manipulate. In my case, I wanted to support JPG, GIF, PNG, and TIFF. So I had to install the following libraries:

Once you have those downloaded, you can go on to Step 2

Step Two – Install Image Libraries

Now that you have all of the image libraries downloaded, its time to install them. Extract them using tar and install them!

JPEG

tar xzvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure
make
sudo make install

LibPNG

tar xzvf libpng-1.2.24.tar.gz
cd libpng-1.2.24
./configure
make
sudo make install

GifLib

tar xzvf giflib-4.1.4.tar.gz
cd giflib-4.1.4
.configure
make
sudo make install

LibTiff

tar xzvf tiff-3.8.2.tar.gz
cd tiff-3.8.2
.configure
make
sudo make install

Step Three – Download & Install ImageMagick

With all of the image libraries installed, you can download ImageMagick. I installed mine to the /usr/ directory, but I think /usr/local/ would work as well.

tar xzvf ImageMagick-6.3.8-9.tar.gz
cd ImageMagick-6.3.8
./configure --prefix=/usr/
make
sudo make install

Step Four – Install ImageMagick

You’re almost there! Now that you have all of the image libraries installed and ImageMagick installed, you can go ahead and install the Ruby gem Mini-Magick

sudo gem install mini_magick

One note that I found very helpful on Matt King’s blog post was to check to make sure the configure script for ImageMagick could find the image libraries you wanted to be able to use. Check the output at the end of the configure script and make sure yes is present next to each image library you want to use.

You should be ready to rock now! To use mini-magick, insert the following lines into your environment.rb file:

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

Now that you’re all set up, you can load up a file and do whatever you want to it using Mini-Magick. Perhaps something like this!

filename = "path to your file"
fileout = "path to output file"
image = MiniMagick::Image.from_file(filename)
image.resize "120x120"
image.write(fileout)

There you go! If something doesn’t work, the first thing I’d check is to make sure all of the libraries and ImageMagick were installed properly. Also check file permissions in case for some reason the directory you’re trying to save your file is locked down so the web server user can’t write to it.

One more side note before I finish. I was able to use these exact steps to install ImageMagick and Mini-Magick on my Debian server which I’m using for my public test environment. So, that cool thing about using these steps installing everything from source allows you to duplicate on other *NIX systems.

4 thoughts on “Ruby on Rails Image Manipulation with ImageMagick and Mini-Magick on OS X

  1. Pingback: billrowell.com » Bill Rowell » Blog Archive » Uploading and Resizing Images in Ruby on Rails

  2. elzr

    Thanks for the info, it was helpful.

    There’s just a tiny mistake in step four, instead of

    sudo gem install mini-magick

    it’s

    sudo gem install mini_magick

    That is, it’s an underscore, not a dash.
    I noticed because I just copy-pasted your code and it wouldn’t run.

    Hope this helps, thanks again!

  3. Pingback: ror image manipulation with mini_magick « ai3x

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.