views:

514

answers:

6

Good friends of stackoverflow, I am here today for guidance regarding image processing/manipulation using Ruby in a Rails environment. I'm creating on-the-fly dynamic banner ads that will feature mostly (if not completely) text. It's fairly simple with just a line or two, but I'd like the option to adjust font, text color, text size, etc.

What libraries do you recommend for this sort of task?

I've read up in rMagick a little bit and I see a lot of complaints about memory issues and lack of text rendering features. I'm not seeing many alternative active projects.

Thanks!

Edit: I got a chance to mess around with RMagick and while it's library is full featured, it seriously lacks in the text department. One feature I'm unable to use is non-breaking spaces. I'm printing a phone number in my text and it really doesn't make sense to have the area code on a different line than the rest of the number.

I'm choosing RMagick as the best solution for now, because it's full-featured and actively developed, but it is by no means a good solution.

A: 

The ImageScience library is made for people who hate rMagick's poor memory usage, etc. I use it as a backend processor for the attachment_fu plugin, which makes it easy to make an image model in Rails.

nilbus
Imagescience is more suited for resizing / cropping images, not generating like he asked for.
Ryan Bigg
+2  A: 

You can farm out your image processing needs to the command-line version of ImageMagick instead of using the the rMagick Ruby bindings.

bta
A: 

You can use mini-magick to work with ImageMagick in Ruby.

Marcus
+1  A: 

I wrote something like the following:

require 'rubygems'
require 'RMagick'
include Magick

image = Image.new(50, 50) {
  self.background_color = "white"
}
text = Draw.new
text.annotate(image, 0,0,0,40, 'Named Colors') {
     self.fill = 'black'
     self.pointsize = 32
}
image.write("image.png")

Which should be easy enough to follow. Also have a look at the documentation. Whilst it's not quite laid out to perfection, it's pretty much all there.

Ryan Bigg
A: 

Recently I have been experimenting with creating charts/graphs from data sets using Ruby. When I was unable to find any libraries or gems that really did what I wanted, I started tinkering around with SVG graphics and I found that they are actually fairly simple to create. The SVG format is simply plain-text XML. I built an SVG image in Inkscape, saved it to a plain SVG file, and my Ruby script uses that file as a template (I dynamically update a few line elements and several text labels and leave the structure of the file intact). SVG gives you all sorts of font options (like CSS or HTML).

These two tutorials give you a quick look at SVG and how you can build an image fairly quickly with any app or language that can write to a text file. Ruby can use plain old puts to build a SVG file, or you can let the 'builder' gem do the formatting for you.

bta
A: 

Are you limited to MRI Ruby? If there's any way you can get access to a jRuby instance (maybe on an EC2 stack for instance) you could make use of the wonderful Processing library. A project I worked on before did something similar with Processing via jRuby, it's really quite a potent combination.

andykram