1GD::Simple(3)         User Contributed Perl Documentation        GD::Simple(3)
2
3
4

NAME

6       GD::Simple - Simplified interface to GD library
7

SYNOPSIS

9           use GD::Simple;
10
11           # create a new image
12           $img = GD::Simple->new(400,250);
13
14           # draw a red rectangle with blue borders
15           $img->bgcolor('red');
16           $img->fgcolor('blue');
17           $img->rectangle(10,10,50,50);
18
19           # draw an empty rectangle with green borders
20           $img->bgcolor(undef);
21           $img->fgcolor('green');
22           $img->rectangle(30,30,100,100);
23
24           # move to (80,80) and draw a green line to (100,190)
25           $img->moveTo(80,80);
26           $img->lineTo(100,190);
27
28           # draw a solid orange ellipse
29           $img->moveTo(110,100);
30           $img->bgcolor('orange');
31           $img->fgcolor('orange');
32           $img->ellipse(40,40);
33
34           # draw a black filled arc
35           $img->moveTo(150,150);
36           $img->fgcolor('black');
37           $img->arc(50,50,0,100,gdNoFill|gdEdged);
38
39           # draw a string at (10,180) using the default
40           # built-in font
41           $img->moveTo(10,180);
42           $img->string('This is very simple');
43
44           # draw a string at (280,210) using 20 point
45           # times italic, angled upward 90 degrees
46           $img->moveTo(280,210);
47           $img->font('Times:italic');
48           $img->fontsize(20);
49           $img->angle(-90);
50           $img->string('This is very fancy');
51
52           # some turtle graphics
53           $img->moveTo(300,100);
54           $img->penSize(3,3);
55           $img->angle(0);
56           $img->line(20);   # 20 pixels going to the right
57           $img->turn(30);   # set turning angle to 30 degrees
58           $img->line(20);   # 20 pixel line
59           $img->line(20);
60           $img->line(20);
61           $img->turn(-90); # set turning angle to -90 degrees
62           $img->line(50);  # 50 pixel line
63
64           # draw a cyan polygon edged in blue
65           my $poly = new GD::Polygon;
66           $poly->addPt(150,100);
67           $poly->addPt(199,199);
68           $poly->addPt(100,199);
69           $img->bgcolor('cyan');
70           $img->fgcolor('blue');
71           $img->penSize(1,1);
72           $img->polygon($poly);
73
74          # convert into png data
75          print $img->png;
76

DESCRIPTION

