GD defines the following three classes:
Notes:
GD::Image::new(width,height)
class method
To create a new, blank image, send a new() message to the GD::Image class. For example:
This will create an image that is 100 x 100 pixels wide. If you don't specify the dimensions, a default of 64 x 64 will be chosen. If something goes wrong (e.g. insufficient memory), this call will return undef.
GD::Image::newFromGif(FILEHANDLE)
class method
This will create an image from a GIF file read in through the provided filehandle. The filehandle must previously have been opened on a valid GIF file or pipe. If successful, this call will return an initialized image which you can then manipulate as you please. If it fails, which usually happens if the thing at the other end of the filehandle is not a valid GIF file, the call returns undef. Notice that the call doesn't automatically close the filehandle for you.
To get information about the size and color usage of the information, you can call the image query methods described below.
GD::Image::newFromXbm(FILEHANDLE)
class method
This works in exactly the same way as newFromGif , but reads the contents of an X Bitmap file:
GD::Image::newFromGd(FILEHANDLE)
class method
This works in exactly the same way as newFromGif , but reads the contents of a GD file. GD is Tom Boutell's disk-based storage format, intended for the rare case when you need to read and write the image to disk quickly. It's not intended for regular use, because, unlike GIF or JPEG, no image compression is performed and these files can become BIG.
GD::Image::gif
object method
This returns the image data in GIF format. You can then print it, pipe it to a display program, or write it to a file. Example:
GD::Image::gd
object method
This returns the image data in GD format. You can then print it, pipe it to a display program, or write it to a file. Example:
GD::Image::colorAllocate(red,green,blue)
object method
This allocates a color with the specified red, green and blue components and returns its index in the color table, if specified. The first color allocated in this way becomes the image's background color. (255,255,255) is white (all pixels on). (0,0,0) is black (all pixels off). (255,0,0) is fully saturated red. (127,127,127) is 50% gray. You can find plenty of examples in /usr/X11/lib/X11/rgb.txt.
If no colors are allocated, then this function returns -1.
Example:
GD::Image::colorDeallocate(colorIndex)
object method
This marks the color at the specified index as being ripe for reallocation. The next time colorAllocate is used, this entry will be replaced. You can call this method several times to deallocate multiple colors. There's no function result from this call.
Example:
GD::Image::colorClosest(red,green,blue)
object method
This returns the index of the color closest in the color table to the red green and blue components specified. If no colors have yet been allocated, then this call returns -1.
Example:
GD::Image::colorExact(red,green,blue)
object method
This returns the index of a color that exactly matches the specified red green and blue components. If such a color is not in the color table, this call returns -1.
GD::Image::colorsTotal)
object method
This returns the total number of colors allocated in the object.
GD::Image::getPixel(x,y)
object method
This returns the color table index underneath the specified point. It can be combined with rgb() to obtain the rgb color underneath the pixel.
Example:
GD::Image::rgb(colorIndex)
object method
This returns a list containing the red, green and blue components of the specified color index.
Example:
GD::Image::transparent(colorIndex)
object method
This marks the color at the specified index as being transparent. Portions of the image drawn in this color will be invisible. This is useful for creating paintbrushes of odd shapes, as well as for making GIF backgrounds transparent for displaying on the Web. Only one color can be transparent at any time. To disable transparency, specify -1 for the index.
If you call this method without any parameters, it will return the current index of the transparent color, or -1 if none.
Example:
GD::Image::setBrush( )
and GD::gdBrushed
You can draw lines and shapes using a brush pattern. Brushes are just images that you can create and manipulate in the usual way. When you draw with them, their contents are used for the color and shape of the lines.
To make a brushed line, you must create or load the brush first, then assign it to the image using setBrush . You can then draw in that with that brush using the gdBrushed special color. It's often useful to set the background of the brush to transparent so that the non-colored parts don't overwrite other parts of your image.
Example:
GD::Image::setStyle(@colors)
and GD::gdStyled
Styled lines consist of an arbitrary series of repeated colors and are
useful for generating dotted and dashed lines. To create a styled
line, use
setStyle
to specify a repeating series of colors. It
accepts an array consisting of one or more color indexes. Then
draw using the
gdStyled
special color. Another special color,
gdTransparent
can be used to introduce holes in the line, as the
example shows.
Example:
To combine the
gdStyled
and
gdBrushed
behaviors, you can specify
gdStyledBrushed
. In this case, a pixel from the current brush
pattern is rendered wherever the color specified in
setStyle()
is
neither gdTransparent nor 0.
setTile
in order to define the particular tile pattern you'll use
for drawing when you specify the gdTiled color.
details.
setStyled
command.
GD::Image::setPixel(x,y,color)
object method
This sets the pixel at (x,y) to the specified color index. No value is returned from this method. The coordinate system starts at the upper left at (0,0) and gets larger as you go down and to the right. You can use a real color, or one of the special colors gdBrushed, gdStyled and gdStyledBrushed can be specified.
Example:
GD::Image::line(x1,y1,x2,y2,color)
object method
This draws a line from (x1,y1) to (x2,y2) of the specified color. You can use a real color, or one of the special colors gdBrushed, gdStyled and gdStyledBrushed.
Example:
GD::Image::dashedLine(x1,y1,x2,y2,color)
object method
This draws a dashed line from (x1,y1) to (x2,y2) in the specified color. A more powerful way to generate arbitrary dashed and dotted lines is to use the setStyle() method described below and to draw with the special color gdStyled.
Example:
GD::Image::rectangle(x1,y1,x2,y2,color)
object method
This draws a rectangle with the specified color. (x1,y1) and (x2,y2) are the upper left and lower right corners respectively. Both real color indexes and the special colors gdBrushed, gdStyled and gdStyledBrushed are accepted.
Example:
GD::Image::filledRectangle(x1,y1,x2,y2,color)
object method
This draws a rectangle filed with the specified color. You can use a real color, or the special fill color gdTiled to fill the polygon with a pattern.
Example:
GD::Image::polygon(polygon,color)
object method
This draws a polygon with the specified color. The polygon must be created first (see below). The polygon must have at least three vertices. If the last vertex doesn't close the polygon, the method will close it for you. Both real color indexes and the special colors gdBrushed, gdStyled and gdStyledBrushed can be specified.
Example:
GD::Image::filledPolygon(poly,color)
object method
This draws a polygon filled with the specified color. You can use a real color, or the special fill color gdTiled to fill the polygon with a pattern.
Example:
GD::Image::arc(cx,cy,width,height,start,end,color)
object method
This draws arcs and ellipses. (cx,cy) are the center of the arc, and (width,height) specify the width and height, respectively. The portion of the ellipse covered by the arc are controlled by start and end, both of which are given in degrees from 0 to 360. Zero is at the top of the ellipse, and angles increase clockwise. To specify a complete ellipse, use 0 and 360 as the starting and ending angles. To draw a circle, use the same value for width and height.
You can specify a normal color or one of the special colors gdBrushed, gdStyled, or gdStyledBrushed.
Example:
GD::Image::fill(x,y,color)
object method
This method flood-fills regions with the specified color. The color will spread through the image, starting at point (x,y), until it is stopped by a pixel of a different color from the starting pixel (this is similar to the ``paintbucket'' in many popular drawing toys). You can specify a normal color, or the special color gdTiled, to flood-fill with patterns.
Example:
Example:
With either of these methods it is important to know that the routines will attempt to flesh out the destination image's color table to match the colors that are being copied from the source. If the destination's color table is already full, then the routines will attempt to find the best match, with varying results.
GD::Image::copy(sourceImage,dstX,dstY,srcX,srcY,width,height)
object method
This is the simpler of the two copy operations, copying the specified region from the source image to the destination image (the one performing the method call). (srcX,srcY) specify the upper left corner of a rectangle in the source image, and (width,height) give the width and height of the region to copy. (dstX,dstY) control where in the destination image to stamp the copy. You can use the same image for both the source and the destination, but the source and destination regions must not overlap or strange things will happen.
Example:
GD::Image::copyResized(sourceImage,dstX,dstY,srcX,srcY,destW,destH,srcW,srcH)
object method
This method is similar to copy() but allows you to choose different sizes for the source and destination rectangles. The source and destination rectangle's are specified independently by (srcW,srcH) and (destW,destH) respectively. copyResized() will stretch or shrink the image to accomodate the size requirements.
Example:
GD::Image::string(font,x,y,string,color)
Object Method
This method draws a string startin at position (x,y) in the specified font and color. Your choices of fonts are gdSmallFont, gdMediumBoldFont, gdTinyFont and gdLargeFont.
Example:
GD::Image::stringUp(font,x,y,string,color)
Object Method
Just like the previous call, but draws the text rotated counterclockwise 90 degrees.
GD::Image::char(font,x,y,char,color)
Object Method
GD::Image::charUp(font,x,y,char,color)
Object Method
These methods draw single characters at position (x,y) in the specified font and color. They're carry-overs from the C interface, where there is a distinction between characters and strings. Perl is insensible to such subtle distinctions.
GD::Image::interlaced( )
GD::Image::interlaced(1)
Object method
This method sets or queries the image's interlaced setting. Interlace produces a cool venetian blinds effect on certain viewers. Provide a true parameter to set the interlace attribute. Provide undef to disable it. Call the method without parameters to find out the current setting.
GD::Image::getBounds( )
Object method
This method will return a two-member list containing the width and height of the image. You query but not not change the size of the image once it's created.
GD::Polygon::new
class method
Create an empty polygon with no vertices.
GD::Polygon::addPt(x,y)
object method
Add point (x,y) to the polygon.
GD::Polygon::getPt(index)
object method
Retrieve the point at the specified vertex.
GD::Polygon::setPt(index,x,y)
object method
Change the value of an already existing vertex. It is an error to set a vertex that isn't already defined.
GD::Polygon:deletePt(index)
object method
Delete the specified vertex, returning its value.
GD::Polygon::length
object method
Return the number of vertices in the polygon.
GD::Polygon::vertices
object method
Return a list of all the verticies in the polygon object. Each membver of the list is a reference to an (x,y) array.
GD::Polygon::bounds
object method
Return the smallest rectangle that completely encloses the polygon. The return value is an array containing the (left,top,right,bottom) of the rectangle.
GD::Polygon::offset(dx,dy)
object method
Offset all the vertices of the polygon by the specified horizontal (dh) and vertical (dy) amounts. Positive numbers move the polygon down and to the right.
GD::Polygon::map(srcL,srcT,srcR,srcB,destL,dstT,dstR,dstB)
object method
Map the polygon from a source rectangle to an equivalent position in a destination rectangle, moving it and resizing it as necessary. See polys.pl for an example of how this works. Both the source and destination rectangles are given in (left,top,right,bottom) coordinates. For convenience, you can use the polygon's own bounding box as the source rectangle.
GD::Font::gdSmallFont
constant
This is the basic small font, ``borrowed'' from a well known public domain 6x12 font.
GD::Font::gdLargeFont
constant
This is the basic large font, ``borrowed'' from a well known public domain 8x16 font.
GD::Font::gdMediumBoldFont
constant
This is a bold font intermediate in size between the small and large fonts, borrowed from a public domain 7x13 font;
GD::Font::gdTinyFont
constant
This is a tiny, almost unreadable font, 5x8 pixels wide.
GD::Font::nchars
object method
This returns the number of characters in the font.
GD::Font::offset
object method
This returns the ASCII value of the first character in the font
GD::Font::width
GD::Font::height
object methods
These return the width and height of the font.
The latest versions of the perl interface are available at