The GD graphics library is documented, and is an example of the abundant perl modules that exist.
So lets get started. Here is an example of a perl program that creates a simple picture.
#!/usr/local/bin/perl
use GD;
$im = new GD::Image(100,100); # create a new image
# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$im->fill(50,50,$white);
$im->arc(50,50,75,95,0,360,$black); # oval
$im->line(35,20,35,50,,$black); # line
$im->line(65,20,65,50,,$black); # line
$im->arc(50,60,50,50,0,180,$black); # arc
# make sure we are writing to a binary stream
binmode STDOUT;
# Convert the image to PNG and print it on standard output
print $im->png;
Wondering about these functions "line" and "arc" and "fill"? You can get documentation on the GD drawing functions here.
% gd1.pl > pic1.pngAnd then view it with various unix programs:
% xv pic1.png OR... % gimp pic1.png OR... % display pic1.png OR put this in the location of your browser... file:/home/yourusername/pic1.pngOr run it directly on the command line:
% gd1.pl | display -NOTE: gimp, display, and xv are supplied by unix.
$im = new GD::Image(100,100); # create a new image . . . # allocate some colors $white = $im->colorAllocate(255,255,255);If this programming style is unfamiliar to you the best way to think of what you're seeing is that an "object" is being created by the new statement. The code describing this object has many methods (or functions) associated with it, and to call those methods, you can use the construction: $im->colorAllocate(), and this method will return values in a way that is similar to functions that you've seen before.
There are a lot of books on object oriented programming, and if you're interested see this book as a start.
#!/usr/bin/perl
use strict;
use CGI qw/:standard/;
use GD;
print header;
my $file = param('file');
my $name = 'mcipriano';
my $htdocs_directory = "/home/$name/public_html/img/";
$file =~ s/.png//;
$file = $file . ".png";
my $img_link = "/~$name/img/$file";
print "<HTML>\n";
print "<body>";
print "<h2>Displaying a png file $file to screen</H2>\n";
my ($x, $y);
$x = 100;
$y = 100;
my($image) = new GD::Image($x,$y);
my $black = $image->colorAllocate(0,0,0);
my $red = $image->colorAllocate(255,0,0);
my $white = $image->colorAllocate(255,255,255);
$image->fill(50,50,$white); $image->arc(50,50,75,95,0,360,$black);
$image->line(35,20,35,50,,$black);
$image->line(65,20,65,50,,$black);
$image->arc(50,60,50,50,0,180,$black);
print "writing to DOCS: $htdocs_file<BR>\n";
open(F_OUT, ">$htdocs_file") || print "Cant write picture: $htdocs_file";
binmode F_OUT;
print F_OUT $image->png;
close(F_OUT);
chmod 0777, $file;
print "displaying IMAGE file: $file<BR>\n";
print "<img src=\"$img_link\" border = 1>\n";
print "</body>";
print "</html>\n";
exit(1);
You may launch this program in the following way:
http://genomics.mbl.edu/~username/test_prog4.pl?file=picture.png
|
|
|---|