Skip to content
corbanbrook edited this page Sep 13, 2010 · 20 revisions

RComposite is…

an abstraction layer for RMagick, (bindings for ImageMagick) to let you easily manipulate and composite images in a way that attempts to mimic Adobe Photoshop.

I found doing simple things in RMagick were quite complex. For instance to set an image 50% transparent you need to load your image, generate an alpha channel mask set to 50% grey, set the correct matte types for the image and mask, and then composite over the alpha information into your image. With RComposite, however, this is fairly trivial, you dont need to know anything about masks, or matte modes, or alpha channels. You just set the opacity.

layer :file => 'photo.png' do
  opacity 50
end

Usage Examples

Example

In this example I am using a theme from Douglas Bowman’s Photo gallery which he kindly released free for use. You can get it from his website.

Douglas is an incredible graphic artist and prepares lovely photo collages for each gallery in his album painstakingly by hand.

What if we wanted to to generate such a collage on the fly from random images in our gallery? The following example shows how you can easily use RComposite to accomplish this.




Load RComposite and setup a blank canvas
require 'rubygems'
require 'rcomposite'
canvas = RComposite::Canvas.new(733, 334)

Add the frame and main photo

canvas.stack do 
  layer :file => 'main.png' do 
    offset 10, 10
    image.crop_resized!(710, 200)  # an RMagick method
  end
  layer :file => 'template.png'
end

Remember whatever is added to the canvas first is what is on top of the layer stack. So the template.png file renders first, and then the main.png on top of it.

The main image cropped and re-sized and composited over our template.







Position blank slides

slides = RComposite::LayerSet.new :slides do
  layer_set :slide_1 do
    layer :file => 'slide.png'
    offset 15, 160
    rotate 15
  end
  layer_set :slide_2 do
    layer :file => 'slide.png'
    offset 115, 165
    rotate -15
  end
  layer_set :slide_3 do
    layer :file => 'slide.png'
    offset 215, 160
    rotate 25
  end
end

Now the slides LayerSet needs to be added to the top of the canvas layer stack.

canvas.layer_set slides, :top

Slide borders positioned over the main image.







Add photos to blank slides

We can easily add semi transparent slide photos to the blank slide borders by editing the slides LayerSet to include a new photo layer.

slides = RComposite::LayerSet.new :slides do
  layer_set :slide_1 do
   
    # Add a photo to the slide border
    layer :file => 'slide_photo1.png' do
      image.crop_resized!(88,88)
      offset 16, 16
      opacity 60
    end

    layer :file => 'slide_border.png'
    offset 15, 160  # note: offset and rotate commands will run on 
    rotate 15         #          every layer within the layer_set
  end
...
end

canvas.layer_set slides, :top

Now save the finished image

canvas.save_as 'demo.png'

Semi-transparent slides added to the canvas.









Now that was pretty easy!