Fleximage displays virtual images

February 28th, 2009

In the past, Fleximage has been constrained to rendering uploaded images from uploaded sources. Resize, crop, apply effects, and send out to the client. Well what if you want to render an image, but the model data you want to render is not really image based? Introducing Fleximage::Blank.

Lets say you have a Comment model. A Comment belongs_to a User. For whatever reason, you need an image that represents any comment. This image might looks like this:

A template like this might create this image:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

Note that the Comment does not need a call to acts_as_fleximage. The Comment model itself has no image data in itself, but now it has an easy way to create an image based view of itself.

Checkout the wiki entry on GitHub for more info.

2 Responses to “Fleximage displays virtual images”

  1. mr_whiskers Says:

    FlexImage is great and has a lot of potential but at this point the documentation is lacking and it’s extremely difficult to use. I have no idea what URLs I should be using for what, and whether I should be adding a :jpg or :png to the end of my methods.

    I just want to display an image, and it’s more like trial and error until one of the methods works. The main doc says “formatted_photo_path()” but this isn’t even a method in the library.

    I have also found zero information on how i can avoid rendering GIF files and/or working with animated GIFs in general.

  2. Alex Wayne Says:

    Well it’s easier if you know rails better. Many of things you mention are more about following rails conventions than anything fleximage specific.

    Use jpg or png depending on which format you want for that image.

    formatted_photo_path is a method provided by the restful route “map.resources :photo”, which you add yourself to routes.rb.

    Gif’s are not currently supported, especially animated ones. You are gonna have to drop down to RMagick calls and hack it yourself if you wan that.

Leave a Reply