1. Computing

Perl Module GD::Simple

How to draw quick graphics and images with the GD::Simple Perl module.

By

The Perl GD Module is a comprehensive interface to the GD graphics and image library, and the Simple class makes drawing with it almost trivially easy. It even includes a throwback to LOGO, a programming language that was all about drawing. Basically, the simple class gives you control of a virtual pen and you issue it commands that change the foreground and background colors, and move it across your images 'page'.

First, like all non-standard Perl modules, you will need to install GD::Simple from CPAN, and then you can include it at the top of your Perl programs with the use statement:

 use GD::Simple; 
Then, to get started drawing, you must create your canvas by creating an image:
 $img = GD::Simple->new(640, 480); 
To change the colors of objects, you can use fgcolor to set the pen and border colors, and bgcolor to set the fill color for your shapes.
 $img->fgcolor('black');
 $img->bgcolor('yellow'); 
You can use methods like rectangle or ellipse to draw some shapes. With rectangle, you set the x,y coordinates for the top left corner and the bottom right corner. The ellipse function is called with the width and height, centered on the current location of the pen.
 $img->rectangle(10, 10, 50, 50);
 $img->ellipse(50, 50); 
You could even use it to create quick graphical headers:
 $img->moveTo(200,25);
 $img->font('Times:italic');
 $img->fontsize(18);
 $img->string('Drawn With Perl'); 
Learning to use GD::Simple is a great way to get into the more complex and powerful GD Module by taking it in smaller, easier steps.

Module Links:

  1. GD::Simple on CPAN
  2. GD::Simple Documentation
  3. Lincoln D. Stein
  1. About.com
  2. Computing
  3. Perl
  4. Programming Perl
  5. Packages & Modules
  6. Perl Module GD::Simple - How to draw quick graphics and images with the GD::Simple Perl module.

©2014 About.com. All rights reserved.