gd
package for programmatically creating images. This module was derived from Spencer Thomas's Gdtclft. Gdtclft was modified by John Ellson before I got my hands on it. Possible uses of nsgd include plotting data, fancy page counters, drawing game boards, or any other application where there is a need for dynamically creating images.
gd
, to those you have available when writing Tcl files for AOLserver. The general steps for creating an image are as follows:
create
or one of the createFromXXX
commands.
color
.
writeXXX
or send it out to an HTTP connection using returnXXX
.
destroy
.
Following is an example Tcl script that makes use of the gd
command. You can see the script in action. Mini-logo is another example using nsgd
.
|
gd create <width> <height>
Return a handle to a new gdImage that is width X height.
gd createFromPNG <filehandle>
gd createFromGD <filehandle>
gd createFromXBM <filehandle>
Return a handle to a new gdImage created by reading a PNG
(resp. GD or XBM) image from the file open on filehandle
.
gd destroy <gdhandle>
Destroy the gdImage referred to by gdhandle
.
gd writePNG <gdhandle> <filehandle>
gd writeGD <gdhandle> <filehandle>
Write the image in gdhandle
to filehandle
as a PNG (resp. GD) file.
gd returnPNG <gdhandle>
Returns the image as a PNG to the HTTP connection.
gd color new <gdhandle> <red> <green> <blue>
Allocate a new color with the given RGB values. Returns the color index, or -1 on failure (256 colors already allocated).
gd color exact <gdhandle> <red> <green> <blue>
Find a color index in the image that exactly matches the given RGB color. Returns the color index, or -1 if no exact match.
gd color closest <gdhandle> <red> <green> <blue>
Find a color in the image that is closest to the given RGB color. Guaranteed to return a color index.
gd color resolve <gdhandle> <red> <green> <blue>
Return the index of the best possible effort to get a color. Guaranteed to return a color index.
gd color free <gdhandle> <color index>
Free the color at the given color index for reuse.
gd color transparent <gdhandle> <color_idx>
Mark the color index as the transparent background color.
gd color get <gdhandle> [<color_idx>]
Return the RGB value at <color_idx>, or {} if it is not allocated. If <color_idx> is not specified, return a list of {color_idx R G B} values for all allocated colors.
gd color gettransparent <gdhandle>
Return the color index of the transparent color.
gd brush <gdhandle> <brushhandle>
Set the brush image to be used for brushed lines. Transparent pixels in the brush will not change the image when the brush is applied.
gd style <gdhandle> <color_idx> ...
Set the line style to the list of color indices. This is interpreted in one of two ways. For a simple styled line, each color is applied to points along the line in turn. The transparent color value may be used to leave gaps in the line. For a styled, brushed line, a 0 (or the transparent color index) means not to fill the pixel, and a non-zero value means to apply the brush.
gd tile <gdhandle> <tilehandle>
Set the tile image to be used for tiled fills. Transparent pixels in the tile will not change the underlying image during tiling.
gd set <gdhandle> <color_idx> <x> <y>
Set the pixel at (x,y) to color <color_idx>.
gd line <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
Draw a line in color <color_idx> from (x1,y1) to (x2,y2).
gd rectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
gd fillrectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
Draw the outline of (resp. fill) a rectangle in color <color_idx> with corners at (x1,y1) and (x2,y2).
gd arc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
Draw an arc in color <color_idx>, centered
at (cx,cy) in a rectangle width
by height
, starting at start
degrees and ending at end
degrees.
Start
must be less than end
.
gd polygon <gdhandle> <color_idx> <x1> <y1> ...
gd fillpolygon <gdhandle> <color_idx> <x1> <y1> ...
Draw the outline of, or fill, a polygon specified by the x, y coordinate list.
gd fill <gdhandle> <color_idx> <x> <y>
gd fill <gdhandle> <color_idx> <x> <y> <borderindex>
Fill with color <color_idx>, starting from (x,y) within a region of pixels all the color of the pixel at (x,y) (resp., within a border colored borderindex).
gd size <gdhandle>
Returns a list {width height} of the image.
gd text <gdhandle> <color_idx> <font> <x> <y> <string> [<up>]
Draw text using the font <font> in color <color_idx>,
with lower left corner at (x, y). <font> may be either
tiny, small, medium, large,
or giant.
This command uses built-in, rather than .ttf, fonts and will work if you do not have
any .ttf fonts available or if you have not installed the FreeType library.
gd textTT <gdhandle> <color_idx> <fontpath> <size> <angle> <x> <y> <string>
Draw text using the .ttf font in <fontpath> in color <color_idx>, with pointsize <size>, rotation in radians <angle>, with lower left corner at (x,y). String may contain UTF8 sequences like: "À" Returns 4 corner coords of bounding rectangle. Use gdhandle = {} to get boundary without rendering. Use negative of color index to disable antialiasing.
gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> <w> <h>
gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy>
<destw> <desth> <srcw> <srch>
Copy a subimage from srchandle(srcx, srcy) to desthandle(destx, desty), size w x h. Or, resize the subimage in copying from srcw x srch to destw x desth.
nsgd.so
into AOLserver's /bin
directory. Then modify your server's .ini
file to load the module by including the line:
nsgd=nsgd.so
in the modules section. Modify these directions as appropriate if you are using a .tcl
startup file rather than an .ini
file. Finally, restart your server and you are ready to go. Of course you must have all the appropriate libraries such as gd
, libpng
, zlib
, etc.
There are several support libraries you need to build nsgd
including gd, libpng and zlib.
If you want to use .ttf fonts, you will need the FreeType library. gd supports reading and writing JPEG
files if you have libjpeg (although I could not get it to work). Similarly, XBMs are supported, but I haven't
tried using them.
Here's what I did to get this to build:
returnJPEG
, createFromJPEG
, writeJPEG
will work.
#ifdef
statements so that it will compile as a dll for the Win32 port of AOLserver.