Dynamic Images

Ruby library providing image rendering described by dynamic templates

Dependencies

Library is using these libraries:

  • cairo

  • pango

  • rexml

How to install & use

Download this repository and place it into your folder.

Require init.rb file from library’s folder.

Optionaly you can load gtk2 libraty too. It’s supporting another image formats.

For more information how to use or about elements read documentation.

Usage of Library

In general about options

Options should be in base given as hash. Format of keys in hash is not fixed. You can use Symbol as good as String. There is also no difference between “-” and “_” chars.

If option accepts more arguments you can specify they in Array and also in String. In case you choose String it’s necessary to seperate arguments by space char.

Example

These Hashes are considered as absolutelly same.

  • {:vertical_align => :middle}, {'vertical_align' => 'middle'}, {"vertical-align" => :middle}, {:vertical_align => "middle"}, {:vertical_align => :middle}, etc.

  • {:to_fit => [:crop, :sentences, 3, :resize]}, {:to_fit => "crop sentences 3 resize"}, {'to-fit' => "crop sentences 3 resize"}, etc.

Passing a Block

In block you can accept object to call methods on it or if you don’t accept any argument, block is called in object instance.

Example

These examples are considered as same.

DynamicImage.new do |img|
  img.text "<b>Warning</b>"
  img.save! "warning.png"
end
DynamicImage.new do
  text "<b>Warning</b>"
  save! "warning.png"
end

Image formats

In base you can save and load all as PNG images. You can enable more formats by loading gtk library. DynamicImage will automatically detect it’s loaded.

require 'gtk2'

Using with Rails

To use within Rails application just download this library as plugin.

rails plugin install git://github.com/malis/dynamic_images.git

You need to add cairo and pango gems to your Gemfile or environment.rb file.

Then just update your controller like this:

def show
  @article = Article.find(params[:id])
  respond_to do |format|
    format.html
    format.png { render_image } #or render_image("show.jpg"), find more in doc
  end
end

Do not forgot to add mime types for used image formats to your environment file.

Mime::Type.register "image/png", :png

Create view articles/show.png.xml.erb like this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE dynamic_images PUBLIC "-//malis//dynamic_images//EN" "https://raw.github.com/malis/dynamic_images/master/lib/parsers/xml.dtd">
<dynamic_images>
  <dynamic_image width="500" align="center" background="blue 0.5">
    <text font="Arial bold 20"><%= @article.title %></text>
    <text indent="30"><%= @article.text %></text>
  </dynamic_image>
</dynamic_images>

That’s all!

Library is tested under Rails 2.3.11 (ruby 1.8.7) and 3.1.1 (ruby 1.9.2)

Copying

Copyright © 2012 Dominik Mališ

This program is free software. You can distribute/modify this program under the terms of the GNU LESSER GENERAL PUBLIC LICENSE.