Skip to content
Squeegy edited this page Sep 12, 2010 · 6 revisions

Sometimes you want to render images based on non image based model data. For example, let’s say we have a comment model that we want to have an image rendering for. In this case, you can leave the model completely alone, since it doesn’t need to accept uploads or save and load images in any way. Instead, you can use the Fleximage::Blank class to create an empty image and then process it to your hearts content.

For example, in the show action template for a plain old comment model (which does not have a call to acts_as_fleximage), we can use a template that loks something like this:


# app/views/comments/show.png.flexi
Fleximage::Blank.new('400x150')).operate do |image|
  # Start with a chat bubble image as the background
  image.image_overlay('public/images/comment_bubble.png')

  # Assuming that the user model acts_as_fleximage, this will draw the users image.
  image.image_overlay(@comment.user.file_path,
    :size => '50x50',
    :alignment => :top_left,
    :offset => '10x10'
  )

  # Add the author name text
  image.text(@comment.author,
    :alignment => :top_left,
    :offset => '10x10',
    :color => 'black',
    :font_size => 24,
    :shadow => {
      :blur => 1,
      :opacity => 0.5,
    }
  )

  # Add the comment body text
  image.text(@comment.body, 
    :alignment => :top_left,
    :offset => '10x90',
    :color => color(128, 128, 128),
    :font_size => 14
  )
end

Which would render an image that may looks something like this:

Clone this wiki locally