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

COLORS

351       This script will create an image showing all the symbolic colors.
352
353        #!/usr/bin/perl
354
355        use strict;
356        use GD::Simple;
357
358        my @color_names = GD::Simple->color_names;
359        my $cols = int(sqrt(@color_names));
360        my $rows = int(@color_names/$cols)+1;
361
362        my $cell_width    = 100;
363        my $cell_height   = 50;
364        my $legend_height = 16;
365        my $width       = $cols * $cell_width;
366        my $height      = $rows * $cell_height;
367
368        my $img = GD::Simple->new($width,$height);
369        $img->font(gdSmallFont);
370
371        for (my $c=0; $c<$cols; $c++) {
372          for (my $r=0; $r<$rows; $r++) {
373            my $color = $color_names[$c*$rows + $r] or next;
374            my @topleft  = ($c*$cell_width,$r*$cell_height);
375            my @botright = ($topleft[0]+$cell_width,$topleft[1]+$cell_height-$legend_height);
376            $img->bgcolor($color);
377            $img->fgcolor($color);
378            $img->rectangle(@topleft,@botright);
379            $img->moveTo($topleft[0]+2,$botright[1]+$legend_height-2);
380            $img->fgcolor('black');
381            $img->string($color);
382          }
383        }
384
385        print $img->png;
386

AUTHOR

388       The GD::Simple module is copyright 2004, Lincoln D. Stein.  It is dis‐
389       tributed under the same terms as Perl itself.  See the "Artistic
390       License" in the Perl source code distribution for licensing terms.
391
392       The latest versions of GD.pm are available at
393
394         http://stein.cshl.org/WWW/software/GD
395

SEE ALSO

397       GD, GD::Polyline, GD::SVG, Image::Magick
398
399
400
401perl v5.8.8                       2006-08-23                     GD::Simple(3)
Impressum