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

DESCRIPTION

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

COLORS

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

AUTHOR

412       The GD::Simple module is copyright 2004, Lincoln D. Stein.  It is
413       distributed under the same terms as Perl itself.  See the "Artistic
414       License" in the Perl source code distribution for licensing terms.
415
416       The latest versions of GD.pm are available at
417       https://github.com/lstein/Perl-GD
418

SEE ALSO

420       GD, GD::Polyline, GD::SVG, Image::Magick
421
422
423
424perl v5.38.0                      2023-07-20                     GD::Simple(3)
Impressum