Using RMagick/ImageMagick in Rails to resize images

ImageMagick is, as you may already know, a software suite used to read, create and convert images. At this point, you may have already noticed the potential of ImageMagick in combination with Rails to read, resize, rotate, generate captchas... you name it.

The connection between ImageMagick and Rails is done using a gem called RMagick. You can take a look at the User's Guide/API here: RMagick User's Guide. As many of the other Rails developers around, I use a Mac to develop my applications, and then I deploy them in a Linux server. As the Mac installation is perhaps a bit trickier, I'm describing it too.

Installing ImageMagick/RMagick in MacOS X

If you don't have imagemagick already installed, the easiest way to do so is using MacPorts (formerly DarwinPorts). You can follow the instructions on the MacPorts site: Installing MacPorts.
Once you have MacPorts installed, you can easily install ImageMagick issuing the following command in your Terminal (you will be asked for your password):

macbookpro:~ daniel$ sudo port install imagemagick

Now, I'm supposing you already have RubyGems installed.
If not, download the source from RubyForge, compile it with ./configure; make and install it with make install.

So, we can proceed to install the RMagick gem. The tricky part is that you need to set some environment variables to point to the place where the ImageMagick binaries are installed. Open your Terminal, and set the environment variables copying and pasting the following lines:

export MAGICK_HOME=/usr/local/ImageMagick/
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"
export PATH="$MAGICK_HOME/bin:$PATH"


You can now issue the gem install rmagick command which will download, compile and install the RMagick gem automatically.

Installing ImageMagick/RMagick in Linux

The installation under Linux is much easier than in MacOS X. Install ImageMagick and development libraries first and then install the gem (and rubygems too, if you don't have it - apt-get install rubygems -).

newton:~# apt-get install imagemagick libmagick9-dev
newton:~# gem install RMagick

Once you have installed RMagick, using it in your Rails application is as easy as adding the line require 'RMagick' in the controller you need RMagick.

In the following example, we create the preview action in our controller, which will, in a simple way, resize any image to a maximum of 200x200.

def preview
  @archive = Archive.find(params[:id])
   if @archive.contenttype == "image"         
      img_orig = Magick::Image.read("/my/picture/path/"+@archive.filename).first
      img = img_orig.resize_to_fit(200,200)
     @response.headers["Content-type"] = img.mime_type
      render :text => img.to_blob   
   end
end

We could use now this action we created to access the image from another view:
    <img src="/archives/preview/<%= @archive.id -%>">

You can take a look at the API to see the complete list of transformations you can do with RMagick: RMagick User's Guide



 


Trackback

Trackback URL for this entry:
http://www.gra2.com/trackback.php/using-rmagick-imagemagick-rails

No trackback comments for this entry.

Comments

Post a comment

It's important not to confuse Rails with Ruby here. Rails is Ruby, but Ruby is not Rails. When you talk about "the connection between ImageMagick and
Rails," it is in reality a connection with Ruby itself. Rails is simply in the fortunate position of being a Ruby framework, meaning that it can interface with
other Ruby libraries and extensions.

In fewer words, RMagick is a Ruby library, not a Rails library.

Anonymous on Monday, March 12 2007 @ 06:30 AM CET Reply | #

You are absolutely right. RMagick is a library for Ruby.

I just said Rails instead of Ruby for the sake of the article, to avoid confusing people, as most of them will use RMagick in combination with Ruby on Rails and not just Ruby. Anyway,
thanks for the clarification.

Cheers,
-- Daniel.

---
www.gra2.com
in.solit.us: Upload and share your files

daniel on Monday, March 12 2007 @ 04:37 PM CET Reply | #

When i use rMagick, i use send_data

img = Magick::Image.read(full_im_path).first&nbsp; <br />
img = img.wave<br />
send_data(img.to_blob, :type=&gt;'image/png', :disposition=&gt;'inline')


Sorry for my english

Anonymous on Thursday, May 24 2007 @ 06:51 PM CEST Reply | #

September 2007, there is now an OS-X installer available

rmagick-osx-installer

at

http://rubyforge.org/projects/rmagick/

Anonymous on Tuesday, September 25 2007 @ 06:00 PM CEST Reply | #

Search



About

newton.gra2.com is a blog about technology, opinion and random thoughts written by Daniel Alvarez, a computer engineer currently living in Zurich, Switzerland.

Topics

News (20/0)
Manuals (24/0)
Security (7/0)
Music (3/0)
Weeklog (1/0)
Personal (34/0)
Photos (3/0)
Opinion (14/0)
Windows (5/0)

Blogroll

Pros i contres (Jordi)
Entrepa de fusta (Oriol)
Spaghetti Code (Isaac)
Made in net (Eric)
Nogare (Juan)
Blog de Isaac Jimenez
Web d'en Jaume Benet
Montcada Wireless (Fran)
Blog d'en Ricard Forniol
Angela Fabregues
in.solit.us

Libertad Digital
FOX News
The Wall Street Journal
The Washington Times
The Jerusalem Post

Michelle Malkin
Eurabian News
Nihil Obstat
Barcepundit
Expose the left
Davids Medienkritik
Johan Norberg
Ayaan Hirsi Ali

User Functions

:

:


Lost your password?

Latest posts

Stories

No new stories

Comments last 2 days

No new comments

Trackbacks last 2 days

No new trackback comments

Links last 2 weeks

No recent new links