GD::Simple(3)
π NAME π
GD::Simple(3) β Simplified interface to GD library. Yes bestie, this is the one from User Contributed Perl Documentation, and it's here to make GD feel way less like a whole boss fight. π«Ά
β¨ SYNOPSIS β¨
For a nice tutorial on using this module, see Gabor Szabo's article at http://perlmaven.com/drawing-images-using-gd-simple. It's giving helpful, honestly. π
use GD::Simple;
# create a new image
$img = GD::Simple->new(400,250);
# draw a red rectangle with blue borders
$img->bgcolor('red');
$img->fgcolor('blue');
$img->rectangle(10,10,50,50);
# draw an empty rectangle with green borders
$img->bgcolor(undef);
$img->fgcolor('green');
$img->rectangle(30,30,100,100);
# move to (80,80) and draw a green line to (100,190)
$img->moveTo(80,80);
$img->lineTo(100,190);
# draw a solid orange ellipse
$img->moveTo(110,100);
$img->bgcolor('orange');
$img->fgcolor('orange');
$img->ellipse(40,40);
# draw a black filled arc
$img->moveTo(150,150);
$img->fgcolor('black');
$img->arc(50,50,0,100,gdNoFill|gdEdged);
# draw a string at (10,180) using the default
# built-in font
$img->moveTo(10,180);
$img->string('This is very simple');
# draw a string at (280,210) using 20 point
# times italic, angled upward 90 degrees
$img->moveTo(280,210);
$img->font('Times:italic');
$img->fontsize(20);
$img->angle(-90);
$img->string('This is very fancy');
# some turtle graphics
$img->moveTo(300,100);
$img->penSize(3,3);
$img->angle(0);
$img->line(20); # 20 pixels going to the right
$img->turn(30); # set turning angle to 30 degrees
$img->line(20); # 20 pixel line
$img->line(20);
$img->line(20);
$img->turn(-90); # set turning angle to -90 degrees
$img->line(50); # 50 pixel line
# draw a cyan polygon edged in blue
my $poly = GD::Polygon->new;
$poly->addPt(150,100);
$poly->addPt(199,199);
$poly->addPt(100,199);
$img->bgcolor('cyan');
$img->fgcolor('blue');
$img->penSize(1,1);
$img->polygon($poly);
# convert into png data
print $img->png;
π DESCRIPTION π
GD::Simple is a subclass of the GD library that shortens many of the long GD method calls by storing information about the pen color, size and position in the GD object itself. It also adds a small number of βturtle graphicsβ style calls for those who prefer to work in polar coordinates. In addition, the library allows you to use symbolic names for colors, such as "chartreuse", and will manage the colors for you. Obsessed, honestly. π
ποΈ The Pen ποΈ
GD::Simple maintains a "pen" whose settings are used for line- and shape-drawing operations. The pen has the following properties:
fgcolorβ The pen foreground color is the color of lines and the borders of filled and unfilled shapes.bgcolorβ The pen background color is the color of the contents of filled shapes.pensizeβ The pen size is the width of the pen. Larger sizes draw thicker lines.positionβ The pen position is its current position on the canvas in(X,Y)coordinates.angleβ When drawing in turtle mode, the pen angle determines the current direction of lines of relative length.turnβ When drawing in turtle mode, the turn determines the clockwise or counterclockwise angle that the pen will turn before drawing the next line.fontβ The font to use when drawing text. Both built-in bitmapped fonts and TrueType fonts are supported.fontsizeβ The size of the font to use when drawing with TrueType fonts.
One sets the position and properties of the pen and then draws. As the drawing progresses, the position of the pen is updated. Main character pen behavior, fr fr. β¨
π οΈ Methods π οΈ
GD::Simple introduces a number of new methods, a few of which have the same name as GD::Image methods, and hence change their behavior. In addition to these new methods, GD::Simple objects support all of the GD::Image methods. If you make a method call that isn't directly supported by GD::Simple, it refers the request to the underlying GD::Image object. Hence one can load a JPEG image into GD::Simple and declare it to be TrueColor by using this call, which is effectively inherited from GD::Image:
my $img = GD::Simple->newFromJpeg('./myimage.jpg',1);
The rest of this section describes GD::Simple-specific methods. π«Ά
$img = GD::Simple->new($x,$y [,$truecolor])/$img = GD::Simple->new($gd)β Create a newGD::Simpleobject. There are two forms ofnew(). In the first form, pass the width and height of the desired canvas, and optionally a boolean flag to request a truecolor image. In the second form, pass a previously-createdGD::Imageobject. No cap, two whole vibes. πGD::Simple->class('GD');/GD::Simple->class('GD::SVG');β Select whethernew()should useGDorGD::SVGinternally. CallGD::Simple->class('GD::SVG')before callingnew()if you wish to generate SVG images.
If future GD subclasses are created, this method will subport them. (Yes, subport. We keep the details exactly as they are, bestie. π)
$img->moveTo($x,$y)β This call changes the position of the pen without drawing. It moves the pen to position($x,$y)on the drawing canvas.$img->move($dx,$dy)/$img->move($dr)β This call changes the position of the pen without drawing. When called with two arguments it moves the pen$dxpixels to the right and$dypixels downward. When called with one argument it moves the pen$drpixels along the vector described by the current pen angle.$img->lineTo($x,$y)β ThelineTo()call simultaneously draws and moves the pen. It draws a line from the current pen position to the position defined by($x,$y)using the current pen size and color. After drawing, the position of the pen is updated to the new position.$img->line($x1,$y1,$x2,$y2 [,$color])/$img->line($dx,$dy)/$img->line($dr)β Theline()call simultaneously draws and moves the pen. When called with two arguments it draws a line from the current position of the pen to the position$dxpixels to the right and$dypixels down. When called with one argument, it draws a line$drpixels long along the angle defined by the current pen angle.
When called with four or five arguments, line() behaves like GD::Image->line(). Slay, inherited behavior.
$img->clearβ This method clears the canvas by painting over it with the current background color.$img->rectangle($x1,$y1,$x2,$y2)β This method draws the rectangle defined by corners($x1,$y1),($x2,$y2). The rectangle's edges are drawn in the foreground color and its contents are filled with the background color. To draw a solid rectangle setbgcolorequal tofgcolor. To draw an unfilled rectangle (transparent inside), setbgcolortoundef.$img->ellipse($width,$height)β This method draws the ellipse centered at the current location with width$widthand height$height. The ellipse's border is drawn in the foreground color and its contents are filled with the background color. To draw a solid ellipse setbgcolorequal tofgcolor. To draw an unfilled ellipse (transparent inside), setbgcolortoundef.$img->arc([$cx,$cy,] $width,$height,$start,$end [,$style])β This method draws filled and unfilled arcs, at the current position, with the current fore- and background colors. See GD for a description of the arguments. To draw a solid arc (such as a pie wedge) setbgcolorequal tofgcolor. To draw an unfilled arc, setbgcolortoundef.$img->polygon($poly)β This method draws filled and unfilled polygon using the current settings offgcolorfor the polygon border andbgcolorfor the polygon fill color. See GD for a description of creating polygons. To draw a solid polygon setbgcolorequal tofgcolor. To draw an unfilled polygon, setbgcolortoundef.$img->polyline($poly)β This method draws polygons without closing the first and last vertices (similar toGD::Image->unclosedPolygon()). It uses thefgcolorto draw the line.$img->string($string)β This method draws the indicated string starting at the current position of the pen. The pen is moved to the end of the drawn string. Depending on the font selected with thefont()method, this will use either a bitmapped GD font or a TrueType font. The angle of the pen will be consulted when drawing the text. For TrueType fonts, any angle is accepted. For GD bitmapped fonts, the angle can be either0(draw horizontal) or-90(draw upwards).
For consistency between the TrueType and GD font behavior, the string is always drawn so that the current position of the pen corresponds to the bottom left of the first character of the text. This is different from the GD behavior, in which the first character of bitmapped fonts hangs down from the pen point.
This method returns a polygon indicating the bounding box of the rendered text. If an error occurred (such as invalid font specification) it returns undef and an error message in $@. Error handling said βnot me,β but it's there. π
$metrics = $img->fontMetrics/($metrics,$width,$height) = GD::Simple->fontMetrics($font,$fontsize,$string)β This method returns information about the current font, most commonly a TrueType font. It can be invoked as an instance method (on a previously-createdGD::Simpleobject) or as a class method (on the'GD::Simple'class).
When called as an instance method, fontMetrics() takes no arguments and returns a single hash reference containing the metrics that describe the currently selected font and size. The hash reference contains the following information:
xheightβ the base height of the font from the bottom to the top of a lowercase'm'ascentβ the length of the upper stem of the lowercase'd'descentβ the length of the lower step of the lowercase'j'lineheightβ the distance from the bottom of the'j'to the top of the'd'leadingβ the distance between two adjacent lines
This description and code was changed with 2.75. Version-lore, iykyk. π
($delta_x,$delta_y)= $img->stringBounds($string)β This method indicates the X and Y offsets (which may be negative) that will occur when the given string is drawn using the current font, fontsize and angle. When the string is drawn horizontally, it gives the width and height of the string's bounding box.$delta_x = $img->stringWidth($string)β tells you how wide this string is gonna be, based on the font, fontsize and angle you've got going right now. It's literally the same thing as($img->stringBounds($string))[0], just less typing. π($x,$y) = $img->curPosβ returns where the pen is sitting rn. Wanna move it? That's amoveTo()job, bestie.$font = $img->font([$newfont] [,$newsize])β gets or sets the current font. Your options are:GD::Fontobjects, TrueType font file paths, or fontconfig font patterns like"Times:italic"(peep fontconfig for that one). Heads up β that last vibe requires you to have the fontconfig library installed and be running libgd version 2.0.33 or higher. β¨
Lowkey a shortcut: you can pass two arguments and set the font and the fontsize at the same time. The fontsize is only valid when you're drawing with TrueType fonts tho.
$size = $img->fontsize([$newfontsize])β gets or sets the current font size. Only valid for TrueType fonts, iykyk.$size = $img->penSize([$newpensize])β gets or sets the current pen width, which is what's used during line drawing operations. Thick pen, thin pen, your call. π$angle = $img->angle([$newangle])β sets the current angle for when you callline()ormove()with a single argument.
Here's an iconic lil example of turn() and angle() teaming up to draw an octagon. The first line drawn is the downward-slanting top right edge. The last line drawn is the horizontal top of the octagon. Obsessed honestly.
$img->moveTo(200,50);
$img->angle(0);
$img->turn(360/8);
for (1..8) { $img->line(50) }
$angle = $img->turn([$newangle])β gets or sets the angle to turn before drawing lines. This value is only used when you callline()ormove()with a single argument. The turning angle gets applied to each call ofline()ormove()right before the actual drawing happens.
Angles are in degrees, and positive values turn the angle clockwise. π₯
$color = $img->fgcolor([$newcolor])β gets or sets the pen's foreground color. You can set it by (1) using an(r,g,b)triple; (2) using a previously-allocated color from the GD palette; or (3) using a symbolic color name such as"chartreuse."The whole list of color names is available viacolor_names(). And the special color name'transparent'will create a completely transparent color. π$color = $img->bgcolor([$newcolor])β gets or sets the pen's background color. Same three ways to set it: (1) an(r,g,b)triple; (2) a previously-allocated color from the GD palette; or (3) a symbolic color name such as"chartreuse."The list of color names comes fromcolor_names(). And yeah, the special color name'transparent'will create a completely transparent color. π«Ά$index = $img->translate_color(@args)β translates a color into a GD palette or TrueColor index. Pass it either an(r,g,b)triple or a symbolic color name. And if you pass a previously-allocated index? It just hands it right back to you unchanged. No drama. π$index = $img->alphaColor(@args,$alpha)β creates an alpha color. You can pass either an(r,g,b)triple or a symbolic color name, followed by an integer indicating its opacity. The opacity value ranges from0(fully opaque) to127(fully transparent). Yes bestie, that's the way it's numbered β don't come for me, that's just how it is.@names = GD::Simple->color_names$translate_table = GD::Simple->color_namesβ call it in list context andcolor_names()returns the list of symbolic color names this module recognizes. Call it in scalar context and it returns a hash reference where the keys are the color names and the values are array references containing[r,g,b]triples. Both slay, just differently. β¨$gd = $img->gdβ returns the internalGD::Imageobject. Usually you will not need to call this, because all GD methods automatically get referred to this object anyway. It's giving "I'm here if you need me but you probably don't." π($red,$green,$blue) = GD::Simple->HSVtoRGB($hue,$saturation,$value)β converts a Hue/Saturation/Value (HSV) color into an RGB triple. The hue, saturation and value are integers from0to255.($hue,$saturation,$value) = GD::Simple->RGBtoHSV($red,$green,$blue)β converts a Red/Green/Blue (RGB) value into a Hue/Saturation/Value (HSV) triple. Again, the hue, saturation and value are integers from0to255. Goes both ways, we love a versatile icon. π
πΈ COLORS πΈ
This script will create an image showing all the symbolic colors. Run it, screenshot it, make it your wallpaper, idc. π
#!/usr/bin/perl
use strict;
use GD::Simple;
my @color_names = GD::Simple->color_names;
my $cols = int(sqrt(@color_names));
my $rows = int(@color_names/$cols)+1;
my $cell_width = 100;
my $cell_height = 50;
my $legend_height = 16;
my $width = $cols * $cell_width;
my $height = $rows * $cell_height;
my $img = GD::Simple->new($width,$height);
$img->font(gdSmallFont);
for (my $c=0; $c<$cols; $c++) {
for (my $r=0; $r<$rows; $r++) {
my $color = $color_names[$c*$rows + $r] or next;
my @topleft = ($c*$cell_width,$r*$cell_height);
my @botright = ($topleft[0]+$cell_width,$topleft[1]+$cell_height-$legend_height);
$img->bgcolor($color);
$img->fgcolor($color);
$img->rectangle(@topleft,@botright);
$img->moveTo($topleft[0]+2,$botright[1]+$legend_height-2);
$img->fgcolor('black');
$img->string($color);
}
}
print $img->png;
π AUTHOR π
The GD::Simple module is copyright 2004, Lincoln D. Stein. It's distributed under the same terms as Perl itself β see the "Artistic License" in the Perl source code distribution for the licensing tea. π
The latest versions of GD.pm are available at https://github.com/lstein/Perl-GD β go get 'em.
π¦ SEE ALSO π¦
GD, GD::Polyline, GD::SVG, Image::Magick
And that's the whole page, bestie β no cap. This was perl v5.42.0, dated 2024-06-23, and it's the GD::Simple(3) manpage, signing off. Go draw something iconic. πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-GD 2.830.0-160000.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-GD 2.830.0-160000.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-GD 2.830.0-160000.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-GD 2.830.0-160000.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms