Like t-shirts? Find hundreds of cool Geek T-shirts at Teenormous

I have previously mentioned using the ruby plugin MiniMagick to resize images. However, I recently realized there is a much easier way that only requires ImageMagick and would therefore work for any web development environment including PHP, Ruby, ASP etc. This technique was inspired by the MiniMagick plugin and involves shelling out directly to the ImageMagick command-line utilities.

I will cover two common techniques:

  • resizing an image
  • determining an image’s width and height

Resizing An Image

To resize an image, you can use ImageMagick’s mogrify utility. For example, to resize an image to 100×100 you would type:

mogrify -thumbnail "100x100>" someimage.gif

The > symbol will make mogrify maintain the aspect ratio. To run this command from RubyOnRails, you can just use backticks to execute it from a command shell like so:

`mogrify -thumbnail "100x100>" someimage.gif`

Determining Image Width and Height

To determine the width and height of the image, we can use ImageMagick’s identify utility like so:

identify -format "%w %h" someimage.gif

This will return the width and height separated by a space. So, to parse these from Ruby you could do:

w, h = `identify -format "%w %h" someimage.gif`.split

It’s that easy. One other side benefit is by running this process from the command-line your webapp will not consume the additional memory needed for the resizing. The command shell will allocate and free this memory. For a RubyOnRails application, this is a huge benefit as one of the biggest issues with other techniques such as RMagick is that they typically may consume too much memory and will crash your web application.

So, I finally got around to converting this site off of Typo. All permalinks should still work. I did not migrate the comments over however.

I chose Typo because I had bought into the hype like everyone else and thought since I am actively working with Rails I should be using a blog written in Rails. Well, since then the creator of Typo has moved on from the project and even his blog is now using Mephisto. But the main reason I migrated from Typo is it just doesn’t have the usability and features of a more mature blogging engine such as WordPress. It was almost painful to write a post in.

At first I spent a few days converting this blog to Mephisto to try to keep it in the Rails family only to find out that the memory requirements for Mephisto are roughly 47MB per fcgi! I think that may have more to do with Rails 1.2 than with Mephisto though but I haven’t spent any time digging in. In any case, beware if you are on a shared host as Mephisto currently is not supported in that environment.

The conversion process was a snap thanks to WordPress’s RSS importer. The rough steps I took were:

  1. Modify Typo’s xml_controller to increase the maximum number of articles in the articles feed. It just has to be greater than the number of posts you have.
  2. Log into your WordPress admin section and click Import -> RSS.
  3. To keep your links the same, you will need to change your WordPress Options -> Permalinks to Date and name based. Then to allow the “articles” portion of Typo urls, you can add this to the generated .htaccess file in the root of your WordPress install:
    RewriteBase /articles
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule articles/(.*) /index.php/$1 [L]