📄 ch32.htm
字号:
get rid of any annoying messages.
<LI>Copy the file <TT><FONT FACE="Courier">gd.h</FONT></TT> to
<TT><FONT FACE="Courier">/usr/local/include</FONT></TT> or <TT><FONT FACE="Courier">/usr/include</FONT></TT>.
<LI>Copy the file <TT><FONT FACE="Courier">libgd.a</FONT></TT>
to <TT><FONT FACE="Courier">/usr/local/lib</FONT></TT>.
</OL>
<P>
To test if you have a working version of the libraries, run the
<TT><FONT FACE="Courier">gddemo</FONT></TT> program. If you do
not get any output in the <TT><FONT FACE="Courier">demoout.gif</FONT></TT>,
you have not compiled the libraries correctly. At this point,
you can review the source files to see what kinds of functions
are available for use with the package. Now that we have the <TT><FONT FACE="Courier">libgd.a</FONT></TT>
library built, we can go ahead and install the <TT><FONT FACE="Courier">GD.pm</FONT></TT>
files.
<H3><A NAME="InstallingGDpm"><B>Installing </B><TT><B><FONT SIZE=4 FACE="Courier">GD.pm</FONT></B></TT></A>
</H3>
<P>
Version 1.00 of <TT><FONT FACE="Courier">GD.pm</FONT></TT> is
meant for use with <TT><FONT FACE="Courier">gd</FONT></TT> version
1.2. If you are not sure which version to use, check out the documentation
at
<BLOCKQUOTE>
<TT><A HREF="http://www-genome.wi.mit.edu/pub/software/WWW/GD.html" tppabs="http://www-genome.wi.mit.edu/pub/software/WWW/GD.html">http://www-genome.wi.mit.edu/pub/software/WWW/GD.html</A></TT>
</BLOCKQUOTE>
<P>
The installation of <TT><FONT FACE="Courier">GD.pm</FONT></TT>
can be done in one of two ways: using static linking or using
dynamic linking. Use dynamic linking only if your operating system
supports it; otherwise, use static linking. Using static linking
does make the <TT><FONT FACE="Courier">perl.exe</FONT></TT> program
a bit larger. In systems that do not support dynamic linking,
your only course may be to use static linking. In either case,
you have to unpack the source files into the <TT><FONT FACE="Courier">ext/GD</FONT></TT>
subdirectory, where your Perl 5 distribution is unpacked.
<P>
For dynamic linking, take these steps:
<OL>
<LI><TT><FONT FACE="Courier">cd</FONT></TT> to the directory where
you unpacked your tar files. (I renamed the <TT><FONT FACE="Courier">GD-1.00</FONT></TT>
directory to <TT><FONT FACE="Courier">GD</FONT></TT> to save myself
keystrokes while typing pathnames.)
<LI>Run the command <TT><FONT FACE="Courier">perl Makefile.PL</FONT></TT>
to create the makefiles.
<LI>Run <TT><FONT FACE="Courier">make</FONT></TT>.
<LI>Run <TT><FONT FACE="Courier">make install</FONT></TT>.
</OL>
<P>
For static linking, take these steps:
<OL>
<LI><TT><FONT FACE="Courier">cd</FONT></TT> to your Perl 5 distribution
directory.
<LI>Run the <TT><FONT FACE="Courier">Configure</FONT></TT> command.
<LI>Answer <I>yes</I> to the question that asks you if you want
to statically link <TT><FONT FACE="Courier">GD.pm</FONT></TT>
files. (Configure will ask this question only if you unpacked
in the Perl distribution tree.)
<LI>Run <TT><FONT FACE="Courier">make</FONT></TT>.
<LI>Run make install.
</OL>
<P>
After you have run the <TT><FONT FACE="Courier">make install</FONT></TT>
program, test whether you have the correct version by running
the demos in <TT><FONT FACE="Courier">ext/GD/demos</FONT></TT>.
You may have to edit the first line of each script in the demo
files to point to your installation of <TT><FONT FACE="Courier">perl.exe</FONT></TT>.
<P>
Also, the script files in the <TT><FONT FACE="Courier">ext/GD
/demos</FONT></TT> directory (under the Perl distribution directory
tree) print a GIF file to standard output. If you do not redirect
this output to another file or to a viewing program like <TT><FONT FACE="Courier">xv</FONT></TT>,
your terminal settings might get mangled, so run the script like
this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">brushes.pl > test.gif<BR>
xv test.gif</FONT></TT>
</BLOCKQUOTE>
<P>
Remember to include <TT><FONT FACE="Courier">xv</FONT></TT> in
your pathname or use another GIF viewer if you like. After you
have confirmed that the demos are working fine, you can start
with your own GIF files.
<H2><A NAME="WorkingwithGDpm"><B><FONT SIZE=5 COLOR=#FF0000>Working
with </FONT></B><TT><B><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">GD.pm</FONT></B></TT></A>
</H2>
<P>
Once you have the <TT><FONT FACE="Courier">GD.pm</FONT></TT> utilities
installed, you can work with the <TT><FONT FACE="Courier">GD.pm</FONT></TT>
file by using the <TT><FONT FACE="Courier">use GD</FONT></TT>
statement. Basically, you create an image object, write a bunch
of drawing commands to it, and then print it out. A small example
is shown in Listing 32.5. The output of this listing is shown
in Figure 32.4.
<P>
<A HREF="f32-4.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f32-4.gif" ><B>Figure 32.4 : </B><I>Using the GD.pm module.</I></A>
<HR>
<BLOCKQUOTE>
<B>Listing 32.5. Using the </B><TT><B><FONT FACE="Courier">GD.pm</FONT></B></TT><B>
module.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2<BR>
3 use GD;<BR>
4<BR>
5 $im = new GD::Image(300,200);<BR>
6<BR>
7 $white = $im->colorAllocate(255, 255, 255);<BR>
8 $black = $im->colorAllocate(0, 0, 0);<BR>
9 $red = $im->colorAllocate(255, 0, 0);<BR>
10 $blue = $im->colorAllocate(0,0,255);<BR>
11 $yellow = $im->colorAllocate(255,250,205);<BR>
12<BR>
13 $im->transparent($white); # white
color is transparent<BR>
14 $im->interlaced(1); #
cool venetian blinds effect<BR>
15<BR>
16 # Create a flat square paintbrush<BR>
17 $brush = new GD::Image(5,5);<BR>
18 $brush->colorAllocate(255,255,255); # white<BR>
19 $brush->colorAllocate(0,0,0); #
black<BR>
20 $brush->transparent($white); #
white is transparent<BR>
21 $brush->filledRectangle(0,0,5,2,$black); # a black rectangle
<BR>
22<BR>
23 #<BR>
24 # Draw some text<BR>
25 #<BR>
26 $im->string(gdLargeFont,150,10,"Large Font!",$red);
<BR>
27 $im->string(gdSmallFont,150,28,"The small font!",$blue);
<BR>
28 $im->stringUp(gdTinyFont,280,150,"Vertical Text!",$black);
<BR>
29 #<BR>
30 # Draw some arcs<BR>
31 #<BR>
32<BR>
33 $im->setBrush($brush);<BR>
34 $im->arc(50,100,50,50,0,90,gdBrushed);<BR>
35 $im->arc(100,100,50,50,0,180,gdBrushed);<BR>
36 $im->arc(150,100,50,50,0,270,gdBrushed);<BR>
37 $im->arc(200,100,50,50,0,360,gdBrushed);<BR>
38 $im->fill(210,110,$blue);<BR>
39<BR>
40<BR>
41 $poly = new GD::Polygon;<BR>
42 $poly->addPt(10,10);<BR>
43 $poly->addPt(50,10);<BR>
44 $poly->addPt(150,50);<BR>
45 $poly->addPt(10,100);<BR>
46 $im->polygon($poly,gdBrushed);<BR>
47<BR>
48 $im->fill(40,40,$yellow);<BR>
49<BR>
50 # print the image to stdout<BR>
51 print $im->gif;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
At line 3, we include the <TT><FONT FACE="Courier">GD.pm</FONT></TT>
module with the <TT><FONT FACE="Courier">use</FONT></TT> statement.
Line 5 starts a new image that is 300 pixels wide and 200 pixels
high. Lines 7 through 11 allocate colors in a color map for the
image. The first color allocated for the map is the background
for the image. Line 13 forces <TT><FONT FACE="Courier">white</FONT></TT>
to be transparent; that is, colors written over white will be
seen as though written over a transparent background.
<P>
The interlaced method (line 14) lets the image be constructed
in stages, with each stage enhancing the focus a little instead
of being rendered in one slow step.
<P>
At line 17, we create a brush to draw with. This brush is a mini-image
in itself and has to be allocated its own color maps. Line 21
creates the filled rectangle to make it a solid brush.
<P>
In lines 26 through 28 we draw some text using different fonts.
Note how easy it is to draw vertical text. In each of the text-drawing
routines you have to specify the font, the <TT><FONT FACE="Courier">x,y</FONT></TT>
location to start at, the text to write, and the color to use.
The font types are listed in the <TT><FONT FACE="Courier">GD.pm</FONT></TT>
file.
<P>
<TT><FONT FACE="Courier">GD.pm</FONT></TT> allows you to draw
various shapes. The <TT><FONT FACE="Courier">arc()</FONT></TT>
function can be used to draw portions of an arc. The parameters
to the call are the location to start drawing, its width and height,
the angle to draw, and the paint brush to use, if any. The <TT><FONT FACE="Courier">paintbrush</FONT></TT>
parameter is optional and if left out will use the default paint
brush. At line 38, we fill in the circle with the color blue.
(Note that a circle is simply an arc drawn for 360 degrees with
its height equal to its width.)
<P>
At line 41 we draw a polygon and in the subsequent lines add points.
The polygon is drawn on the image area at line 46. In line 48
we fill the inside of the polygon with a yellow background color.
<P>
At line 51 we print the image to the standard output from where
it can be piped to a viewing program or stored on disk. Actually,
what we do in the next section is to pipe the output to the standard
output as a response to a Web page request.
<H2><A NAME="UsingGDwithHTMLPages"><B><FONT SIZE=5 COLOR=#FF0000>Using
GD with HTML Pages</FONT></B></A></H2>
<P>
Listing 32.6 presents another program which creates a tree of
sorts and prints it to the standard output as a GIF file. This
program recursively creates the branches of a tree. At the end
of each recursion it draws a little, green rectangle. Admittedly,
it's not a pretty tree, but with a few random number tricks, you
can make this script generate a new tree every time it's called.
This regeneration feature makes the script excellent for use in
scripts. For example, you can make the output of the script be
the source URL to an HTML image tag. Whenever the HTML page is
loaded, a new tree image is displayed.
<HR>
<BLOCKQUOTE>
<B>Listing 32.6. Using the tree-generation program.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2<BR>
3<BR>
4 $PI=3.1415926; #
no need to use a jillion digits<BR>
5 $PIBY2 = $PI/2; #
Use a fanout of -pi/2 to +pi/2<BR>
6 $NPIBY2 = -1. * $PI/2;<BR>
7<BR>
8 sub deg2rad {<BR>
9 my $deg = shift;<BR>
10 $deg %= 360;<BR>
11 return $rad = $deg * $PI
/ 180.0;<BR>
12 }<BR>
13<BR>
14 # GLOBALS:<BR>
15 # $height = of image<BR>
16 # $width = of image<BR>
17 # $maxfans = max fanout per branch<BR>
18 # $fanAngle = angle per fanout<BR>
19 # $fanRatio = new branch length / parent
branch length<BR>
20 #<BR>
21 # INPUT PARAMETERS:<BR>
22 # $h = current depth<BR>
23 # $x = current x<BR>
24 # $y = current y<BR>
25 # $theta = angle to start from<BR>
26 # $flen = length of parent
branch<BR>
27 #<BR>
28 # LOCAL PARAMETERS:<BR>
29 # $nx,$ny = offsets from current $x,$y
<BR>
30 # $i = angle to draw<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -