Coupons with Imaging module

In this post I show you how to create coupons using the Magnolia Imaging module. I start with a background image and overlay the special promotion text. Campaign specific details are managed in the Data module while static text is configured in the Imaging module.


Let's start by registering an image generator. For this purpose I use the ImageOperationChain generator since it allows me to execute multiple image operations one after the other. My image operation chain will be called coupon and it outputs PNG files.


The first image operation in the chain, loadImage, loads a background graphic. I am using the ClassPathImageLoader class which can retrieve an image from the classpath. (You can also load from DMS, website or external URL.) By default, a Magnolia webapp's classpath includes any JARs in /WEB-INF/lib and any classes and resources in /WEB-INF/classes. I copied my background graphic into the latter and named it coupon.png. Here's the operation configuration.


When I request the image I need to pass an existing page in the URL. This is how the parameter provider factory works. I can see the image at http://<host>/<instance>/.imaging/coupon/website.png. This is a Christmas coupon with some red lights and space for text below.


Campaign management in Data module

For managing the campaigns I created a custom data type in the Data module. The campaign entry dialog allows editors to enter a discount percentage, product and validity period. I entered a Christmas campaign that offers 25% discount on a single in-store product.


The Data module stores the information in the data workspace. The discount percentage, for example, is stored in /campaign/Christmas/percentage.

Text overlay

The TextFromNode class can retrieve content from any workspace and overlay it on top of the image. In the percentage operation I fetch the discount percentage from the Data module. I need to pass the workspace name and path in the request URL, http://<host>/<instance>/.imaging/coupon/data/campaign/Christmas.png.

The percentSymbol operation overlays the % symbol using the FixedText class. The symbol is given in the text property. This works fine for static strings that don't change often. See Text overlay to learn about the nodes and valid values.


I added the text "off" also as fixed text and requested the image. Here's what is looks like at this point.


Adding the product description, validity period and legal disclaimers works in a similar fashion. Changing typeface, font size and positioning I was able to make the design more interesting. Here's the final result.


When the next campaign kicks in, all you need to do is ask your graphic designer for a new background image and create a campaign data item in the Data module.


Background image credit: George Deputee and Juliancolton2 on Flickr.