1GD::Simple(3) User Contributed Perl Documentation GD::Simple(3)
2
3
4
6 GD::Simple - Simplified interface to GD library
7
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 = new GD::Polygon;
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
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 ($delta_x,$delta_y)= $img->stringBounds($string)
269 This method indicates the X and Y offsets (which may be negative)
270 that will occur when the given string is drawn using the current
271 font, fontsize and angle. When the string is drawn horizontally, it
272 gives the width and height of the string's bounding box.
273
274 $delta_x = $img->stringWidth($string)
275 This method indicates the width of the string given the current
276 font, fontsize and angle. It is the same as
277 ($img->stringBounds($string))[0]
278
279 ($x,$y) = $img->curPos
280 Return the current position of the pen. Set the current position
281 using moveTo().
282
283 $font = $img->font([$newfont] [,$newsize])
284 Get or set the current font. Fonts can be GD::Font objects,
285 TrueType font file paths, or fontconfig font patterns like
286 "Times:italic" (see fontconfig). The latter feature requires that
287 you have the fontconfig library installed and are using libgd
288 version 2.0.33 or higher.
289
290 As a shortcut, you may pass two arguments to set the font and the
291 fontsize simultaneously. The fontsize is only valid when drawing
292 with TrueType fonts.
293
294 $size = $img->fontsize([$newfontsize])
295 Get or set the current font size. This is only valid for TrueType
296 fonts.
297
298 $size = $img->penSize([$newpensize])
299 Get or set the current pen width for use during line drawing
300 operations.
301
302 $angle = $img->angle([$newangle])
303 Set the current angle for use when calling line() or move() with a
304 single argument.
305
306 Here is an example of using turn() and angle() together to draw an
307 octagon. The first line drawn is the downward-slanting top right
308 edge. The last line drawn is the horizontal top of the octagon.
309
310 $img->moveTo(200,50);
311 $img->angle(0);
312 $img->turn(360/8);
313 for (1..8) { $img->line(50) }
314
315 $angle = $img->turn([$newangle])
316 Get or set the current angle to turn prior to drawing lines. This
317 value is only used when calling line() or move() with a single
318 argument. The turning angle will be applied to each call to line()
319 or move() just before the actual drawing occurs.
320
321 Angles are in degrees. Positive values turn the angle clockwise.
322
323 $color = $img->fgcolor([$newcolor])
324 Get or set the pen's foreground color. The current pen color can
325 be set by (1) using an (r,g,b) triple; (2) using a previously-
326 allocated color from the GD palette; or (3) by using a symbolic
327 color name such as "chartreuse." The list of color names can be
328 obtained using color_names(). The special color name 'transparent'
329 will create a completely transparent color.
330
331 $color = $img->bgcolor([$newcolor])
332 Get or set the pen's background color. The current pen color can
333 be set by (1) using an (r,g,b) triple; (2) using a previously-
334 allocated color from the GD palette; or (3) by using a symbolic
335 color name such as "chartreuse." The list of color names can be
336 obtained using color_names(). The special color name 'transparent'
337 will create a completely transparent color.
338
339 $index = $img->translate_color(@args)
340 Translates a color into a GD palette or TrueColor index. You may
341 pass either an (r,g,b) triple or a symbolic color name. If you pass
342 a previously-allocated index, the method will return it unchanged.
343
344 $index = $img->alphaColor(@args,$alpha)
345 Creates an alpha color. You may pass either an (r,g,b) triple or a
346 symbolic color name, followed by an integer indicating its opacity.
347 The opacity value ranges from 0 (fully opaque) to 127 (fully
348 transparent).
349
350 @names = GD::Simple->color_names
351 $translate_table = GD::Simple->color_names
352 Called in a list context, color_names() returns the list of
353 symbolic color names recognized by this module. Called in a scalar
354 context, the method returns a hash reference in which the keys are
355 the color names and the values are array references containing
356 [r,g,b] triples.
357
358 $gd = $img->gd
359 Return the internal GD::Image object. Usually you will not need to
360 call this since all GD methods are automatically referred to this
361 object.
362
363 ($red,$green,$blue) = GD::Simple->HSVtoRGB($hue,$saturation,$value)
364 Convert a Hue/Saturation/Value (HSV) color into an RGB triple. The
365 hue, saturation and value are integers from 0 to 255.
366
367 ($hue,$saturation,$value) = GD::Simple->RGBtoHSV($red,$green,$blue)
368 Convert a Red/Green/Blue (RGB) value into a Hue/Saturation/Value
369 (HSV) triple. The hue, saturation and value are integers from 0 to
370 255.
371
373 This script will create an image showing all the symbolic colors.
374
375 #!/usr/bin/perl
376
377 use strict;
378 use GD::Simple;
379
380 my @color_names = GD::Simple->color_names;
381 my $cols = int(sqrt(@color_names));
382 my $rows = int(@color_names/$cols)+1;
383
384 my $cell_width = 100;
385 my $cell_height = 50;
386 my $legend_height = 16;
387 my $width = $cols * $cell_width;
388 my $height = $rows * $cell_height;
389
390 my $img = GD::Simple->new($width,$height);
391 $img->font(gdSmallFont);
392
393 for (my $c=0; $c<$cols; $c++) {
394 for (my $r=0; $r<$rows; $r++) {
395 my $color = $color_names[$c*$rows + $r] or next;
396 my @topleft = ($c*$cell_width,$r*$cell_height);
397 my @botright = ($topleft[0]+$cell_width,$topleft[1]+$cell_height-$legend_height);
398 $img->bgcolor($color);
399 $img->fgcolor($color);
400 $img->rectangle(@topleft,@botright);
401 $img->moveTo($topleft[0]+2,$botright[1]+$legend_height-2);
402 $img->fgcolor('black');
403 $img->string($color);
404 }
405 }
406
407 print $img->png;
408
410 The GD::Simple module is copyright 2004, Lincoln D. Stein. It is
411 distributed under the same terms as Perl itself. See the "Artistic
412 License" in the Perl source code distribution for licensing terms.
413
414 The latest versions of GD.pm are available at
415 https://github.com/lstein/Perl-GD
416
418 GD, GD::Polyline, GD::SVG, Image::Magick
419
420
421
422perl v5.32.0 2020-09-24 GD::Simple(3)