1GD(3) User Contributed Perl Documentation GD(3)
2
3
4
6 GD.pm - Interface to Gd Graphics Library
7
9 use GD;
10
11 # create a new image
12 $im = new GD::Image(100,100);
13
14 # allocate some colors
15 $white = $im->colorAllocate(255,255,255);
16 $black = $im->colorAllocate(0,0,0);
17 $red = $im->colorAllocate(255,0,0);
18 $blue = $im->colorAllocate(0,0,255);
19
20 # make the background transparent and interlaced
21 $im->transparent($white);
22 $im->interlaced('true');
23
24 # Put a black frame around the picture
25 $im->rectangle(0,0,99,99,$black);
26
27 # Draw a blue oval
28 $im->arc(50,50,95,75,0,360,$blue);
29
30 # And fill it with red
31 $im->fill(50,50,$red);
32
33 # make sure we are writing to a binary stream
34 binmode STDOUT;
35
36 # Convert the image to PNG and print it on standard output
37 print $im->png;
38
40 GD.pm is a Perl interface to Thomas Boutell's gd graphics library
41 (version 2.01 or higher; see below). GD allows you to create color
42 drawings using a large number of graphics primitives, and emit the
43 drawings as PNG files.
44
45 GD defines the following four classes:
46
47 "GD::Image"
48 An image class, which holds the image data and accepts graphic
49 primitive method calls.
50
51 "GD::Font"
52 A font class, which holds static font information and used for
53 text rendering.
54
55 "GD::Polygon"
56 A simple polygon object, used for storing lists of vertices prior
57 to rendering a polygon into an image.
58
59 "GD::Simple"
60 A "simple" class that simplifies the GD::Image API and then adds a
61 set of object-oriented drawing methods using turtle graphics,
62 simplified font handling, ability to work in polar coordinates,
63 HSV color spaces, and human-readable color names like "lightblue".
64 Please see GD::Simple for a description of these methods.
65
66 A Simple Example:
67
68 #!/usr/local/bin/perl
69
70 use GD;
71
72 # create a new image
73 $im = new GD::Image(100,100);
74
75 # allocate some colors
76 $white = $im->colorAllocate(255,255,255);
77 $black = $im->colorAllocate(0,0,0);
78 $red = $im->colorAllocate(255,0,0);
79 $blue = $im->colorAllocate(0,0,255);
80
81 # make the background transparent and interlaced
82 $im->transparent($white);
83 $im->interlaced('true');
84
85 # Put a black frame around the picture
86 $im->rectangle(0,0,99,99,$black);
87
88 # Draw a blue oval
89 $im->arc(50,50,95,75,0,360,$blue);
90
91 # And fill it with red
92 $im->fill(50,50,$red);
93
94 # make sure we are writing to a binary stream
95 binmode STDOUT;
96
97 # Convert the image to PNG and print it on standard output
98 print $im->png;
99
100 Notes:
101
102 1. To create a new, empty image, send a new() message to GD::Image,
103 passing it the width and height of the image you want to create. An
104 image object will be returned. Other class methods allow you to
105 initialize an image from a preexisting JPG, PNG, GD, GD2 or XBM file.
106 2. Next you will ordinarily add colors to the image's color table.
107 colors are added using a colorAllocate() method call. The three
108 parameters in each call are the red, green and blue (rgb) triples for
109 the desired color. The method returns the index of that color in the
110 image's color table. You should store these indexes for later use.
111 3. Now you can do some drawing! The various graphics primitives are
112 described below. In this example, we do some text drawing, create an
113 oval, and create and draw a polygon.
114 4. Polygons are created with a new() message to GD::Polygon. You can
115 add points to the returned polygon one at a time using the addPt()
116 method. The polygon can then be passed to an image for rendering.
117 5. When you're done drawing, you can convert the image into PNG format
118 by sending it a png() message. It will return a (potentially large)
119 scalar value containing the binary data for the image. Ordinarily you
120 will print it out at this point or write it to a file. To ensure
121 portability to platforms that differentiate between text and binary
122 files, be sure to call "binmode()" on the file you are writing the
123 image to.
124
126 The following class methods allow you to create new GD::Image objects.
127
128 $image = GD::Image->new([$width,$height],[$truecolor])
129 $image = GD::Image->new(*FILEHANDLE)
130 $image = GD::Image->new($filename)
131 $image = GD::Image->new($data)
132 The new() method is the main constructor for the GD::Image class.
133 Called with two integer arguments, it creates a new blank image of
134 the specified width and height. For example:
135
136 $myImage = new GD::Image(100,100) || die;
137
138 This will create an image that is 100 x 100 pixels wide. If you
139 don't specify the dimensions, a default of 64 x 64 will be chosen.
140
141 The optional third argument, $truecolor, tells new() to create a
142 truecolor GD::Image object. Truecolor images have 24 bits of color
143 data (eight bits each in the red, green and blue channels
144 respectively), allowing for precise photograph-quality color usage.
145 If not specified, the image will use an 8-bit palette for
146 compatibility with older versions of libgd.
147
148 Alternatively, you may create a GD::Image object based on an
149 existing image by providing an open filehandle, a filename, or the
150 image data itself. The image formats automatically recognized and
151 accepted are: PNG, JPEG, XPM and GD2. Other formats, including
152 WBMP, and GD version 1, cannot be recognized automatically at this
153 time.
154
155 If something goes wrong (e.g. insufficient memory), this call will
156 return undef.
157
158 $image = GD::Image->trueColor([0,1])
159 For backwards compatibility with scripts previous versions of GD,
160 new images created from scratch (width, height) are palette based
161 by default. To change this default to create true color images
162 use:
163
164 GD::Image->trueColor(1);
165
166 somewhere before creating new images. To switch back to palette
167 based by default, use:
168
169 GD::Image->trueColor(0);
170
171 $image = GD::Image->newPalette([$width,$height])
172 $image = GD::Image->newTrueColor([$width,$height])
173 The newPalette() and newTrueColor() methods can be used to
174 explicitly create an palette based or true color image regardless
175 of the current setting of trueColor().
176
177 $image = GD::Image->newFromPng($file, [$truecolor])
178 $image = GD::Image->newFromPngData($data, [$truecolor])
179 The newFromPng() method will create an image from a PNG file read
180 in through the provided filehandle or file path. The filehandle
181 must previously have been opened on a valid PNG file or pipe. If
182 successful, this call will return an initialized image which you
183 can then manipulate as you please. If it fails, which usually
184 happens if the thing at the other end of the filehandle is not a
185 valid PNG file, the call returns undef. Notice that the call
186 doesn't automatically close the filehandle for you. But it does
187 call "binmode(FILEHANDLE)" for you, on platforms where this
188 matters.
189
190 You may use any of the following as the argument:
191
192 1) a simple filehandle, such as STDIN
193 2) a filehandle glob, such as *PNG
194 3) a reference to a glob, such as \*PNG
195 4) an IO::Handle object
196 5) the pathname of a file
197
198 In the latter case, newFromPng() will attempt to open the file for
199 you and read the PNG information from it.
200
201 Example1:
202
203 open (PNG,"barnswallow.png") || die;
204 $myImage = newFromPng GD::Image(\*PNG) || die;
205 close PNG;
206
207 Example2:
208 $myImage = newFromPng GD::Image('barnswallow.png');
209
210 To get information about the size and color usage of the
211 information, you can call the image query methods described below.
212 Images created by reading PNG images will be truecolor if the image
213 file itself is truecolor. To force the image to be palette-based,
214 pass a value of 0 in the optional $truecolor argument.
215
216 The newFromPngData() method will create a new GD::Image initialized
217 with the PNG format data contained in $data.
218
219 $image = GD::Image->newFromJpeg($file, [$truecolor])
220 $image = GD::Image->newFromJpegData($data, [$truecolor])
221 These methods will create an image from a JPEG file. They work
222 just like newFromPng() and newFromPngData(), and will accept the
223 same filehandle and pathname arguments.
224
225 Images created by reading JPEG images will always be truecolor. To
226 force the image to be palette-based, pass a value of 0 in the
227 optional $truecolor argument.
228
229 $image = GD::Image->newFromGif($file)
230 $image = GD::Image->newFromGifData($data)
231 These methods will create an image from a GIF file. They work just
232 like newFromPng() and newFromPngData(), and will accept the same
233 filehandle and pathname arguments.
234
235 Images created from GIFs are always 8-bit palette images. To
236 convert to truecolor, you must create a truecolor image and then
237 perform a copy.
238
239 $image = GD::Image->newFromXbm($file)
240 This works in exactly the same way as "newFromPng", but reads the
241 contents of an X Bitmap (black & white) file:
242
243 open (XBM,"coredump.xbm") || die;
244 $myImage = newFromXbm GD::Image(\*XBM) || die;
245 close XBM;
246
247 There is no newFromXbmData() function, because there is no
248 corresponding function in the gd library.
249
250 $image = GD::Image->newFromGd($file)
251 $image = GD::Image->newFromGdData($data)
252 These methods initialize a GD::Image from a Gd file, filehandle, or
253 data. Gd is Tom Boutell's disk-based storage format, intended for
254 the rare case when you need to read and write the image to disk
255 quickly. It's not intended for regular use, because, unlike PNG or
256 JPEG, no image compression is performed and these files can become
257 BIG.
258
259 $myImage = newFromGd GD::Image("godzilla.gd") || die;
260 close GDF;
261
262 $image = GD::Image->newFromGd2($file)
263 $image = GD::Image->newFromGd2Data($data)
264 This works in exactly the same way as "newFromGd()" and
265 newFromGdData, but use the new compressed GD2 image format.
266
267 $image = GD::Image->newFromGd2Part($file,srcX,srcY,width,height)
268 This class method allows you to read in just a portion of a GD2
269 image file. In addition to a filehandle, it accepts the top-left
270 corner and dimensions (width,height) of the region of the image to
271 read. For example:
272
273 open (GDF,"godzilla.gd2") || die;
274 $myImage = GD::Image->newFromGd2Part(\*GDF,10,20,100,100) || die;
275 close GDF;
276
277 This reads a 100x100 square portion of the image starting from
278 position (10,20).
279
280 $image = GD::Image->newFromXpm($filename)
281 This creates a new GD::Image object starting from a filename. This
282 is unlike the other newFrom() functions because it does not take a
283 filehandle. This difference comes from an inconsistency in the
284 underlying gd library.
285
286 $myImage = newFromXpm GD::Image('earth.xpm') || die;
287
288 This function is only available if libgd was compiled with XPM
289 support.
290
291 NOTE: The libgd library is unable to read certain XPM files,
292 returning an all-black image instead.
293
295 Once a GD::Image object is created, you can draw with it, copy it, and
296 merge two images. When you are finished manipulating the object, you
297 can convert it into a standard image file format to output or save to a
298 file.
299
300 Image Data Output Methods
301 The following methods convert the internal drawing format into standard
302 output file formats.
303
304 $pngdata = $image->png([$compression_level])
305 This returns the image data in PNG format. You can then print it,
306 pipe it to a display program, or write it to a file. Example:
307
308 $png_data = $myImage->png;
309 open (DISPLAY,"| display -") || die;
310 binmode DISPLAY;
311 print DISPLAY $png_data;
312 close DISPLAY;
313
314 Note the use of "binmode()". This is crucial for portability to
315 DOSish platforms.
316
317 The optional $compression_level argument controls the amount of
318 compression to apply to the output PNG image. Values range from
319 0-9, where 0 means no compression (largest files, highest quality)
320 and 9 means maximum compression (smallest files, worst quality). A
321 compression level of -1 uses the default compression level selected
322 when zlib was compiled on your system, and is the same as calling
323 png() with no argument. Be careful not to confuse this argument
324 with the jpeg() quality argument, which ranges from 0-100 and has
325 the opposite meaning from compression (higher numbers give higher
326 quality).
327
328 $gifdata = $image->gifanimbegin([$GlobalCM [, $Loops]])
329 For libgd version 2.0.33 and higher, this call begins an animated
330 GIF by returning the data that comprises animated gif image file
331 header. After you call this method, call gifanimadd() one or more
332 times to add the frames of the image. Then call gifanimend(). Each
333 frame must be the same width and height.
334
335 A typical sequence will look like this:
336
337 my $gifdata = $image->gifanimbegin;
338 $gifdata .= $image->gifanimadd; # first frame
339 for (1..100) {
340 # make a frame of right size
341 my $frame = GD::Image->new($image->getBounds);
342 add_frame_data($frame); # add the data for this frame
343 $gifdata .= $frame->gifanimadd; # add frame
344 }
345 $gifdata .= $image->gifanimend; # finish the animated GIF
346 print $gifdata; # write animated gif to STDOUT
347
348 If you do not wish to store the data in memory, you can print it to
349 stdout or a file.
350
351 The image that you call gifanimbegin on is used to set the image
352 size, color resolution and color map. If argument $GlobalCM is 1,
353 the image color map becomes the GIF89a global color map. If $Loops
354 is given and >= 0, the NETSCAPE2.0 application extension is
355 created, with looping count. Looping count 0 means forever.
356
357 $gifdata = $image->gifanimadd([$LocalCM [, $LeftOfs [, $TopOfs [,
358 $Delay [, $Disposal [, $previm]]]]]])
359 Returns the data that comprises one animated gif image frame. You
360 can then print it, pipe it to a display program, or write it to a
361 file. With $LeftOfs and $TopOfs you can place this frame in
362 different offset than (0,0) inside the image screen. Delay between
363 the previous frame and this frame is in 1/100s units. Disposal is
364 usually and by default 1. Compression is activated by giving the
365 previous image as a parameter. This function then compares the
366 images and only writes the changed pixels to the new frame in
367 animation. The Disposal parameter for optimized animations must be
368 set to 1, also for the first frame. $LeftOfs and $TopOfs
369 parameters are ignored for optimized frames.
370
371 $gifdata = $image->gifanimend()
372 Returns the data for end segment of animated gif file. It always
373 returns string ';'. This string must be printed to an animated gif
374 file after all image frames to properly terminate it according to
375 GIF file syntax. Image object is not used at all in this method.
376
377 $jpegdata = $image->jpeg([$quality])
378 This returns the image data in JPEG format. You can then print it,
379 pipe it to a display program, or write it to a file. You may pass
380 an optional quality score to jpeg() in order to control the JPEG
381 quality. This should be an integer between 0 and 100. Higher
382 quality scores give larger files and better image quality. If you
383 don't specify the quality, jpeg() will choose a good default.
384
385 $gifdata = $image->gif().
386 This returns the image data in GIF format. You can then print it,
387 pipe it to a display program, or write it to a file.
388
389 $gddata = $image->gd
390 This returns the image data in GD format. You can then print it,
391 pipe it to a display program, or write it to a file. Example:
392
393 binmode MYOUTFILE;
394 print MYOUTFILE $myImage->gd;
395
396 $gd2data = $image->gd2
397 Same as gd(), except that it returns the data in compressed GD2
398 format.
399
400 $wbmpdata = $image->wbmp([$foreground])
401 This returns the image data in WBMP format, which is a black-and-
402 white image format. Provide the index of the color to become the
403 foreground color. All other pixels will be considered background.
404
405 Color Control
406 These methods allow you to control and manipulate the GD::Image color
407 table.
408
409 $index = $image->colorAllocate(red,green,blue)
410 This allocates a color with the specified red, green and blue
411 components and returns its index in the color table, if specified.
412 The first color allocated in this way becomes the image's
413 background color. (255,255,255) is white (all pixels on). (0,0,0)
414 is black (all pixels off). (255,0,0) is fully saturated red.
415 (127,127,127) is 50% gray. You can find plenty of examples in
416 /usr/X11/lib/X11/rgb.txt.
417
418 If no colors are allocated, then this function returns -1.
419
420 Example:
421
422 $white = $myImage->colorAllocate(0,0,0); #background color
423 $black = $myImage->colorAllocate(255,255,255);
424 $peachpuff = $myImage->colorAllocate(255,218,185);
425
426 $index = $image->colorAllocateAlpha(reg,green,blue,alpha)
427 This allocates a color with the specified red, green, and blue
428 components, plus the specified alpha channel. The alpha value may
429 range from 0 (opaque) to 127 (transparent). The "alphaBlending"
430 function changes the way this alpha channel affects the resulting
431 image.
432
433 $image->colorDeallocate(colorIndex)
434 This marks the color at the specified index as being ripe for
435 reallocation. The next time colorAllocate is used, this entry will
436 be replaced. You can call this method several times to deallocate
437 multiple colors. There's no function result from this call.
438
439 Example:
440
441 $myImage->colorDeallocate($peachpuff);
442 $peachy = $myImage->colorAllocate(255,210,185);
443
444 $index = $image->colorClosest(red,green,blue)
445 This returns the index of the color closest in the color table to
446 the red green and blue components specified. If no colors have yet
447 been allocated, then this call returns -1.
448
449 Example:
450
451 $apricot = $myImage->colorClosest(255,200,180);
452
453 $index = $image->colorClosestHWB(red,green,blue)
454 This also attempts to return the color closest in the color table
455 to the red green and blue components specified. It uses a
456 Hue/White/Black color representation to make the selected color
457 more likely to match human perceptions of similar colors.
458
459 If no colors have yet been allocated, then this call returns -1.
460
461 Example:
462
463 $mostred = $myImage->colorClosestHWB(255,0,0);
464
465 $index = $image->colorExact(red,green,blue)
466 This returns the index of a color that exactly matches the
467 specified red green and blue components. If such a color is not in
468 the color table, this call returns -1.
469
470 $rosey = $myImage->colorExact(255,100,80);
471 warn "Everything's coming up roses.\n" if $rosey >= 0;
472
473 $index = $image->colorResolve(red,green,blue)
474 This returns the index of a color that exactly matches the
475 specified red green and blue components. If such a color is not in
476 the color table and there is room, then this method allocates the
477 color in the color table and returns its index.
478
479 $rosey = $myImage->colorResolve(255,100,80);
480 warn "Everything's coming up roses.\n" if $rosey >= 0;
481
482 $colorsTotal = $image->colorsTotal object method
483 This returns the total number of colors allocated in the object.
484
485 $maxColors = $myImage->colorsTotal;
486
487 In the case of a TrueColor image, this call will return undef.
488
489 $index = $image->getPixel(x,y) object method
490 This returns the color table index underneath the specified point.
491 It can be combined with rgb() to obtain the rgb color underneath
492 the pixel.
493
494 Example:
495
496 $index = $myImage->getPixel(20,100);
497 ($r,$g,$b) = $myImage->rgb($index);
498
499 ($red,$green,$blue) = $image->rgb($index)
500 This returns a list containing the red, green and blue components
501 of the specified color index.
502
503 Example:
504
505 @RGB = $myImage->rgb($peachy);
506
507 $image->transparent($colorIndex)
508 This marks the color at the specified index as being transparent.
509 Portions of the image drawn in this color will be invisible. This
510 is useful for creating paintbrushes of odd shapes, as well as for
511 making PNG backgrounds transparent for displaying on the Web. Only
512 one color can be transparent at any time. To disable transparency,
513 specify -1 for the index.
514
515 If you call this method without any parameters, it will return the
516 current index of the transparent color, or -1 if none.
517
518 Example:
519
520 open(PNG,"test.png");
521 $im = newFromPng GD::Image(PNG);
522 $white = $im->colorClosest(255,255,255); # find white
523 $im->transparent($white);
524 binmode STDOUT;
525 print $im->png;
526
527 Special Colors
528 GD implements a number of special colors that can be used to achieve
529 special effects. They are constants defined in the GD:: namespace, but
530 automatically exported into your namespace when the GD module is
531 loaded.
532
533 $image->setBrush($image)
534 You can draw lines and shapes using a brush pattern. Brushes are
535 just images that you can create and manipulate in the usual way.
536 When you draw with them, their contents are used for the color and
537 shape of the lines.
538
539 To make a brushed line, you must create or load the brush first,
540 then assign it to the image using setBrush(). You can then draw in
541 that with that brush using the gdBrushed special color. It's often
542 useful to set the background of the brush to transparent so that
543 the non-colored parts don't overwrite other parts of your image.
544
545 Example:
546
547 # Create a brush at an angle
548 $diagonal_brush = new GD::Image(5,5);
549 $white = $diagonal_brush->colorAllocate(255,255,255);
550 $black = $diagonal_brush->colorAllocate(0,0,0);
551 $diagonal_brush->transparent($white);
552 $diagonal_brush->line(0,4,4,0,$black); # NE diagonal
553
554 # Set the brush
555 $myImage->setBrush($diagonal_brush);
556
557 # Draw a circle using the brush
558 $myImage->arc(50,50,25,25,0,360,gdBrushed);
559
560 $image->setThickness($thickness)
561 Lines drawn with line(), rectangle(), arc(), and so forth are 1
562 pixel thick by default. Call setThickness() to change the line
563 drawing width.
564
565 $image->setStyle(@colors)
566 Styled lines consist of an arbitrary series of repeated colors and
567 are useful for generating dotted and dashed lines. To create a
568 styled line, use setStyle() to specify a repeating series of
569 colors. It accepts an array consisting of one or more color
570 indexes. Then draw using the gdStyled special color. Another
571 special color, gdTransparent can be used to introduce holes in the
572 line, as the example shows.
573
574 Example:
575
576 # Set a style consisting of 4 pixels of yellow,
577 # 4 pixels of blue, and a 2 pixel gap
578 $myImage->setStyle($yellow,$yellow,$yellow,$yellow,
579 $blue,$blue,$blue,$blue,
580 gdTransparent,gdTransparent);
581 $myImage->arc(50,50,25,25,0,360,gdStyled);
582
583 To combine the "gdStyled" and "gdBrushed" behaviors, you can
584 specify "gdStyledBrushed". In this case, a pixel from the current
585 brush pattern is rendered wherever the color specified in
586 setStyle() is neither gdTransparent nor 0.
587
588 gdTiled
589 Draw filled shapes and flood fills using a pattern. The pattern is
590 just another image. The image will be tiled multiple times in
591 order to fill the required space, creating wallpaper effects. You
592 must call "setTile" in order to define the particular tile pattern
593 you'll use for drawing when you specify the gdTiled color.
594 details.
595
596 gdStyled
597 The gdStyled color is used for creating dashed and dotted lines. A
598 styled line can contain any series of colors and is created using
599 the setStyled() command.
600
601 gdAntiAliased
602 The "gdAntiAliased" color is used for drawing lines with
603 antialiasing turned on. Antialiasing will blend the jagged edges
604 of lines with the background, creating a smoother look. The actual
605 color drawn is set with setAntiAliased().
606
607 $image->setAntiAliased($color)
608 "Antialiasing" is a process by which jagged edges associated with
609 line drawing can be reduced by blending the foreground color with
610 an appropriate percentage of the background, depending on how much
611 of the pixel in question is actually within the boundaries of the
612 line being drawn. All line-drawing methods, such as line() and
613 polygon, will draw antialiased lines if the special "color"
614 gdAntiAliased is used when calling them.
615
616 setAntiAliased() is used to specify the actual foreground color to
617 be used when drawing antialiased lines. You may set any color to be
618 the foreground, however as of libgd version 2.0.12 an alpha channel
619 component is not supported.
620
621 Antialiased lines can be drawn on both truecolor and palette-based
622 images. However, attempts to draw antialiased lines on highly
623 complex palette-based backgrounds may not give satisfactory
624 results, due to the limited number of colors available in the
625 palette. Antialiased line-drawing on simple backgrounds should work
626 well with palette-based images; otherwise create or fetch a
627 truecolor image instead. When using palette-based images, be sure
628 to allocate a broad spectrum of colors in order to have sufficient
629 colors for the antialiasing to use.
630
631 $image->setAntiAliasedDontBlend($color,[$flag])
632 Normally, when drawing lines with the special gdAntiAliased
633 "color," blending with the background to reduce jagged edges is the
634 desired behavior. However, when it is desired that lines not be
635 blended with one particular color when it is encountered in the
636 background, the setAntiAliasedDontBlend() method can be used to
637 indicate the special color that the foreground should stand out
638 more clearly against.
639
640 Once turned on, you can turn this feature off by calling
641 setAntiAliasedDontBlend() with a second argument of 0:
642
643 $image->setAntiAliasedDontBlend($color,0);
644
645 Drawing Commands
646 These methods allow you to draw lines, rectangles, and ellipses, as
647 well as to perform various special operations like flood-fill.
648
649 $image->setPixel($x,$y,$color)
650 This sets the pixel at (x,y) to the specified color index. No
651 value is returned from this method. The coordinate system starts
652 at the upper left at (0,0) and gets larger as you go down and to
653 the right. You can use a real color, or one of the special colors
654 gdBrushed, gdStyled and gdStyledBrushed can be specified.
655
656 Example:
657
658 # This assumes $peach already allocated
659 $myImage->setPixel(50,50,$peach);
660
661 $image->line($x1,$y1,$x2,$y2,$color)
662 This draws a line from (x1,y1) to (x2,y2) of the specified color.
663 You can use a real color, or one of the special colors gdBrushed,
664 gdStyled and gdStyledBrushed.
665
666 Example:
667
668 # Draw a diagonal line using the currently defined
669 # paintbrush pattern.
670 $myImage->line(0,0,150,150,gdBrushed);
671
672 $image->dashedLine($x1,$y1,$x2,$y2,$color)
673 DEPRECATED: The libgd library provides this method solely for
674 backward compatibility with libgd version 1.0, and there have been
675 reports that it no longer works as expected. Please use the
676 setStyle() and gdStyled methods as described below.
677
678 This draws a dashed line from (x1,y1) to (x2,y2) in the specified
679 color. A more powerful way to generate arbitrary dashed and dotted
680 lines is to use the setStyle() method described below and to draw
681 with the special color gdStyled.
682
683 Example:
684
685 $myImage->dashedLine(0,0,150,150,$blue);
686
687 $image->rectangle($x1,$y1,$x2,$y2,$color)
688 This draws a rectangle with the specified color. (x1,y1) and
689 (x2,y2) are the upper left and lower right corners respectively.
690 Both real color indexes and the special colors gdBrushed, gdStyled
691 and gdStyledBrushed are accepted.
692
693 Example:
694
695 $myImage->rectangle(10,10,100,100,$rose);
696
697 $image->filledRectangle($x1,$y1,$x2,$y2,$color)
698 This draws a rectangle filed with the specified color. You can use
699 a real color, or the special fill color gdTiled to fill the polygon
700 with a pattern.
701
702 Example:
703
704 # read in a fill pattern and set it
705 $tile = newFromPng GD::Image('happyface.png');
706 $myImage->setTile($tile);
707
708 # draw the rectangle, filling it with the pattern
709 $myImage->filledRectangle(10,10,150,200,gdTiled);
710
711 $image->openPolygon($polygon,$color)
712 This draws a polygon with the specified color. The polygon must be
713 created first (see below). The polygon must have at least three
714 vertices. If the last vertex doesn't close the polygon, the method
715 will close it for you. Both real color indexes and the special
716 colors gdBrushed, gdStyled and gdStyledBrushed can be specified.
717
718 Example:
719
720 $poly = new GD::Polygon;
721 $poly->addPt(50,0);
722 $poly->addPt(99,99);
723 $poly->addPt(0,99);
724 $myImage->openPolygon($poly,$blue);
725
726 $image->unclosedPolygon($polygon,$color)
727 This draws a sequence of connected lines with the specified color,
728 without connecting the first and last point to a closed polygon.
729 The polygon must be created first (see below). The polygon must
730 have at least three vertices. Both real color indexes and the
731 special colors gdBrushed, gdStyled and gdStyledBrushed can be
732 specified.
733
734 You need libgd 2.0.33 or higher to use this feature.
735
736 Example:
737
738 $poly = new GD::Polygon;
739 $poly->addPt(50,0);
740 $poly->addPt(99,99);
741 $poly->addPt(0,99);
742 $myImage->unclosedPolygon($poly,$blue);
743
744 $image->filledPolygon($poly,$color)
745 This draws a polygon filled with the specified color. You can use
746 a real color, or the special fill color gdTiled to fill the polygon
747 with a pattern.
748
749 Example:
750
751 # make a polygon
752 $poly = new GD::Polygon;
753 $poly->addPt(50,0);
754 $poly->addPt(99,99);
755 $poly->addPt(0,99);
756
757 # draw the polygon, filling it with a color
758 $myImage->filledPolygon($poly,$peachpuff);
759
760 $image->ellipse($cx,$cy,$width,$height,$color)
761 $image->filledEllipse($cx,$cy,$width,$height,$color)
762 These methods() draw ellipses. ($cx,$cy) is the center of the arc,
763 and ($width,$height) specify the ellipse width and height,
764 respectively. filledEllipse() is like Ellipse() except that the
765 former produces filled versions of the ellipse.
766
767 $image->arc($cx,$cy,$width,$height,$start,$end,$color)
768 This draws arcs and ellipses. (cx,cy) are the center of the arc,
769 and (width,height) specify the width and height, respectively. The
770 portion of the ellipse covered by the arc are controlled by start
771 and end, both of which are given in degrees from 0 to 360. Zero is
772 at the top of the ellipse, and angles increase clockwise. To
773 specify a complete ellipse, use 0 and 360 as the starting and
774 ending angles. To draw a circle, use the same value for width and
775 height.
776
777 You can specify a normal color or one of the special colors
778 gdBrushed, gdStyled, or gdStyledBrushed.
779
780 Example:
781
782 # draw a semicircle centered at 100,100
783 $myImage->arc(100,100,50,50,0,180,$blue);
784
785 $image->filledArc($cx,$cy,$width,$height,$start,$end,$color
786 [,$arc_style])
787 This method is like arc() except that it colors in the pie wedge
788 with the selected color. $arc_style is optional. If present it is
789 a bitwise OR of the following constants:
790
791 gdArc connect start & end points of arc with a rounded edge
792 gdChord connect start & end points of arc with a straight line
793 gdPie synonym for gdChord
794 gdNoFill outline the arc or chord
795 gdEdged connect beginning and ending of the arc to the center
796
797 gdArc and gdChord are mutually exclusive. gdChord just connects
798 the starting and ending angles with a straight line, while gdArc
799 produces a rounded edge. gdPie is a synonym for gdArc. gdNoFill
800 indicates that the arc or chord should be outlined, not filled.
801 gdEdged, used together with gdNoFill, indicates that the beginning
802 and ending angles should be connected to the center; this is a good
803 way to outline (rather than fill) a "pie slice."
804
805 Example:
806
807 $image->filledArc(100,100,50,50,0,90,$blue,gdEdged|gdNoFill);
808
809 $image->fill($x,$y,$color)
810 This method flood-fills regions with the specified color. The
811 color will spread through the image, starting at point (x,y), until
812 it is stopped by a pixel of a different color from the starting
813 pixel (this is similar to the "paintbucket" in many popular drawing
814 toys). You can specify a normal color, or the special color
815 gdTiled, to flood-fill with patterns.
816
817 Example:
818
819 # Draw a rectangle, and then make its interior blue
820 $myImage->rectangle(10,10,100,100,$black);
821 $myImage->fill(50,50,$blue);
822
823 $image->fillToBorder($x,$y,$bordercolor,$color)
824 Like "fill", this method flood-fills regions with the specified
825 color, starting at position (x,y). However, instead of stopping
826 when it hits a pixel of a different color than the starting pixel,
827 flooding will only stop when it hits the color specified by
828 bordercolor. You must specify a normal indexed color for the
829 bordercolor. However, you are free to use the gdTiled color for
830 the fill.
831
832 Example:
833
834 # This has the same effect as the previous example
835 $myImage->rectangle(10,10,100,100,$black);
836 $myImage->fillToBorder(50,50,$black,$blue);
837
838 Image Copying Commands
839 Two methods are provided for copying a rectangular region from one
840 image to another. One method copies a region without resizing it. The
841 other allows you to stretch the region during the copy operation.
842
843 With either of these methods it is important to know that the routines
844 will attempt to flesh out the destination image's color table to match
845 the colors that are being copied from the source. If the destination's
846 color table is already full, then the routines will attempt to find the
847 best match, with varying results.
848
849 $image->copy($sourceImage,$dstX,$dstY,
850 $srcX,$srcY,$width,$height)
851
852 This is the simplest of the several copy operations, copying the
853 specified region from the source image to the destination image
854 (the one performing the method call). (srcX,srcY) specify the
855 upper left corner of a rectangle in the source image, and
856 (width,height) give the width and height of the region to copy.
857 (dstX,dstY) control where in the destination image to stamp the
858 copy. You can use the same image for both the source and the
859 destination, but the source and destination regions must not
860 overlap or strange things will happen.
861
862 Example:
863
864 $myImage = new GD::Image(100,100);
865 ... various drawing stuff ...
866 $srcImage = new GD::Image(50,50);
867 ... more drawing stuff ...
868 # copy a 25x25 pixel region from $srcImage to
869 # the rectangle starting at (10,10) in $myImage
870 $myImage->copy($srcImage,10,10,0,0,25,25);
871
872 $image->clone()
873 Make a copy of the image and return it as a new object. The new
874 image will look identical. However, it may differ in the size of
875 the color palette and other nonessential details.
876
877 Example:
878
879 $myImage = new GD::Image(100,100);
880 ... various drawing stuff ...
881 $copy = $myImage->clone;
882
883 $image->copyMerge($sourceImage,$dstX,$dstY,
884 $srcX,$srcY,$width,$height,$percent)
885
886 This copies the indicated rectangle from the source image to the
887 destination image, merging the colors to the extent specified by
888 percent (an integer between 0 and 100). Specifying 100% has the
889 same effect as copy() -- replacing the destination pixels with the
890 source image. This is most useful for highlighting an area by
891 merging in a solid rectangle.
892
893 Example:
894
895 $myImage = new GD::Image(100,100);
896 ... various drawing stuff ...
897 $redImage = new GD::Image(50,50);
898 ... more drawing stuff ...
899 # copy a 25x25 pixel region from $srcImage to
900 # the rectangle starting at (10,10) in $myImage, merging 50%
901 $myImage->copyMerge($srcImage,10,10,0,0,25,25,50);
902
903 $image->copyMergeGray($sourceImage,$dstX,$dstY,
904 $srcX,$srcY,$width,$height,$percent)
905
906 This is identical to copyMerge() except that it preserves the hue
907 of the source by converting all the pixels of the destination
908 rectangle to grayscale before merging.
909
910 $image->copyResized($sourceImage,$dstX,$dstY,
911 $srcX,$srcY,$destW,$destH,$srcW,$srcH)
912
913 This method is similar to copy() but allows you to choose different
914 sizes for the source and destination rectangles. The source and
915 destination rectangle's are specified independently by (srcW,srcH)
916 and (destW,destH) respectively. copyResized() will stretch or
917 shrink the image to accommodate the size requirements.
918
919 Example:
920
921 $myImage = new GD::Image(100,100);
922 ... various drawing stuff ...
923 $srcImage = new GD::Image(50,50);
924 ... more drawing stuff ...
925 # copy a 25x25 pixel region from $srcImage to
926 # a larger rectangle starting at (10,10) in $myImage
927 $myImage->copyResized($srcImage,10,10,0,0,50,50,25,25);
928
929 $image->copyResampled($sourceImage,$dstX,$dstY,
930 $srcX,$srcY,$destW,$destH,$srcW,$srcH)
931
932 This method is similar to copyResized() but provides "smooth"
933 copying from a large image to a smaller one, using a weighted
934 average of the pixels of the source area rather than selecting one
935 representative pixel. This method is identical to copyResized()
936 when the destination image is a palette image.
937
938 $image->copyRotated($sourceImage,$dstX,$dstY,
939 $srcX,$srcY,$width,$height,$angle)
940
941 Like copyResized() but the $angle argument specifies an arbitrary
942 amount to rotate the image clockwise (in degrees). In addition,
943 $dstX and $dstY species the center of the destination image, and
944 not the top left corner.
945
946 $image->trueColorToPalette([$dither], [$colors])
947 This method converts a truecolor image to a palette image. The code
948 for this function was originally drawn from the Independent JPEG
949 Group library code, which is excellent. The code has been modified
950 to preserve as much alpha channel information as possible in the
951 resulting palette, in addition to preserving colors as well as
952 possible. This does not work as well as might be hoped. It is
953 usually best to simply produce a truecolor output image instead,
954 which guarantees the highest output quality. Both the dithering
955 (0/1, default=0) and maximum number of colors used (<=256, default
956 = gdMaxColors) can be specified.
957
958 Image Transformation Commands
959 Gd also provides some common image transformations:
960
961 $image = $sourceImage->copyRotate90()
962 $image = $sourceImage->copyRotate180()
963 $image = $sourceImage->copyRotate270()
964 $image = $sourceImage->copyFlipHorizontal()
965 $image = $sourceImage->copyFlipVertical()
966 $image = $sourceImage->copyTranspose()
967 $image = $sourceImage->copyReverseTranspose()
968 These methods can be used to rotate, flip, or transpose an image.
969 The result of the method is a copy of the image.
970
971 $image->rotate180()
972 $image->flipHorizontal()
973 $image->flipVertical()
974 These methods are similar to the copy* versions, but instead modify
975 the image in place.
976
977 Character and String Drawing
978 GD allows you to draw characters and strings, either in normal
979 horizontal orientation or rotated 90 degrees. These routines use a
980 GD::Font object, described in more detail below. There are four built-
981 in monospaced fonts, available in the global variables gdGiantFont,
982 gdLargeFont, gdMediumBoldFont, gdSmallFont and gdTinyFont.
983
984 In addition, you can use the load() method to load GD-formatted bitmap
985 font files at runtime. You can create these bitmap files from X11 BDF-
986 format files using the bdf2gd.pl script, which should have been
987 installed with GD (see the bdf_scripts directory if it wasn't). The
988 format happens to be identical to the old-style MSDOS bitmap ".fnt"
989 files, so you can use one of those directly if you happen to have one.
990
991 For writing proportional scaleable fonts, GD offers the stringFT()
992 method, which allows you to load and render any TrueType font on your
993 system.
994
995 $image->string($font,$x,$y,$string,$color)
996 This method draws a string starting at position (x,y) in the
997 specified font and color. Your choices of fonts are gdSmallFont,
998 gdMediumBoldFont, gdTinyFont, gdLargeFont and gdGiantFont.
999
1000 Example:
1001
1002 $myImage->string(gdSmallFont,2,10,"Peachy Keen",$peach);
1003
1004 $image->stringUp($font,$x,$y,$string,$color)
1005 Just like the previous call, but draws the text rotated
1006 counterclockwise 90 degrees.
1007
1008 $image->char($font,$x,$y,$char,$color)
1009 $image->charUp($font,$x,$y,$char,$color)
1010 These methods draw single characters at position (x,y) in the
1011 specified font and color. They're carry-overs from the C
1012 interface, where there is a distinction between characters and
1013 strings. Perl is insensible to such subtle distinctions.
1014
1015 $font = GD::Font->load($fontfilepath)
1016 This method dynamically loads a font file, returning a font that
1017 you can use in subsequent calls to drawing methods. For example:
1018
1019 my $courier = GD::Font->load('./courierR12.fnt') or die "Can't load font";
1020 $image->string($courier,2,10,"Peachy Keen",$peach);
1021
1022 Font files must be in GD binary format, as described above.
1023
1024 @bounds =
1025 $image->stringFT($fgcolor,$fontname,$ptsize,$angle,$x,$y,$string)
1026 @bounds =
1027 GD::Image->stringFT($fgcolor,$fontname,$ptsize,$angle,$x,$y,$string)
1028 @bounds =
1029 $image->stringFT($fgcolor,$fontname,$ptsize,$angle,$x,$y,$string,\%options)
1030 This method uses TrueType to draw a scaled, antialiased string
1031 using the TrueType vector font of your choice. It requires that
1032 libgd to have been compiled with TrueType support, and for the
1033 appropriate TrueType font to be installed on your system.
1034
1035 The arguments are as follows:
1036
1037 fgcolor Color index to draw the string in
1038 fontname A path to the TrueType (.ttf) font file or a font pattern.
1039 ptsize The desired point size (may be fractional)
1040 angle The rotation angle, in radians (positive values rotate counter clockwise)
1041 x,y X and Y coordinates to start drawing the string
1042 string The string itself
1043
1044 If successful, the method returns an eight-element list giving the
1045 boundaries of the rendered string:
1046
1047 @bounds[0,1] Lower left corner (x,y)
1048 @bounds[2,3] Lower right corner (x,y)
1049 @bounds[4,5] Upper right corner (x,y)
1050 @bounds[6,7] Upper left corner (x,y)
1051
1052 In case of an error (such as the font not being available, or FT
1053 support not being available), the method returns an empty list and
1054 sets $@ to the error message.
1055
1056 The string may contain UTF-8 sequences like: "À"
1057
1058 You may also call this method from the GD::Image class name, in
1059 which case it doesn't do any actual drawing, but returns the
1060 bounding box using an inexpensive operation. You can use this to
1061 perform layout operations prior to drawing.
1062
1063 Using a negative color index will disable antialiasing, as
1064 described in the libgd manual page at
1065 <http://www.boutell.com/gd/manual2.0.9.html#gdImageStringFT>.
1066
1067 An optional 8th argument allows you to pass a hashref of options to
1068 stringFT(). Several hashkeys are recognized: linespacing, charmap,
1069 resolution, and kerning.
1070
1071 The value of linespacing is supposed to be a multiple of the
1072 character height, so setting linespacing to 2.0 will result in
1073 double-spaced lines of text. However the current version of libgd
1074 (2.0.12) does not do this. Instead the linespacing seems to be
1075 double what is provided in this argument. So use a spacing of 0.5
1076 to get separation of exactly one line of text. In practice, a
1077 spacing of 0.6 seems to give nice results. Another thing to watch
1078 out for is that successive lines of text should be separated by the
1079 "\r\n" characters, not just "\n".
1080
1081 The value of charmap is one of "Unicode", "Shift_JIS" and "Big5".
1082 The interaction between Perl, Unicode and libgd is not clear to me,
1083 and you should experiment a bit if you want to use this feature.
1084
1085 The value of resolution is the vertical and horizontal resolution,
1086 in DPI, in the format "hdpi,vdpi". If present, the resolution will
1087 be passed to the Freetype rendering engine as a hint to improve the
1088 appearance of the rendered font.
1089
1090 The value of kerning is a flag. Set it to false to turn off the
1091 default kerning of text.
1092
1093 Example:
1094
1095 $gd->stringFT($black,'/dosc/windows/Fonts/pala.ttf',40,0,20,90,
1096 "hi there\r\nbye now",
1097 {linespacing=>0.6,
1098 charmap => 'Unicode',
1099 });
1100
1101 If GD was compiled with fontconfig support, and the fontconfig
1102 library is available on your system, then you can use a font name
1103 pattern instead of a path. Patterns are described in fontconfig
1104 and will look something like this "Times:italic". For backward
1105 compatibility, this feature is disabled by default. You must
1106 enable it by calling useFontConfig(1) prior to the stringFT() call.
1107
1108 $image->useFontConfig(1);
1109
1110 For backward compatibility with older versions of the FreeType
1111 library, the alias stringTTF() is also recognized.
1112
1113 $hasfontconfig = $image->useFontConfig($flag)
1114 Call useFontConfig() with a value of 1 in order to enable support
1115 for fontconfig font patterns (see stringFT). Regardless of the
1116 value of $flag, this method will return a true value if the
1117 fontconfig library is present, or false otherwise.
1118
1119 $result =
1120 $image-stringFTCircle($cx,$cy,$radius,$textRadius,$fillPortion,$font,$points,$top,$bottom,$fgcolor)>
1121 This draws text in a circle. Currently (libgd 2.0.33) this function
1122 does not work for me, but the interface is provided for
1123 completeness. The call signature is somewhat complex. Here is an
1124 excerpt from the libgd manual page:
1125
1126 Draws the text strings specified by top and bottom on the image,
1127 curved along the edge of a circle of radius radius, with its center
1128 at cx and cy. top is written clockwise along the top; bottom is
1129 written counterclockwise along the bottom. textRadius determines
1130 the "height" of each character; if textRadius is 1/2 of radius,
1131 characters extend halfway from the edge to the center. fillPortion
1132 varies from 0 to 1.0, with useful values from about 0.4 to 0.9, and
1133 determines how much of the 180 degrees of arc assigned to each
1134 section of text is actually occupied by text; 0.9 looks better than
1135 1.0 which is rather crowded. font is a freetype font; see
1136 gdImageStringFT. points is passed to the freetype engine and has an
1137 effect on hinting; although the size of the text is determined by
1138 radius, textRadius, and fillPortion, you should pass a point size
1139 that "hints" appropriately -- if you know the text will be large,
1140 pass a large point size such as 24.0 to get the best results.
1141 fgcolor can be any color, and may have an alpha component, do
1142 blending, etc.
1143
1144 Returns a true value on success.
1145
1146 Alpha channels
1147 The alpha channel methods allow you to control the way drawings are
1148 processed according to the alpha channel. When true color is turned on,
1149 colors are encoded as four bytes, in which the last three bytes are the
1150 RGB color values, and the first byte is the alpha channel. Therefore
1151 the hexadecimal representation of a non transparent RGB color will be:
1152 C=0x00(rr)(bb)(bb)
1153
1154 When alpha blending is turned on, you can use the first byte of the
1155 color to control the transparency, meaning that a rectangle painted
1156 with color 0x00(rr)(bb)(bb) will be opaque, and another one painted
1157 with 0x7f(rr)(gg)(bb) will be transparent. The Alpha value must be >= 0
1158 and <= 0x7f.
1159
1160 $image->alphaBlending($integer)
1161 The alphaBlending() method allows for two different modes of
1162 drawing on truecolor images. In blending mode, which is on by
1163 default (libgd 2.0.2 and above), the alpha channel component of the
1164 color supplied to all drawing functions, such as "setPixel",
1165 determines how much of the underlying color should be allowed to
1166 shine through. As a result, GD automatically blends the existing
1167 color at that point with the drawing color, and stores the result
1168 in the image. The resulting pixel is opaque. In non-blending mode,
1169 the drawing color is copied literally with its alpha channel
1170 information, replacing the destination pixel. Blending mode is not
1171 available when drawing on palette images.
1172
1173 Pass a value of 1 for blending mode, and 0 for non-blending mode.
1174
1175 $image->saveAlpha($saveAlpha)
1176 By default, GD (libgd 2.0.2 and above) does not attempt to save
1177 full alpha channel information (as opposed to single-color
1178 transparency) when saving PNG images. (PNG is currently the only
1179 output format supported by gd which can accommodate alpha channel
1180 information.) This saves space in the output file. If you wish to
1181 create an image with alpha channel information for use with tools
1182 that support it, call saveAlpha(1) to turn on saving of such
1183 information, and call alphaBlending(0) to turn off alpha blending
1184 within the library so that alpha channel information is actually
1185 stored in the image rather than being composited immediately at the
1186 time that drawing functions are invoked.
1187
1188 Miscellaneous Image Methods
1189 These are various utility methods that are useful in some
1190 circumstances.
1191
1192 $image->interlaced([$flag])
1193 This method sets or queries the image's interlaced setting.
1194 Interlace produces a cool venetian blinds effect on certain
1195 viewers. Provide a true parameter to set the interlace attribute.
1196 Provide undef to disable it. Call the method without parameters to
1197 find out the current setting.
1198
1199 ($width,$height) = $image->getBounds()
1200 This method will return a two-member list containing the width and
1201 height of the image. You query but not change the size of the
1202 image once it's created.
1203
1204 $width = $image->width
1205 $height = $image->height
1206 Return the width and height of the image, respectively.
1207
1208 $is_truecolor = $image->isTrueColor()
1209 This method will return a Boolean representing whether the image is
1210 true color or not.
1211
1212 $flag = $image1->compare($image2)
1213 Compare two images and return a bitmap describing the differences
1214 found, if any. The return value must be logically ANDed with one
1215 or more constants in order to determine the differences. The
1216 following constants are available:
1217
1218 GD_CMP_IMAGE The two images look different
1219 GD_CMP_NUM_COLORS The two images have different numbers of colors
1220 GD_CMP_COLOR The two images' palettes differ
1221 GD_CMP_SIZE_X The two images differ in the horizontal dimension
1222 GD_CMP_SIZE_Y The two images differ in the vertical dimension
1223 GD_CMP_TRANSPARENT The two images have different transparency
1224 GD_CMP_BACKGROUND The two images have different background colors
1225 GD_CMP_INTERLACE The two images differ in their interlace
1226 GD_CMP_TRUECOLOR The two images are not both true color
1227
1228 The most important of these is GD_CMP_IMAGE, which will tell you
1229 whether the two images will look different, ignoring differences in
1230 the order of colors in the color palette and other invisible
1231 changes. The constants are not imported by default, but must be
1232 imported individually or by importing the :cmp tag. Example:
1233
1234 use GD qw(:DEFAULT :cmp);
1235 # get $image1 from somewhere
1236 # get $image2 from somewhere
1237 if ($image1->compare($image2) & GD_CMP_IMAGE) {
1238 warn "images differ!";
1239 }
1240
1241 $image->clip($x1,$y1,$x2,$y2)
1242 ($x1,$y1,$x2,$y2) = $image->clip
1243 Set or get the clipping rectangle. When the clipping rectangle is
1244 set, all drawing will be clipped to occur within this rectangle.
1245 The clipping rectangle is initially set to be equal to the
1246 boundaries of the whole image. Change it by calling clip() with the
1247 coordinates of the new clipping rectangle. Calling clip() without
1248 any arguments will return the current clipping rectangle.
1249
1250 $flag = $image->boundsSafe($x,$y)
1251 The boundsSafe() method will return true if the point indicated by
1252 ($x,$y) is within the clipping rectangle, or false if it is not.
1253 If the clipping rectangle has not been set, then it will return
1254 true if the point lies within the image boundaries.
1255
1256 Grouping Methods
1257 GD does not support grouping of objects, but GD::SVG does. In that
1258 subclass, the following methods declare new groups of graphical
1259 objects:
1260
1261 $image->startGroup([$id,\%style])
1262 $image->endGroup()
1263 $group = $image->newGroup
1264 See GD::SVG for information.
1265
1267 A few primitive polygon creation and manipulation methods are provided.
1268 They aren't part of the Gd library, but I thought they might be handy
1269 to have around (they're borrowed from my qd.pl Quickdraw library).
1270 Also see GD::Polyline.
1271
1272 $poly = GD::Polygon->new
1273 Create an empty polygon with no vertices.
1274
1275 $poly = new GD::Polygon;
1276
1277 $poly->addPt($x,$y)
1278 Add point (x,y) to the polygon.
1279
1280 $poly->addPt(0,0);
1281 $poly->addPt(0,50);
1282 $poly->addPt(25,25);
1283 $myImage->fillPoly($poly,$blue);
1284
1285 ($x,$y) = $poly->getPt($index)
1286 Retrieve the point at the specified vertex.
1287
1288 ($x,$y) = $poly->getPt(2);
1289
1290 $poly->setPt($index,$x,$y)
1291 Change the value of an already existing vertex. It is an error to
1292 set a vertex that isn't already defined.
1293
1294 $poly->setPt(2,100,100);
1295
1296 ($x,$y) = $poly->deletePt($index)
1297 Delete the specified vertex, returning its value.
1298
1299 ($x,$y) = $poly->deletePt(1);
1300
1301 $poly->clear()
1302 Delete all vertices, restoring the polygon to its initial empty
1303 state.
1304
1305 $poly->toPt($dx,$dy)
1306 Draw from current vertex to a new vertex, using relative (dx,dy)
1307 coordinates. If this is the first point, act like addPt().
1308
1309 $poly->addPt(0,0);
1310 $poly->toPt(0,50);
1311 $poly->toPt(25,-25);
1312 $myImage->fillPoly($poly,$blue);
1313
1314 $vertex_count = $poly->length
1315 Return the number of vertices in the polygon.
1316
1317 $points = $poly->length;
1318
1319 @vertices = $poly->vertices
1320 Return a list of all the vertices in the polygon object. Each
1321 member of the list is a reference to an (x,y) array.
1322
1323 @vertices = $poly->vertices;
1324 foreach $v (@vertices)
1325 print join(",",@$v),"\n";
1326 }
1327
1328 @rect = $poly->bounds
1329 Return the smallest rectangle that completely encloses the polygon.
1330 The return value is an array containing the (left,top,right,bottom)
1331 of the rectangle.
1332
1333 ($left,$top,$right,$bottom) = $poly->bounds;
1334
1335 $poly->offset($dx,$dy)
1336 Offset all the vertices of the polygon by the specified horizontal
1337 (dh) and vertical (dy) amounts. Positive numbers move the polygon
1338 down and to the right.
1339
1340 $poly->offset(10,30);
1341
1342 $poly->map($srcL,$srcT,$srcR,$srcB,$destL,$dstT,$dstR,$dstB)
1343 Map the polygon from a source rectangle to an equivalent position in
1344 a destination rectangle, moving it and resizing it as necessary.
1345 See polys.pl for an example of how this works. Both the source and
1346 destination rectangles are given in (left,top,right,bottom)
1347 coordinates. For convenience, you can use the polygon's own
1348 bounding box as the source rectangle.
1349
1350 # Make the polygon really tall
1351 $poly->map($poly->bounds,0,0,50,200);
1352
1353 $poly->scale($sx,$sy)
1354 Scale each vertex of the polygon by the X and Y factors indicated by
1355 sx and sy. For example scale(2,2) will make the polygon twice as
1356 large. For best results, move the center of the polygon to position
1357 (0,0) before you scale, then move it back to its previous position.
1358
1359 $poly->transform($sx,$rx,$sy,$ry,$tx,$ty)
1360 Run each vertex of the polygon through a transformation matrix,
1361 where sx and sy are the X and Y scaling factors, rx and ry are the X
1362 and Y rotation factors, and tx and ty are X and Y offsets. See the
1363 Adobe PostScript Reference, page 154 for a full explanation, or
1364 experiment.
1365
1366 GD::Polyline
1367 Please see GD::Polyline for information on creating open polygons and
1368 splines.
1369
1371 The libgd library (used by the Perl GD library) has built-in support
1372 for about half a dozen fonts, which were converted from public-domain X
1373 Windows fonts. For more fonts, compile libgd with TrueType support and
1374 use the stringFT() call.
1375
1376 If you wish to add more built-in fonts, the directory bdf_scripts
1377 contains two contributed utilities that may help you convert X-Windows
1378 BDF-format fonts into the format that libgd uses internally. However
1379 these scripts were written for earlier versions of GD which included
1380 its own mini-gd library. These scripts will have to be adapted for use
1381 with libgd, and the libgd library itself will have to be recompiled and
1382 linked! Please do not contact me for help with these scripts: they are
1383 unsupported.
1384
1385 Each of these fonts is available both as an imported global (e.g.
1386 gdSmallFont) and as a package method (e.g. GD::Font->Small).
1387
1388 gdSmallFont
1389 GD::Font->Small
1390 This is the basic small font, "borrowed" from a well known public
1391 domain 6x12 font.
1392
1393 gdLargeFont
1394 GD::Font->Large
1395 This is the basic large font, "borrowed" from a well known public
1396 domain 8x16 font.
1397
1398 gdMediumBoldFont
1399 GD::Font->MediumBold
1400 This is a bold font intermediate in size between the small and
1401 large fonts, borrowed from a public domain 7x13 font;
1402
1403 gdTinyFont
1404 GD::Font->Tiny
1405 This is a tiny, almost unreadable font, 5x8 pixels wide.
1406
1407 gdGiantFont
1408 GD::Font->Giant
1409 This is a 9x15 bold font converted by Jan Pazdziora from a sans
1410 serif X11 font.
1411
1412 $font->nchars
1413 This returns the number of characters in the font.
1414
1415 print "The large font contains ",gdLargeFont->nchars," characters\n";
1416
1417 $font->offset
1418 This returns the ASCII value of the first character in the font
1419
1420 $width = $font->width
1421 $height = $font->height
1422 "height"
1423 These return the width and height of the font.
1424
1425 ($w,$h) = (gdLargeFont->width,gdLargeFont->height);
1426
1428 libgd, the C-language version of gd, can be obtained at URL
1429 http://www.boutell.com/gd/. Directions for installing and using it can
1430 be found at that site. Please do not contact me for help with libgd.
1431
1433 The GD.pm interface is copyright 1995-2007, Lincoln D. Stein. It is
1434 distributed under GPL and the Artistic License 2.0.
1435
1436 The latest versions of GD.pm are available at
1437
1438 http://stein.cshl.org/WWW/software/GD
1439
1441 GD::Polyline, GD::SVG, GD::Simple, Image::Magick
1442
1444 Hey! The above document had some coding errors, which are explained
1445 below:
1446
1447 Around line 463:
1448 You forgot a '=back' before '=head1'
1449
1450 Around line 475:
1451 '=item' outside of any '=over'
1452
1453
1454
1455perl v5.12.0 2009-07-10 GD(3)