GD::Simple - Simplified interface to GD library |
GD::Simple - Simplified interface to GD library
use GD::Simple;
# create a new image $img = GD::Simple->new(400,250);
# draw a red rectangle with blue borders $img->bgcolor('red'); $img->fgcolor('blue'); $img->rectangle(10,10,50,50);
# draw an empty rectangle with green borders $img->bgcolor(undef); $img->fgcolor('green'); $img->rectangle(30,30,100,100);
# move to (80,80) and draw a green line to (100,190) $img->moveTo(80,80); $img->lineTo(100,190);
# draw a solid orange ellipse $img->moveTo(110,100); $img->bgcolor('orange'); $img->fgcolor('orange'); $img->ellipse(40,40);
# draw a black filled arc $img->moveTo(150,150); $img->fgcolor('black'); $img->arc(50,50,0,100,gdNoFill|gdEdged);
# draw a string at (10,180) using the default # built-in font $img->moveTo(10,180); $img->string('This is very simple');
# draw a string at (280,210) using 20 point # times italic, angled upward 90 degrees $img->moveTo(280,210); $img->font('Times:italic'); $img->fontsize(20); $img->angle(-90); $img->string('This is very fancy');
# some turtle graphics $img->moveTo(300,100); $img->penSize(3,3); $img->angle(0); $img->line(20); # 20 pixels going to the right $img->turn(30); # set turning angle to 30 degrees $img->line(20); # 20 pixel line $img->line(20); $img->line(20); $img->turn(-90); # set turning angle to -90 degrees $img->line(50); # 50 pixel line
# draw a cyan polygon edged in blue my $poly = new GD::Polygon; $poly->addPt(150,100); $poly->addPt(199,199); $poly->addPt(100,199); $img->bgcolor('cyan'); $img->fgcolor('blue'); $img->penSize(1,1); $img->polygon($poly);
# convert into png data print $img->png;
GD::Simple is a subclass of the GD library that shortens many of the long GD method calls by storing information about the pen color, size and position in the GD object itself. It also adds a small number of ``turtle graphics'' style calls for those who prefer to work in polar coordinates. In addition, the library allows you to use symbolic names for colors, such as ``chartreuse'', and will manage the colors for you.
GD::Simple maintains a ``pen'' whose settings are used for line- and shape-drawing operations. The pen has the following properties:
One sets the position and properties of the pen and then draws. As the drawing progresses, the position of the pen is updated.
GD::Simple introduces a number of new methods, a few of which have the same name as GD::Image methods, and hence change their behavior. In addition to these new methods, GD::Simple objects support all of the GD::Image methods. If you make a method call that isn't directly supported by GD::Simple, it refers the request to the underlying GD::Image object. Hence one can load a JPEG image into GD::Simple and declare it to be TrueColor by using this call, which is effectively inherited from GD::Image:
my $img = GD::Simple->newFromJpeg('./myimage.jpg',1);
The rest of this section describes GD::Simple-specific methods.
moveTo($x,$y)
move($dx,$dy)
move($dr)
lineTo($x,$y)
lineTo()
call simultaneously draws and moves the pen. It draws a
line from the current pen position to the position defined by ($x,$y)
using the current pen size and color. After drawing, the position of
the pen is updated to the new position.
line($dx,$dy)
line($dr)
line()
call simultaneously draws and moves the pen. When called
with two arguments it draws a line from the current position of the
pen to the position $dx pixels to the right and $dy pixels down. When
called with one argument, it draws a line $dr pixels long along the
angle defined by the current pen angle.
rectangle($x1,$y1,$x2,$y2)
ellipse($cx,$cy,$width,$height)
polygon($poly)
polyline($poly)
string($string)
font()
method, this will use
either a bitmapped GD font or a TrueType font. The angle of the pen
will be consulted when drawing the text. For TrueType fonts, any angle
is accepted. For GD bitmapped fonts, the angle can be either 0 (draw
horizontal) or -90 (draw upwards).
For consistency between the TrueType and GD font behavior, the string is always drawn so that the current position of the pen corresponds to the bottom left of the first character of the text. This is different from the GD behavior, in which the first character of bitmapped fonts hangs down from the pen point.
This method returns a polygon indicating the bounding bod of the rendered text. If an error occurred (such as invalid font specification) it returns undef and an error message in $@.
stringWidth($string)
font([$newfont])
fontsize([$newfontsize])
penSize([$newpensize])
angle([$newangle])
line()
or move()
with a
single argument.
Here is an example of using turn()
and angle()
together to draw an
octagon. The first line drawn is the downward-slanting top right
edge. The last line drawn is the horizontal top of the octagon.
$img->moveTo(200,50); $img->angle(0); $img->turn(360/8); for (1..8) { $img->line(50) }
turn([$newangle])
line()
or move()
with a single
argument. The turning angle will be applied to each call to line()
or
move()
just before the actual drawing occurs.
Angles are in degrees. Positive values turn the angle clockwise.
fgcolor([$newcolor])
bgcolor([$newcolor])
translate_color(@args)
alphaColor(@args,$alpha)
color_names()
returns the list of symbolic
color names recognized by this module. Called in a scalar context,
the method returns a hash reference in which the keys are the color
names and the values are array references containing [r,g,b] triples.
HSVtoRGB($hue,$saturation,$value)
RGBtoHSV($hue,$saturation,$value)
This script will create an image showing all the symbolic colors.
#!/usr/bin/perl
use strict; use GD::Simple;
my @color_names = GD::Simple->color_names; my $cols = int(sqrt(@color_names)); my $rows = int(@color_names/$cols)+1;
my $cell_width = 100; my $cell_height = 50; my $legend_height = 16; my $width = $cols * $cell_width; my $height = $rows * $cell_height;
my $img = GD::Simple->new($width,$height); $img->font(gdSmallFont);
for (my $c=0; $c<$cols; $c++) { for (my $r=0; $r<$rows; $r++) { my $color = $color_names[$c*$rows + $r] or next; my @topleft = ($c*$cell_width,$r*$cell_height); my @botright = ($topleft[0]+$cell_width,$topleft[1]+$cell_height-$legend_height); $img->bgcolor($color); $img->fgcolor($color); $img->rectangle(@topleft,@botright); $img->moveTo($topleft[0]+2,$botright[1]+$legend_height-2); $img->fgcolor('black'); $img->string($color); } }
print $img->png;
The GD::Simple module is copyright 2004, Lincoln D. Stein. It is distributed under the same terms as Perl itself. See the ``Artistic License'' in the Perl source code distribution for licensing terms.
The latest versions of GD.pm are available at
http://stein.cshl.org/WWW/software/GD
GD, the GD::Polyline manpage, the GD::SVG manpage, the Image::Magick manpage
GD::Simple - Simplified interface to GD library |