qr4r – Generate QRCode PNGs in Ruby

One of our ongoing projects has been using QRCodes as page identifiers on promo materials. Up to this point, we’ve been using online QRCode generators (of which there are plenty: QR-Code Generator, Create QR Code, Google Url Shortener to name a few).

We were tired of cutting and pasting urls. Could we build QRCodes for urls directly in the app? Some of the services mentioned above have APIs, which we could use to generate QRCode images. But instead of adding 3rd party web API calls to my app, I wanted to see how hard it’d be to own that QRCode generation logic.

A quick search revealed a few projects on github that all seem to start with rQRCode as a base. This gem generates the QRCode data, but not a png; it should be a great place to start. I also found a couple libraries that use rQRCode plus Chunky_PNG to construct PNGs. But, since we’ve done a bunch of work with ImageMagick and mojo_magick, I thought I might leverage those tools instead.

After releasing mojo_magick on rubygems, I built the qr4r gem. It uses rQRCode to generate the QRCode data, then some simple ImageMagick commands via mojo_magick to write out the image.

How can you use this awesomeness, you ask?

Add qr4r gem to your Gemfile
gem "qr4r"
Update your bundle
bundle install
Fire up a console and …

bundle exec irb
ruby> require 'qr4r'
ruby> Qr4r::encode('this is a qr code, yo', 'my_qrcode.png', :border => 10)

you get this:

Go get your QRCode on!

Docs/Code On Github: qr4r

Advertisements

Adventures with ImageMagick in Ruby: MojoMagick vs RMagick

I was already using MojoMagick in a project. I needed to add some functionality that would resize an image to fill a given space. That is, given width and height, resize the image so that it maximally fills the box (and maybe hangs over the edge). Clearly, this is a job for ImageMagick, so I thought I should be able to access this functionality through MojoMagick. Sadly, MojoMagick doesn’t expose this resizing functionality.

I decided to try moving to RMagick. RMagick seems to be all the hotness based on google searches for ImageMagick and ruby so it seemed like the way to go. And while I was upgrading, I thought I should also get on to the latest version of ImageMagick (from ~6.3 to 6.7).

Thus, I ran down a rat hole that was not terribly pleasant. Issues included:

  • ImageMagick rpm required building from source (old machine, old OS)
  • Much fighting with bundler and rvm versions and ldconfig to get the machine’s head on straight.
  • Finally, ruby/rake segfaulting with every run – which after some reading seemed to be related to RMagick + ImageMagick upgrades that left old bits of ImageMagick around on the system. (read more at stack overflow.)

Yuk.

The fix? Simple: go back to MojoMagick — the dirt simple ImageMagick-Ruby library. I pulled the source from the svn repo, added enough stuff to get a bundler gem built, added the functionality I needed (which was about a 2 line change), and stuck it up on github.

Granted, RMagick is very slick and ruby-ish and has much more of ImageMagick mapped into it’s API. And on a more current system the install can be easier, though not necessarily painless (see rmagick on ubuntu at stackoverflow). But if you don’t need too many bells and whistles, the bare bones ImageMagick functionality that you get with MojoMagick might save you some trouble.

MojoMagick: by Steve Midgley
Updated on github: by Mr Rogers

Put it in your Gemfile with

gem 'mojo_magick', :git => 'git://github.com/rcode5/mojo_magick.git'