78       GD::Simple is a subclass of the GD library that shortens many of the
79       long GD method calls by storing information about the pen color, size
80       and position in the GD object itself.  It also adds a small number of
81       "turtle graphics" style calls for those who prefer to work in polar
82       coordinates.  In addition, the library allows you to use symbolic names
83       for colors, such as "chartreuse", and will manage the colors for you.
84
85   The Pen
86       GD::Simple maintains a "pen" whose settings are used for line- and
87       shape-drawing operations.  The pen has the following properties:
88
89       fgcolor
90           The pen foreground color is the color of lines and the borders of
91           filled and unfilled shapes.
92
93       bgcolor
94           The pen background color is the color of the contents of filled
95           shapes.
96
97       pensize
98           The pen size is the width of the pen.  Larger sizes draw thicker
99           lines.
100
101       position
102           The pen position is its current position on the canvas in (X,Y)
103           coordinates.
104
105       angle
106           When drawing in turtle mode, the pen angle determines the current
107           direction of lines of relative length.
108
109       turn
110           When drawing in turtle mode, the turn determines the clockwise or
111           counterclockwise angle that the pen will turn before drawing the
112           next line.
113
114       font
115           The font to use when drawing text.  Both built-in bitmapped fonts
116           and TrueType fonts are supported.
117
118       fontsize
119           The size of the font to use when drawing with TrueType fonts.
120
121       One sets the position and properties of the pen and then draws.  As the
122       drawing progresses, the position of the pen is updated.
123
124   Methods
125       GD::Simple introduces a number of new methods, a few of which have the
126       same name as GD::Image methods, and hence change their behavior. In
127       addition to these new methods, GD::Simple objects support all of the
128       GD::Image methods. If you make a method call that isn't directly
129       supported by GD::Simple, it refers the request to the underlying
130       GD::Image object.  Hence one can load a JPEG image into GD::Simple and
131       declare it to be TrueColor by using this call, which is effectively
132       inherited from GD::Image:
133
134         my $img = GD::Simple->newFromJpeg('./myimage.jpg',1);
135
136       The rest of this section describes GD::Simple-specific methods.
137
138       $img = GD::Simple->new($x,$y [,$truecolor])
139       $img = GD::Simple->new($gd)
140           Create a new GD::Simple object. There are two forms of new(). In
141           the first form, pass the width and height of the desired canvas,
142           and optionally a boolean flag to request a truecolor image. In the
143           second form, pass a previously-created GD::Image object.
144
145       GD::Simple->class('GD');
146       GD::Simple->class('GD::SVG');
147           Select whether new() should use GD or GD::SVG internally. Call
148           GD::Simple->class('GD::SVG') before calling new() if you wish to
149           generate SVG images.
150
151           If future GD subclasses are created, this method will subport them.
152
153       $img->moveTo($x,$y)
154           This call changes the position of the pen without drawing. It moves
155           the pen to position ($x,$y) on the drawing canvas.
156
157       $img->move($dx,$dy)
158       $img->move($dr)
159           This call changes the position of the pen without drawing. When
160           called with two arguments it moves the pen $dx pixels to the right
161           and $dy pixels downward.  When called with one argument it moves
162           the pen $dr pixels along the vector described by the current pen
163           angle.
164
165       $img->lineTo($x,$y)
166           The lineTo() call simultaneously draws and moves the pen.  It draws
167           a line from the current pen position to the position defined by
168           ($x,$y) using the current pen size and color.  After drawing, the
169           position of the pen is updated to the new position.
170
171       $img->line($x1,$y1,$x2,$y2 [,$color])
172       $img->line($dx,$dy)
173       $img->line($dr)
174           The line() call simultaneously draws and moves the pen. When called
175           with two arguments it draws a line from the current position of the
176           pen to the position $dx pixels to the right and $dy pixels down.
177           When called with one argument, it draws a line $dr pixels long
178           along the angle defined by the current pen angle.
179
180           When called with four or five arguments, line() behaves like
181           GD::Image->line().
182
183       $img->clear
184           This method clears the canvas by painting over it with the current
185           background color.
186
187       $img->rectangle($x1,$y1,$x2,$y2)
188           This method draws the rectangle defined by corners ($x1,$y1),
189           ($x2,$y2). The rectangle's edges are drawn in the foreground color
190           and its contents are filled with the background color. To draw a
191           solid rectangle set bgcolor equal to fgcolor. To draw an unfilled
192           rectangle (transparent inside), set bgcolor to undef.
193
194       $img->ellipse($width,$height)
195           This method draws the ellipse centered at the current location with
196           width $width and height $height.  The ellipse's border is drawn in
197           the foreground color and its contents are filled with the
198           background color. To draw a solid ellipse set bgcolor equal to
199           fgcolor. To draw an unfilled ellipse (transparent inside), set
200           bgcolor to undef.
201
202       $img->arc($cx,$cy,$width,$height,$start,$end [,$style])
203           This method draws filled and unfilled arcs.  See GD for a
204           description of the arguments. To draw a solid arc (such as a pie
205           wedge) set bgcolor equal to fgcolor. To draw an unfilled arc, set
206           bgcolor to undef.
207
208       $img->polygon($poly)
209           This method draws filled and unfilled polygon using the current
210           settings of fgcolor for the polygon border and bgcolor for the
211           polygon fill color.  See GD for a description of creating polygons.
212           To draw a solid polygon set bgcolor equal to fgcolor. To draw an
213           unfilled polygon, set bgcolor to undef.
214
215       $img->polyline($poly)
216           This method draws polygons without closing the first and last
217           vertices (similar to GD::Image->unclosedPolygon()). It uses the
218           fgcolor to draw the line.
219
220       $img->string($string)
221           This method draws the indicated string starting at the current
222           position of the pen. The pen is moved to the end of the drawn
223           string.  Depending on the font selected with the font() method,
224           this will use either a bitmapped GD font or a TrueType font.  The
225           angle of the pen will be consulted when drawing the text. For
226           TrueType fonts, any angle is accepted.  For GD bitmapped fonts, the
227           angle can be either 0 (draw horizontal) or -90 (draw upwards).
228
229           For consistency between the TrueType and GD font behavior, the
230           string is always drawn so that the current position of the pen
231           corresponds to the bottom left of the first character of the text.
232           This is different from the GD behavior, in which the first
233           character of bitmapped fonts hangs down from the pen point.
234
235           This method returns a polygon indicating the bounding box of the
236           rendered text.  If an error occurred (such as invalid font
237           specification) it returns undef and an error message in $@.
238
239       $metrics = $img->fontMetrics
240       ($metrics,$width,$height) =
241       GD::Simple->fontMetrics($font,$fontsize,$string)
242           This method returns information about the current font, most
243           commonly a TrueType font. It can be invoked as an instance method
244           (on a previously-created GD::Simple object) or as a class method
245           (on the 'GD::Simple' class).
246
247           When called as an instance method, fontMetrics() takes no arguments
248           and returns a single hash reference containing the metrics that
249           describe the currently selected font and size. The hash reference
250           contains the following information:
251
252             xheight      the base height of the font from the bottom to the top of
253                          a lowercase 'm'
254
255             ascent       the length of the upper stem of the lowercase 'd'
256
257             descent      the length of the lower step of the lowercase 'j'
258
259             lineheight   the distance from the bottom of the 'j' to the top of
260                          the 'd'
261
262             leading      the distance between two adjacent lines
263
264       ($delta_x,$delta_y)= $img->stringBounds($string)
265           This method indicates the X and Y offsets (which may be negative)
266           that will occur when the given string is drawn using the current
267           font, fontsize and angle. When the string is drawn horizontally, it
268           gives the width and height of the string's bounding box.
269
270       $delta_x = $img->stringWidth($string)
271           This method indicates the width of the string given the current
272           font, fontsize and angle. It is the same as
273           ($img->stringBounds($string))[0]
274
275       ($x,$y) = $img->curPos
276           Return the current position of the pen.  Set the current position
277           using moveTo().
278
279       $font = $img->font([$newfont] [,$newsize])
280           Get or set the current font.  Fonts can be GD::Font objects,
281           TrueType font file paths, or fontconfig font patterns like
282           "Times:italic" (see fontconfig). The latter feature requires that
283           you have the fontconfig library installed and are using libgd
284           version 2.0.33 or higher.
285
286           As a shortcut, you may pass two arguments to set the font and the
287           fontsize simultaneously. The fontsize is only valid when drawing
288           with TrueType fonts.
289
290       $size = $img->fontsize([$newfontsize])
291           Get or set the current font size.  This is only valid for TrueType
292           fonts.
293
294       $size = $img->penSize([$newpensize])
295           Get or set the current pen width for use during line drawing
296           operations.
297
298       $angle = $img->angle([$newangle])
299           Set the current angle for use when calling line() or move() with a
300           single argument.
301
302           Here is an example of using turn() and angle() together to draw an
303           octagon.  The first line drawn is the downward-slanting top right
304           edge.  The last line drawn is the horizontal top of the octagon.
305
306             $img->moveTo(200,50);
307             $img->angle(0);
308             $img->turn(360/8);
309             for (1..8) { $img->line(50) }
310
311       $angle = $img->turn([$newangle])
312           Get or set the current angle to turn prior to drawing lines.  This
313           value is only used when calling line() or move() with a single
314           argument.  The turning angle will be applied to each call to line()
315           or move() just before the actual drawing occurs.
316
317           Angles are in degrees.  Positive values turn the angle clockwise.
318
319       $color = $img->fgcolor([$newcolor])
320           Get or set the pen's foreground color.  The current pen color can
321           be set by (1) using an (r,g,b) triple; (2) using a previously-
322           allocated color from the GD palette; or (3) by using a symbolic
323           color name such as "chartreuse."  The list of color names can be
324           obtained using color_names(). The special color name 'transparent'
325           will create a completely transparent color.
326
327       $color = $img->bgcolor([$newcolor])
328           Get or set the pen's background color.  The current pen color can
329           be set by (1) using an (r,g,b) triple; (2) using a previously-
330           allocated color from the GD palette; or (3) by using a symbolic
331           color name such as "chartreuse."  The list of color names can be
332           obtained using color_names(). The special color name 'transparent'
333           will create a completely transparent color.
334
335       $index = $img->translate_color(@args)
336           Translates a color into a GD palette or TrueColor index.  You may
337           pass either an (r,g,b) triple or a symbolic color name. If you pass
338           a previously-allocated index, the method will return it unchanged.
339
340       $index = $img->alphaColor(@args,$alpha)
341           Creates an alpha color.  You may pass either an (r,g,b) triple or a
342           symbolic color name, followed by an integer indicating its opacity.
343           The opacity value ranges from 0 (fully opaque) to 127 (fully
344           transparent).
345
346       @names = GD::Simple->color_names
347       $translate_table = GD::Simple->color_names
348           Called in a list context, color_names() returns the list of
349           symbolic color names recognized by this module.  Called in a scalar
350           context, the method returns a hash reference in which the keys are
351           the color names and the values are array references containing
352           [r,g,b] triples.
353
354       $gd = $img->gd
355           Return the internal GD::Image object.  Usually you will not need to
356           call this since all GD methods are automatically referred to this
357           object.
358
359       ($red,$green,$blue) = GD::Simple->HSVtoRGB($hue,$saturation,$value)
360           Convert a Hue/Saturation/Value (HSV) color into an RGB triple. The
361           hue, saturation and value are integers from 0 to 255.
362
363       ($hue,$saturation,$value) =
364       GD::Simple->RGBtoHSV($hue,$saturation,$value)
365           Convert a Red/Green/Blue (RGB) value into a Hue/Saturation/Value
366           (HSV) triple. The hue, saturation and value are integers from 0 to
367           255.
368

