-
Notifications
You must be signed in to change notification settings - Fork 53
VirtualImages
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.
The initialization of a Blank
looks like this:
- size: x and y dimensions of the image to created. Examples:
"320x240"
,[320, 240]
,320
The options hash can contain the following keys:
- color: A named color or a Magick::Pixel object (which can be created by the
color(r, g, b, a=0)
helper method. This will be the background color of the new image. The default is completely transparent (color(255, 255, 255, 255)
).
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 looks 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 could look something like this: