Suite Home download terms of use wizard api docs T2I Service
Note: All images on this Site and the Buttons are generated throught this service!

T2IServlet

This is the description about a comercial service.

You can use this service for test but not for any public available website without hosting agreement.

If you are interested in using this servlet, contact me for more details

email: thiemann (at) othiemann (dot) info

Hosting is available for a monthly rate of 10,- Euro including 2GB traffic(plus 16% tax for german customers). Using this offer you also have the chance to upload your own ttf fonts (up to 2MB) via ssh.

Basic functionality

txt is parameter to pass dynamic text to servlet. (default value = "Hello world") Servlet will render this text as graphic image.

http://t2i.othiemann.com/t2i/txt/This%20is%20T2IServlet

Multiline support

Servlet support usage of \n (%0A) inside text as newline separator

http://t2i.othiemann.com/t2i/txt/This%0ais%0aT2IServlet

Colors

col is parameter to define text color. It have hex RRGGBB format similar to HTML hex colors, but without hex radix char '#'.(default=000000)
bgcol is parameter to define background color.(default=ffffff)

http://t2i.othiemann.com/t2i/txt/This%20is%20T2IServlet/col/000099/bgcol/f0ffff

Font

font path to .ttf font file, relative to web application base (default is arial.ttf)
fh font height in pixels (default=12)

http://t2i.othiemann.com/t2i/txt/This%20is%20T2IServlet/col/000099/bgcol/f0ffff/font/GARABD.TTF/fh/28

Limiting width

w parameter to specify width of generated image. Servlet will make attempt to split text in multiline string to fit specified width. When width is not enough to fit single longest word then resulting width will be minimum possible width to fit. When there is no problem to fit this parameter also acts as minimal width for generated image. When this parameter is omited then width of generated image is equals to required width without splitting text in multiline (unless \n found).

http://t2i.othiemann.com/t2i/txt/This%20is%20T2IServlet/col/000099/bgcol/f0ffff/font/GARABD.TTF/fh/28/w/120

Limiting minimum height

h parameter to specify minimum height for generated image. If requred height is more than specified then result will be reured height. If required height is less than this parameter value, then height will be as specified. When omited height will be minimum required.

http://t2i.othiemann.com/t2i/txt/This%20is%20T2IServlet/col/000099/bgcol/f0ffff/font/GARABD.TTF/fh/28/h/100

Aligning text

align parameter to specify text alignment. This parameter may contain up to 2 words for vertical and horizontal text alignment.
Vertical alignment keywords are: top,middle,bottom
Horizontal alignment keywords are: left,center,right
See below example of center,middle allignment for multiline text. (default is "top,left")

http://t2i.othiemann.com/t2i/txt/This%0ais%0aT2IServlet/col/000099/bgcol/f0ffff/font/GARABD.TTF/fh/28/h/200/w/200/align/center,middle

Background Image

bg parameter to specify bgimage path relative to web-app base. When background image is less than generated image it is automatically tiled. When no parameter passed then you will see just solid color BG specified in bgcolor.

http://t2i.othiemann.com/t2i/txt/This%0ais%0aT2IServlet/col/000099/bgcol/f0ffff/font/GARABD.TTF/fh/28/h/200/w/200/align/center,middle/bg/bg.gif

Alligning background

bgalign parameter to specify bgimage alligning. Format and values are the same as for text alignment. Default is top,left

http://t2i.othiemann.com/t2i/txt/This%0ais%0aT2IServlet/col/000099/bgcol/f0ffff/font/GARABD.TTF/fh/28/h/180/w/180/align/center,middle/bg/bg.gif/bgalign/center,middle

Margins

mw width
mh height
Parameters to define margins around text. Default value is 2 for both parameters.

http://t2i.othiemann.com/t2i/txt/This%20is%20T2IServlet/col/000099/bgcol/f0ffff/font/GARABD.TTF/fh/28/mw/20/mh/20

Cache control

Servlet have internal filesystem based cache where it stores generated images. When the same combination of parameters is received, servlet will find already saved image and return it instead of generating new one. But sometimes it's usefull to switch caching off to get "fresh" images and/or refresh cache copy. wtk_mode_no_cache value may be any, when parameter present in path then new fresh copy of image will be generated and saved in cache too
wtk_ttl Life time parameter. default value is 1w (1 week). When servlet return data to browser it set expores header to one week ahead unless you specify other expiration time
wtk_cache_clean when this parameter present in path ALL cache content will be dropped prior to creating new image

Parameters passing

Passing parameters to servlet is possible as in traditional way via query string in GET method, as via servlet path info when parameters are passed like path tokens
In few words
http://t2i.othiemann.com/t2i?txt=Hello&w=100
and http://t2i.othiemann.com/t2i/txt/Hello/w/100
is equivalent. This is quite usefull because some proxies and clients do not cache requests which contains traditional GET parameters. Thus using of passing parameters via path info and keeping in mind expiration header servlet returns to client you can dramaticaly reduce you server load.

Servlet Init Parameters

cache_dir parameter for setting cache directory, default value WEB-INF/tmp/{servletClassName}
font_dir parameter for setting font directory default is project root directory
Both paths are relative to project root

Extending functionality

T2IServlet extending CachedGraphicsServlet which provides a lot of usefull methods to generate graphic output.
Here is for your curiosity source code for very first version of T2Iservlet. You only need to override this 3 methods to implement your own required functionality. package com.vmax.servlet; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; public class T2IServlet extends CachedGraphicsServlet { // for generating cache entry key servlet need to know set of // actual parameters names this code require. // Override this method with names of parameters you really use. protected String[] getActualParameterNames() { return new String[]{ "txt","font","col","bgcol","bg","w","h","fh","mw","mh","align","bgalign" }; } // Servlet need to know what type of image you will write to // output stream. Well if we use PNG than no problem. // but when you would like to write JPG then you would need to // override this method protected String getContentType(HttpServletRequest req) { return "image/png"; } // Override this method to implement your own logic of generating image protected void serve(BasicHttpRequest req, ByteArrayOutputStream baos) throws Exception { // get parameters String fontpath = getParam(req,"font","arial.ttf"); Color col = getColorParam(req, "col", 0x00000000); Color bgcol = getColorParam(req, "bgcol", 0x00ffffff); BufferedImage bgim = getImageParam(req, "bg"); String txt = getParam(req,"txt","Hello world"); int fontHeight = getParam(req,"fh",12); int margin_width = getParam(req,"mw",2); int margin_height = getParam(req,"mh",2); int fitWidth = getParam(req,"w",0)-margin_width*2; int fitHeight = getParam(req,"h",0)-margin_height*2; int textAlign = getAlignParameter(req,"align",0); int bgAlign = getAlignParameter(req,"bgalign",0); // load font and obtain font metrics for size calculations Font font = loadFontFromPath(fontpath,fontHeight); FontMetrics fm = getMetrics(font); // Split text in multiline depending on font metrics, // required width and height and \n inside text java.util.List lines = splitText(txt,fm,fitWidth); // Calculate size neccessary to render this lines Dimension requred_size = getRequiredRect(lines,fm,fitWidth,fitHeight); // Now we have all ready to create image int width = (int)Math.ceil(requred_size.getWidth()+2*margin_width); int height = (int)Math.ceil(requred_size.getHeight()+2*margin_height); BufferedImage im=new BufferedImage( width,height, BufferedImage.TYPE_INT_ARGB ); Graphics2D graphics=im.createGraphics(); setRenderingHints(graphics); // draw background graphics.setBackground(bgcol); graphics.clearRect(0,0,im.getWidth(),im.getHeight()); tileBackground(im,graphics,bgim,bgAlign); // draw text graphics.setFont(font); graphics.setColor(col); drawLines(im,graphics,lines,margin_width,margin_height,fm,textAlign); // write result into stream ImageIO.write(im,"png",baos); } }