3.0.1.3.2 – introduction to cgi 4/8/2004 3.0.1.3.2 - introduction to cgi 1 3.0.1.3.2 introduction...

21
4/8/2004 3.0.1.3.2 - Introduction to CGI 1 .1.3.2 – Introduction to CGI 3.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

Upload: millicent-ashlee-jones

Post on 03-Jan-2016

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 1

3.0.1.3.2 – Introduction to CGI

3.0.1.3.2Introduction to CGI – Session 2

· Introduction to CGI:

Generating images with Perl GD.pm module Databases: introduction Database simulation with CSV Working Bulletin Board example

Page 2: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 2

3.0.1.3.2 – Introduction to CGI

Drawing Images with Perl

CPAN provides a multitude of differentmodules for generating different kinds of graphic output

Page 3: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 3

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)

· 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

Legal battle between Unisys(creator of LZW compressionalgorithm) and developersresulted in dropping of GIFsupport in newer versions ofGD.pm

Page 4: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 4

3.0.1.3.2 – Introduction to CGI

GD.pm - module by Lincoln Stein

#!/usr/local/bin/perl -wTuse 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;

First Example: simple drawing

This code uses $image->gifmethod, so it runs only withVersion 1.19 of GD

Page 5: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 5

3.0.1.3.2 – Introduction to CGI

GD.pm drawing methods

0,0

100,20

X

Y

· Some drawing methods of GD.pm:·

Coordinate system. The start of coordinate axis screen. X lies horizontally and Y - vertically

use GD;..

my $im = new GD::Image($width,$height);

$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);

print header(-type=>"image/png");binmode STDOUT;print $im->png;

Page 6: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 6

3.0.1.3.2 – Introduction to CGI

Calling CGI-generated images

http://www.bcgsc.ca/cgi-bin/someimage.cgi?someparam=22

Path Parameter

<img src=”http://www.bcgsc.ca/cgi-bin/someimage.cgi”>

· 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({-src=>”http://www.bcgsc.ca/cgi-bin/someimage.cgi”});

Page 7: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 7

3.0.1.3.2 – Introduction to CGI

Manipulating static images

#!/usr/bin/perl -wuse 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;

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;

GD may be also used forrotating, cloning, mergingImages etc

Page 8: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 8

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

When an image is merged with another one, its pixel data overwrites the pixel data of the target image

#!/usr/bin/perl -wuse 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;

$im2->copy($im,0,0,0,0,500,635);print header(-type=>"image/gif");binmode STDOUT;print $im2->gif;

Beware of newer methods inlater versions of GD, as the given example might benefit greatly by using some newer stuff and the code would have been much shorter!

Page 9: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 9

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.

Page 10: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 10

3.0.1.3.2 – Introduction to CGI

Simple example of using GG::Graph::bars

· Printing bars in CGI:

..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 ] );

$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;

As the previous example, this code uses GIF format,so please note that it runs only with version 1.19 of GD

set() and plot()methods are commonfor all GD::Graph modules

Page 11: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 11

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.

· Set() and plot() methods

· my @data = (· [ qw(Cow Chimp Human Dog Mouse Camel)],· [ 60,48,46,78,40,74 ]· );

· $graph->set(x_label=> 'Species',· y_label=> 'chromosomes',· title => “some title”,· ) or die $graph->error;

· my $image = $graph->plot(\@data);

Look for more bells and whistles in documentation for GD::Graph modules available on CPAN website

www.cpan.org

Page 12: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 12

3.0.1.3.2 – Introduction to CGI

Examples from scientific websites

NCBI Mapviewer.

Wormbase website.

Page 13: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 13

3.0.1.3.2 – Introduction to CGI

Using databases in Perl

· DBI and DBD interaction:

Definitions:DBI - Database interfaceDBD - Database driver

PE

RL

SC

RIP

T

DB

I

DBD

DBD

DBD

MySQL

Oracle

Google

Page 14: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 14

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]));

print "CGI version ".$q->version;

print $q->end_html;

Interpreter:

usr/local/bin/perl (above)

usr/bin/perl (below)

Page 15: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 15

3.0.1.3.2 – Introduction to CGI

Connection 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->connect(‘DBI:mysql:database:host:3306’,’user’,’password’) or die “No luck\n”;..

$dbh->do(qq(insert into table_name values(‘Frodo’, ‘hobbit’,’1-900-37636’)));

my $sth = $dbh->prepare(qq(select from table_name name, occupation, phone_number));$sth->execute;

Page 16: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 16

3.0.1.3.2 – Introduction to CGI

Getting data with statement handle

· First thing to do after execution:

· Other methods for fetching data from statement handle:

· Clean after yourself:

while(my @row = $sth->fetchrow_array){ .. do something with @row here}

$sth->finish;$dbh->disconnect;

$ary_ref = $sth->fetchrow_arrayref; $hash_ref = $sth->fetchrow_hashref; $ary_ref = $sth->fetchall_arrayref; $ary_ref = $sth->fetchall_arrayref( $slice, $max_rows );

Perl can disconnect onexit but it is not a goodthing to leave it neglected

Page 17: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 17

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).

· 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->connect("DBI:CSV:f_dir=/home/user/www/cgi-bin/db") or die "Couldn't connect\n”;

Page 18: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 18

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;

Page 19: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 19

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;

Page 20: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 20

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;

Page 21: 3.0.1.3.2 – Introduction to CGI 4/8/2004 3.0.1.3.2 - Introduction to CGI 1 3.0.1.3.2 Introduction to CGI – Session 2 · Introduction to CGI:  Generating

4/8/2004 3.0.1.3.2 - Introduction to CGI 21

3.0.1.3.2 – Introduction to CGI

3.0.1.3.2Introduction 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 DBI::CSV for apps

development