Title: Introduction to CGI:
13.0.1.3.2 Introduction to CGI Session 2
- Introduction to CGI
- Generating images with Perl
- GD.pm module
- Databases introduction
- Database simulation with CSV
- Working Bulletin Board example
2Drawing Images with Perl
CPAN provides a multitude of different modules
for generating different kinds of graphic output
3On 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)
- 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,
Legal battle between Unisys (creator of LZW
compression algorithm) and developers resulted in
dropping of GIF support in newer versions
of GD.pm
4GD.pm - module by Lincoln Stein
First Example simple drawing
!/usr/local/bin/perl -wT use CGI
qw(standard) use GD .. my im new
GDImage(400,400) my color
(blackgtim-gtcolorAllocate(0,0,0),
whitegtim-gtcolorAllocate(255,255,255),
yellowgtim-gtcolorAllocate(255,255,0) )
im-gtfill(200,200,colorwhite) im-gtarc(200,20
0,150,150,0,360,colorblack) im-gtfill(200,200,
coloryellow) im-gtarc(170,170,10,10,0,360,col
orblack) im-gtarc(230,170,10,10,0,360,colorbl
ack) im-gtfill(200,200,coloryellow) im-gtfil
l(200,200,coloryellow) im-gtarc(200,200,110,11
0,0,180,colorblack) print header(-typegt"imag
e/gif") binmode STDOUT print im-gtgif
This code uses image-gtgif method, so it runs
only with Version 1.19 of GD
5GD.pm drawing methods
- Some drawing methods of GD.pm
-
use GD .. my im new GDImage(width,height)
im-gtline(x1,y1,x2,y2,color) im-gtarc(x
,y,width,height,start,end,color) im-gtellip
se(cx,cy,width,height,color) image-gtfilledR
ectangle(x1,y1,x2,y2,color) im-gtstring(f
ont,x,y,string,color)
im-gtfill(200,200,color) im-gtfillToBorder(x,
y,bordercolor,color) print
header(-typegt"image/png") binmode STDOUT print
im-gtpng
Coordinate system. The start of coordinate axis
screen. X lies horizontally and Y - vertically
6Calling CGI-generated images
- Referencing Images
- In the URL box of a web browser
- In HTML code of your web page
- Object-oriented way (CGI.pm style)
.. print img(-srcgthttp//www.bcgsc.ca/cgi-bin
/someimage.cgi)
7Manipulating static images
!/usr/bin/perl -w use CGI qw(standard) use
GD use IOFile use strict my fh new
IOFile fh-gtopen("Myimage.gif") or die
"Couldn't open file\n" my im2
GDImage-gtnewFromGif(fh) fh-gtclose my im
new GDImage(500,635) my color (black gt
im-gtcolorAllocate(0,0,0), white gt
im-gtcolorAllocate(255,255,255), green gt
im-gtcolorAllocate(0,255,0) )
im-gtfill(100,100,colorgreen) im-gtarc(390,10
0,250,150,0,360,colorblack) im-gtfill(390,100,
colorwhite) im-gttransparent(colorgreen)
im-gtstring(gdGiantFont,310,90,Some
stuff",colorblack) im2-gtcopy(im,0,0,0,0,500
,635) print header(-typegt"image/gif") binmode
STDOUT print im2-gtgif
GD may be also used for rotating, cloning,
merging Images etc
8Debriefing
- First, we are bringing in the external image
into the script using its file handle as an
argument for newFromGif() method - When an image is merged with another one, its
pixel data overwrites the pixel data of the
target image
!/usr/bin/perl -w use CGI qw(standard) use
GD use IOFile use strict my fh new
IOFile fh-gtopen("Myimage.gif") or die
"Couldn't open file\n" my im2
GDImage-gtnewFromGif(fh) fh-gtclose
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!
im2-gtcopy(im,0,0,0,0,500,635) print
header(-typegt"image/gif") binmode STDOUT print
im2-gtgif
9GDGraph modules
- GDGraph provides basic diagram modules
- Points, Bars, Pie Charts, Area, 3D
graphs etc.
10Simple example of using GGGraphbars
.. use CGI use GDGraphbars use constant
TITLE gt "Number of Chromosomes in mammals" my
q new CGI my graph new GDGraphbars(400,
400) my _at_data ( qw(Cow Chimp Human Dog
Mouse Camel), 60,48,46,78,40,74
) graph-gtset(x_labelgt 'Species',
y_labelgt 'chromosomes', title gt
TITLE, ) or die graph-gterror print
q-gtheader(-typegt"image/gif") my image
graph-gtplot(\_at_data) binmode STDOUT print
image-gtgif
set() and plot() methods are common for all
GDGraph modules
As the previous example, this code uses GIF
format, so please note that it runs only with
version 1.19 of GD
11Common methods for GDGraph modules
- Use array of anonymous arrays to pass the data to
GDGraph 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. - Set() and plot() methods
- my _at_data (
- qw(Cow Chimp Human Dog Mouse Camel),
- 60,48,46,78,40,74
- )
Look for more bells and whistles in documentation
for GDGraph modules available on CPAN website
www.cpan.org
- graph-gtset(x_labelgt 'Species',
- y_labelgt 'chromosomes',
- title gt some title,
- ) or die graph-gterror
- my image graph-gtplot(\_at_data)
12Examples from scientific websites
NCBI Mapviewer.
Wormbase website.
13Using databases in Perl
Definitions DBI - Database interface DBD -
Database driver
14Checking on DBD drivers
- ..
- use CGI
- use DBI
- use CGICarp(fatalsToBrowser)
- my q new CGI
- print q-gtheader(-typegt"text/html")
- print q-gtstart_html(-titlegt"Testing DBI
drivers") - my _at_drivers DBI-gtavailable_drivers
- print q-gtul(q-gtli(_at_drivers))
- print "CGI version ".q-gtversion
- print q-gtend_html
Interpreter usr/local/bin/perl
(above) usr/bin/perl (below)
15Connection to mysql databse example
- First, get a handle for that database
- Second, hmm.. There two things could be done
- If you need to get some data from database,
create a statement handle
use DBI my dbh DBI-gtconnect(DBImysqldatab
asehost3306,user,password) or die No
luck\n ..
dbh-gtdo(qq(insert into table_name
values(Frodo, hobbit,1-900-37636)))
my sth dbh-gtprepare(qq(select from table_name
name, occupation, phone_number)) sth-gtexecute
16Getting data with statement handle
- First thing to do after execution
- Other methods for fetching data from statement
handle - Clean after yourself
while(my _at_row sth-gtfetchrow_array) .. do
something with _at_row here
ary_ref sth-gtfetchrow_arrayref
hash_ref sth-gtfetchrow_hashref ary_ref
sth-gtfetchall_arrayref ary_ref
sth-gtfetchall_arrayref( slice, max_rows )
sth-gtfinish dbh-gtdisconnect
Perl can disconnect on exit but it is not a
good thing to leave it neglected
17DBICSV - testing ground for database
development
- DBDCSV 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). - 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!
!/usr/bin/perl -w use DBI my dbh
DBI-gtconnect("DBICSVf_dir/home/user/www/cgi-bin
/db") or die "Couldn't connect\n
18Simple 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-gtconnect("DBICSVf_dir/home/user/www/cgi-bin
/db") or die "Couldn't connect to the
database\n" my sth dbh-gtprepare(qq(
create table forum( Name
CHAR(15), Message CHAR(100) )) ) or die
"CAN NOT PREPARE STMT\n" sth-gtexecute dbh-gtdo(
"insert into forum values('Frodo','Umm...
Ring')") dbh-gtdo("insert into forum
values('Gollum','This is my precois')") dbh-gtd
o("insert into forum values('Gandalf','Relax,
buddy - you can not pass')") dbh-gtdisconnect
19Connecting to database from CGI script
- Lets 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-gtconnect("DBICSVf_di
rdb") or die "Couldn't connect to the
database\n" my sth dbh-gtprepare("select
from forum") or dbh-gterrstr() sth-gtexecute or
dbh-gterrstr() my _at_lines while(my _at_row
sth-gtfetchrow_array) push(_at_lines,td(_at_row))
sth-gtfinish dbh-gtdisconnect print
header(-typegt"text/html") print
start_html(-titlegt"Creator of tables") if(_at_lines
)print table(-celpaddinggt2,-widthgt500,Tr(_at_li
nes)) print end_html
20Dynamic update of CSV database from a web page
use DBI my name param("Name") my message
param("Message") if(name message) my
dbh DBI-gtconnect("DBICSVf_dirdb") or die
"Couldn't connect to the database\n"
dbh-gtdo("insert into forum values('name','messa
ge')") dbh-gtdisconnect print
redirect("forum_csv.cgi") .. print
start_form(-name gt"poster",
-actiongt"forum_csv.cgi",
-methodgt"post"), "Name",
textfield(-name gt"Name",
-maxlengthgt18), br,
textarea(-namegt"Message",
-colsgt40, -rowsgt15), br,
submit(-namegt"sender",
-valuegt"Send"), end_form,
end_html
213.0.1.3.2 Introduction to CGI Session 2
- Images
- CGI can make images dynamic
- GD.pm is good for schematic drawings
- Use CPAN to look for fancy stuff
- Database
- DBI is your friend
- Use DBICSV for apps development