Iv just started my very first attempt at using GD to produce graphics and
I'm confused. I have RTFM in fact iv read a few but I'm still confused.
I'm using ActivePerl on an NT system, so I loaded in the GD module using
PPM, set the binmode to STDOUT as specified in the manual and wrote a hunk
of code that I think should produce a big rectangle (it's only a test!). I
tried outputting in both PNG and JPEG format but it just appears as
'hieroglyphics' on the browser page, obviously this is being interpreted as
text instead of graphical information.
I'm sure I'm being stupid and missed something fundamental. Help please!
The rest of the script deals with searching a database and extracting third
year values, these will hopefully be used within the graph to show the
respective cumulative results.
The Code so far..
print <<HTML_SCRIPT1;
Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE>Third Year Pipeline Search:</TITLE>
</HEAD>
<BODY BGCOLOR="gray" BACKGROUND="/images/bck49.jpg">
<H1><u>Third Year Pipeline Resources Graph:</u></H1>
<FONT SIZE=2>
HTML_SCRIPT1
use CGI;
use DBI;
use GD;
my $q = new CGI;
my $lobtype = $q->param( "Lob" );
my @rawsql = "*";
use DBI;
my $dbh = DBI->connect('dbi:ODBC:projects', 'test','');
my $sql = "SELECT \"Project-Years\".\"Project Title\",
RevenuenewIncremental,
RevenuenewCanabalised,
RevenuenewTotal,
RevenueProtected,
CostSaving,
CostAvoidance
FROM Products,
Projects,
\"Project-Years\"
WHERE Products.History = 'Current'
AND Projects.History = 'Current'
AND \"Project-Years\".History = 'Current'
AND Products.\"ID-REF\" = Projects.\"ID-REF\"
AND Projects.ProjectTitle =
\"Project-Years\".\"Project Title\"
AND LOB like '%'
ORDER BY Products.\"ID-REF\",
Projects.ProjectTitle,
\"Project-Years\".Year
";
my $sth = $dbh->prepare($sql);
$sth->execute();
my $incremental =0; my $canabalised =0; my $total =0;
my $protected =0; my $saving =0; my $avoidance =0;
my @lastproject; my $yearloop =1;
@lastproject = $sth->fetchrow_array();
while (@rawsql = $sth->fetchrow_array()){
if ($yearloop eq 3){
$incremental = $incremental + $lastproject[1];
$canabalised = $canabalised + $lastproject[2];
$total = $total + $lastproject[3];
$protected = $protected + $lastproject[4];
$saving = $saving + $lastproject[5];
$avoidance = $avoidance + $lastproject[6];
$yearloop ++;
if ($lastproject[0] ne $rawsql[0]){$yearloop =1;}
}
elsif ($lastproject[0] ne $rawsql[0]){ $yearloop =1;}
else {$yearloop ++;}
@lastproject = @rawsql;
}
binmode STDOUT;
testgraph();
print <<HTML_SCRIPT2;
<P> Incremental=$incremental canabalised=$canabalised Total=$total
Protected=$protected Savings=$saving Avoidance=$avoidance
<HR>
<p align="center"><a
href="http://uk07_comp92/ProjectsDB/html/default.html">BACK</a></p>
</BODY>
</HTML>
HTML_SCRIPT2
$sth->finish();
$dbh->disconnect;
sub testgraph{
my $image = new GD::Image( 100, 100);
my $background = $image->colorAllocate( 255, 255, 255);
my $area_color = $image->colorAllocate( 255, 0, 0);
my $axis_color = $image->colorAllocate( 0, 255, 0);
my $text_color = $image->colorAllocate( 0, 0, 255);
$image->string(gdLargeFont, 10, 15, "Third Year Output", $text_color );
my $polygon = new GD::Polygon;
$polygon->addPt( 30, 50);
for (my $i =0; $i < 100; $i++){
$polygon->addPt(30 + 100 /(100 -1 ) * $i, 50 - $i * 100 / 10 );}
$polygon->addPt(30 + 100, 50);
$image->filledPolygon($polygon, $area_color);
$image->line(30, 50, 30 +100, 50, $axis_color);
$image->line(30, 50, 30, 50 -100, $axis_color);
$image->transparent($background);
print $image->jpeg;
}