COLORS

370       This script will create an image showing all the symbolic colors.
371
372        #!/usr/bin/perl
373
374        use strict;
375        use GD::Simple;
376
377        my @color_names = GD::Simple->color_names;
378        my $cols = int(sqrt(@color_names));
379        my $rows = int(@color_names/$cols)+1;
380
381        my $cell_width    = 100;
382        my $cell_height   = 50;
383        my $legend_height = 16;
384        my $width       = $cols * $cell_width;
385        my $height      = $rows * $cell_height;
386
387        my $img = GD::Simple->new($width,$height);
388        $img->font(gdSmallFont);
389
390        for (my $c=0; $c<$cols; $c++) {
391          for (my $r=0; $r<$rows; $r++) {
392            my $color = $color_names[$c*$rows + $r] or next;
393            my @topleft  = ($c*$cell_width,$r*$cell_height);
394            my @botright = ($topleft[0]+$cell_width,$topleft[1]+$cell_height-$legend_height);
395            $img->bgcolor($color);
396            $img->fgcolor($color);
397            $img->rectangle(@topleft,@botright);
398            $img->moveTo($topleft[0]+2,$botright[1]+$legend_height-2);
399            $img->fgcolor('black');
400            $img->string($color);
401          }
402        }
403
404        print $img->png;
405

AUTHOR

407       The GD::Simple module is copyright 2004, Lincoln D. Stein.  It is
408       distributed under the same terms as Perl itself.  See the "Artistic
409       License" in the Perl source code distribution for licensing terms.
410
411       The latest versions of GD.pm are available at
412
413         http://stein.cshl.org/WWW/software/GD
414

SEE ALSO

416       GD, GD::Polyline, GD::SVG, Image::Magick
417
418
419
420perl v5.16.3                      2013-02-26                     GD::Simple(3)
Impressum