Coordinate system.

Download Report

Transcript Coordinate system.

3.0.1.3.2 – Introduction to CGI
3.0.1.3.2
Introduction to CGI – Session 2
·
Introduction to CGI:





4/8/2004
Generating images with Perl
GD.pm module
Databases: introduction
Database simulation with CSV
Working Bulletin Board example
3.0.1.3.2 - Introduction to CGI
1
3.0.1.3.2 – Introduction to CGI
Drawing Images with Perl
CPAN provides a
multitude of different
modules for generating
different kinds of
graphic output
4/8/2004
3.0.1.3.2 - Introduction to CGI
2
3.0.1.3.2 – Introduction to CGI
On image formats supported by GD.pm
· GIF
· Last version of GD.pm which supports GIF format is 1.19,
· GIF features:
•
•
•
•
256 colors,
Interlace,
Animated GIF (Gif89a)
LZW compression (lossless)
Legal battle between Unisys
(creator of LZW compression
algorithm) and developers
resulted in dropping of GIF
support in newer versions of
GD.pm
· PNG
· LZW-stripped GIF,
· Additional features:
·
·
·
·
Alternative compression algorithm,
3modes: 256 color; 16-bit grayscale, 48-bit truecolor
Support for alpha channel,
Better interlacing
· JPEG
· Ideal for photographs, as it designed for encoding continuous tone images
· 24-bit color,
· Compression with losses
4/8/2004
3.0.1.3.2 - Introduction to CGI
3
3.0.1.3.2 – Introduction to CGI
GD.pm - module by Lincoln Stein
First Example: simple drawing
#!/usr/local/bin/perl -wT
use CGI qw(:standard);
use GD;
..
my $im = new GD::Image(400,400);
my %color = (black=>$im->colorAllocate(0,0,0),
white=>$im->colorAllocate(255,255,255),
yellow=>$im->colorAllocate(255,255,0)
);
$im->fill(200,200,$color{white});
$im->arc(200,200,150,150,0,360,$color{black});
$im->fill(200,200,$color{yellow});
$im->arc(170,170,10,10,0,360,$color{black});
$im->arc(230,170,10,10,0,360,$color{black});
$im->fill(200,200,$color{yellow});
$im->fill(200,200,$color{yellow});
$im->arc(200,200,110,110,0,180,$color{black});
print header(-type=>"image/gif");
binmode STDOUT;
print $im->gif;
4/8/2004
This code uses $image->gif
method, so it runs only with
Version 1.19 of GD
3.0.1.3.2 - Introduction to CGI
4
3.0.1.3.2 – Introduction to CGI
GD.pm drawing methods
· Some drawing methods of GD.pm:
·
0,0
use GD;
..
X
100,20
my $im = new GD::Image($width,$height);
Y
$im->line($x1,$y1,$x2,$y2,$color);
$im->arc($x,$y,$width,$height,$start,$end,$color);
$im->ellipse($cx,$cy,$width,$height,$color);
$image->filledRectangle($x1,$y1,$x2,$y2,$color);
$im->string($font,$x,$y,$string,$color);
$im->fill(200,200,$color);
$im->fillToBorder($x,$y,$bordercolor,$color);
Coordinate system. The start
of coordinate axis screen. X lies
horizontally and Y - vertically
print header(-type=>"image/png");
binmode STDOUT;
print $im->png;
4/8/2004
3.0.1.3.2 - Introduction to CGI
5
3.0.1.3.2 – Introduction to CGI
Calling CGI-generated images
·
Referencing Images:
·
In the URL box of a web browser:
Path
Parameter
http://www.bcgsc.ca/cgi-bin/someimage.cgi?someparam=22
·
In HTML code of your web page:
<img src=”http://www.bcgsc.ca/cgi-bin/someimage.cgi”>
·
Object-oriented way (CGI.pm style):
..
print img({-src=>”http://www.bcgsc.ca/cgi-bin/someimage.cgi”});
4/8/2004
3.0.1.3.2 - Introduction to CGI
6
3.0.1.3.2 – Introduction to CGI
Manipulating static images
#!/usr/bin/perl -w
use CGI qw(:standard);
use GD;
use IO::File;
use strict;
my $fh = new IO::File;
$fh->open("Myimage.gif") or die "Couldn't open file\n";
my $im2 = GD::Image->newFromGif($fh);
$fh->close;
GD may be also used for
rotating, cloning, merging
Images etc
my $im = new GD::Image(500,635);
my %color = (black => $im->colorAllocate(0,0,0),
white => $im->colorAllocate(255,255,255),
green => $im->colorAllocate(0,255,0)
);
$im->fill(100,100,$color{green});
$im->arc(390,100,250,150,0,360,$color{black});
$im->fill(390,100,$color{white});
$im->transparent($color{green});
$im->string(gdGiantFont,310,90,“Some stuff",$color{black});
$im2->copy($im,0,0,0,0,500,635);
print header(-type=>"image/gif");
binmode STDOUT;
print $im2->gif;
4/8/2004
3.0.1.3.2 - Introduction to CGI
7
3.0.1.3.2 – Introduction to CGI
Debriefing:
First, we are bringing in the external image into the script using
its file handle as an argument for newFromGif() method
#!/usr/bin/perl -w
use CGI qw(:standard);
use GD;
use IO::File;
use strict;
my $fh = new IO::File;
$fh->open("Myimage.gif") or die "Couldn't open file\n";
my $im2 = GD::Image->newFromGif($fh);
$fh->close;
Beware of newer methods in
later versions of GD, as the
given example might benefit
greatly by using some newer
stuff and the code would have
been much shorter!
When an image is merged with another one, its pixel data
overwrites the pixel data of the target image
$im2->copy($im,0,0,0,0,500,635);
print header(-type=>"image/gif");
binmode STDOUT;
print $im2->gif;
4/8/2004
3.0.1.3.2 - Introduction to CGI
8
3.0.1.3.2 – Introduction to CGI
GD::Graph modules
· GD::Graph provides basic diagram modules:
· Points, Bars, Pie Charts, Area, 3D graphs etc.
4/8/2004
3.0.1.3.2 - Introduction to CGI
9
3.0.1.3.2 – Introduction to CGI
Simple example of using GG::Graph::bars
· Printing bars in CGI:
set() and plot()
methods are common
for all GD::Graph modules
..
use CGI;
use GD::Graph::bars;
use constant TITLE => "Number of Chromosomes in mammals";
my $q = new CGI;
my $graph = new GD::Graph::bars(400,400);
my @data = (
[ qw(Cow Chimp Human Dog Mouse Camel)],
[ 60,48,46,78,40,74 ]
);
As the previous example, this
code uses GIF format,
so please note that it runs
only with version 1.19 of GD
$graph->set(x_label=> 'Species',
y_label=> 'chromosomes',
title => TITLE,
) or die $graph->error;
print $q->header(-type=>"image/gif");
my $image = $graph->plot(\@data);
binmode STDOUT;
print $image->gif;
4/8/2004
3.0.1.3.2 - Introduction to CGI
10
3.0.1.3.2 – Introduction to CGI
Common methods for GD::Graph modules
·
Use array of anonymous arrays to pass the data to GD::Graph modules, X
series goes first. Please note, that there are specific procedures are required
to make X axis numeric. Sort your data by X value.
· my @data = (
·
[ qw(Cow Chimp Human Dog Mouse Camel)],
·
[ 60,48,46,78,40,74 ]
·
);
·
Look for more bells and
whistles in documentation for
GD::Graph modules available
on CPAN website
Set() and plot() methods
· $graph->set(x_label=> 'Species',
·
y_label=> 'chromosomes',
·
title => “some title”,
·
) or die $graph->error;
www.cpan.org
· my $image = $graph->plot(\@data);
4/8/2004
3.0.1.3.2 - Introduction to CGI
11
3.0.1.3.2 – Introduction to CGI
Examples from scientific websites
NCBI Mapviewer.
Wormbase website.
4/8/2004
3.0.1.3.2 - Introduction to CGI
12
3.0.1.3.2 – Introduction to CGI
Using databases in Perl
· DBI and DBD interaction:
DBD
DBI
PERL SCRIPT
MySQL
Definitions:
DBI - Database interface
DBD - Database driver
DBD
Oracle
DBD
Google
4/8/2004
3.0.1.3.2 - Introduction to CGI
13
3.0.1.3.2 – Introduction to CGI
Checking on DBD drivers:
..
use CGI;
use DBI;
use CGI::Carp(fatalsToBrowser);
my $q = new CGI;
print $q->header(-type=>"text/html");
print $q->start_html(-title=>"Testing DBI
drivers");
my @drivers = DBI->available_drivers;
print $q->ul($q->li([@drivers]));
Interpreter:
usr/local/bin/perl (above)
usr/bin/perl (below)
print "CGI version ".$q->version;
print $q->end_html;
4/8/2004
3.0.1.3.2 - Introduction to CGI
14
3.0.1.3.2 – Introduction to CGI
Connection to mysql databse: example
· First, get a handle for that database:
use DBI;
my $dbh =
..
DBI->connect(‘DBI:mysql:database:host:3306’,’user’,’password’) or die “No luck\n”;
· Second, hmm….. There two things could be done:
$dbh->do(qq(insert into table_name values(‘Frodo’, ‘hobbit’,’1-900-37636’)));
· If you need to get some data from database, create a statement handle:
my $sth = $dbh->prepare(qq(select from table_name name, occupation, phone_number));
$sth->execute;
4/8/2004
3.0.1.3.2 - Introduction to CGI
15
3.0.1.3.2 – Introduction to CGI
Getting data with statement handle
· First thing to do after execution:
while(my @row = $sth->fetchrow_array){
..
do something with @row here
}
· Other methods for fetching data from statement handle:
$ary_ref
$hash_ref
$ary_ref
$ary_ref
=
=
=
=
$sth->fetchrow_arrayref;
$sth->fetchrow_hashref;
$sth->fetchall_arrayref;
$sth->fetchall_arrayref( $slice, $max_rows );
· Clean after yourself:
Perl can disconnect on
exit but it is not a good
thing to leave it neglected
$sth->finish;
$dbh->disconnect;
4/8/2004
3.0.1.3.2 - Introduction to CGI
16
3.0.1.3.2 – Introduction to CGI
DBI::CSV - testing ground for database development
DBD::CSV provides SQL-database functionality without database engine
· CSV stands for Comma Separated Values. There is no database backend in case of
CSV (no db engine running). Relies on flock() method (file locking system).
#!/usr/bin/perl -w
use DBI;
my $dbh
= DBI->connect("DBI:CSV:f_dir=/home/user/www/cgi-bin/db") or die "Couldn't connect\n”;
· CSV database understands SQL - migration to fully-functional mySQL database
requires only couple of lines of code!
· Note: CSV driver may not work correctly over NFS, the best way to make it work - run Apache on
localhost for testing CSV-powered cgi scripts. Offline scripts work just fine!
4/8/2004
3.0.1.3.2 - Introduction to CGI
17
3.0.1.3.2 – Introduction to CGI
Simple example (table creation and insertion of data)
· Simple example
· The following script does two things:
· creates a table
· puts some records into table
#!/usr/bin/perl -w
use DBI;
my $dbh = DBI->connect("DBI:CSV:f_dir=/home/user/www/cgi-bin/db") or die "Couldn't connect
to the database\n";
my $sth = $dbh->prepare(qq(
create table forum(
Name CHAR(15),
Message CHAR(100)
))
) or die "CAN NOT PREPARE STMT\n";
$sth->execute;
$dbh->do("insert into forum values('Frodo','Umm... Ring')");
$dbh->do("insert into forum values('Gollum','This is my precois')");
$dbh->do("insert into forum values('Gandalf','Relax, buddy - you can not pass')");
$dbh->disconnect;
4/8/2004
3.0.1.3.2 - Introduction to CGI
18
3.0.1.3.2 – Introduction to CGI
Connecting to database from CGI script
· Let’s build a CGI script which reads from CSV table
· This script connects to the database from the previous example, reads our records and
print them in a HTML table
..
use DBI;
my $dbh = DBI->connect("DBI:CSV:f_dir=db") or die "Couldn't connect to the database\n";
my $sth = $dbh->prepare("select * from forum") or $dbh->errstr();
$sth->execute or $dbh->errstr();
my @lines;
while(my @row = $sth->fetchrow_array){
push(@lines,td([@row]));
}
$sth->finish;
$dbh->disconnect;
print header(-type=>"text/html");
print start_html(-title=>"Creator of tables");
if(@lines){print table({-celpadding=>2,-width=>500},Tr([@lines]));}
print end_html;
4/8/2004
3.0.1.3.2 - Introduction to CGI
19
3.0.1.3.2 – Introduction to CGI
Dynamic update of CSV database from a web page
use DBI;
my $name = param("Name");
my $message = param("Message");
if($name && $message){
my $dbh = DBI->connect("DBI:CSV:f_dir=db") or die "Couldn't connect to the database\n";
$dbh->do("insert into forum values('$name','$message')");
$dbh->disconnect;
print redirect("forum_csv.cgi");
}
..
print start_form(-name =>"poster",
-action=>"forum_csv.cgi",
-method=>"post"),
"Name:",
textfield(-name =>"Name",
-maxlength=>18),
br,
textarea(-name=>"Message",
-cols=>40,
-rows=>15),
br,
submit(-name=>"sender",
-value=>"Send"),
end_form,
end_html;
4/8/2004
3.0.1.3.2 - Introduction to CGI
20
3.0.1.3.2 – Introduction to CGI
3.0.1.3.2
Introduction to CGI – Session 2
·
Images:
·
·
·
·
Database:
·
·
4/8/2004
CGI can make images dynamic
GD.pm is good for schematic drawings
Use CPAN to look for fancy stuff
DBI is your friend
Use DBI::CSV for apps development
3.0.1.3.2 - Introduction to CGI
21