1 / 21

3.0.1.3.2 Introduction to CGI – Session 2

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. Drawing Images with Perl. CPAN provides a multitude of different modules for generating

lucio
Télécharger la présentation

3.0.1.3.2 Introduction to CGI – Session 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 3.0.1.3.2 - Introduction to CGI

  2. Drawing Images with Perl CPAN provides a multitude of different modules for generating different kinds of graphic output 3.0.1.3.2 - Introduction to CGI

  3. 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 compression algorithm) and developers resulted in dropping of GIF support in newer versions of GD.pm 3.0.1.3.2 - Introduction to CGI

  4. 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; This code uses $image->gif method, so it runs only with Version 1.19 of GD 3.0.1.3.2 - Introduction to CGI

  5. GD.pm drawing methods • Some drawing methods of GD.pm: 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; Coordinate system. The start of coordinate axis screen. X lies horizontally and Y - vertically 3.0.1.3.2 - Introduction to CGI

  6. Calling 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({-src=>”http://www.bcgsc.ca/cgi-bin/someimage.cgi”}); 3.0.1.3.2 - Introduction to CGI

  7. 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; 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 for rotating, cloning, merging Images etc 3.0.1.3.2 - Introduction to CGI

  8. 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 -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! $im2->copy($im,0,0,0,0,500,635); print header(-type=>"image/gif"); binmode STDOUT; print $im2->gif; 3.0.1.3.2 - Introduction to CGI

  9. GD::Graph modules • GD::Graph provides basic diagram modules: • Points, Bars, Pie Charts, Area, 3D graphs etc. 3.0.1.3.2 - Introduction to CGI

  10. 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; set() and plot() methods are common for all GD::Graph modules As the previous example, this code uses GIF format, so please note that it runs only with version 1.19 of GD 3.0.1.3.2 - Introduction to CGI

  11. 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 ] • ); Look for more bells and whistles in documentation for GD::Graph modules available on CPAN website www.cpan.org • $graph->set(x_label=> 'Species', • y_label=> 'chromosomes', • title => “some title”, • ) or die $graph->error; • my $image = $graph->plot(\@data); 3.0.1.3.2 - Introduction to CGI

  12. Examples from scientific websites NCBI Mapviewer. Wormbase website. 3.0.1.3.2 - Introduction to CGI

  13. Using databases in Perl • DBI and DBD interaction: Definitions: DBI - Database interface DBD - Database driver 3.0.1.3.2 - Introduction to CGI

  14. 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) 3.0.1.3.2 - Introduction to CGI

  15. 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; 3.0.1.3.2 - Introduction to CGI

  16. 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 } $ary_ref = $sth->fetchrow_arrayref; $hash_ref = $sth->fetchrow_hashref; $ary_ref = $sth->fetchall_arrayref; $ary_ref = $sth->fetchall_arrayref( $slice, $max_rows ); $sth->finish; $dbh->disconnect; Perl can disconnect on exit but it is not a good thing to leave it neglected 3.0.1.3.2 - Introduction to CGI

  17. 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”; 3.0.1.3.2 - Introduction to CGI

  18. 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; 3.0.1.3.2 - Introduction to CGI

  19. 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; 3.0.1.3.2 - Introduction to CGI

  20. 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; 3.0.1.3.2 - Introduction to CGI

  21. 3.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 DBI::CSV for apps development 3.0.1.3.2 - Introduction to CGI

More Related