# ProgresBar.pm : an object for generating simple.gif-based progress bars # with a groovy little 3-d effect # # Author: Steve McNabb steve@tralt.com # my employer (Training Alternatives Inc.) has graciously # allowed me to release this tool as open source software # # This script is copyright 1999 Training Alternatives Inc # all rights reserved - http://www.trainingalternatives.com # # you may use and/or modify this script under the same terms as # Perl itself. See your distribution for details. use GD; package ProgressBar; sub new { my $self = {}; bless $self; return $self; } sub makebar # $score value, $total out of { #this sub will accept values from user or set defaults my $class = shift @_; my ($score, $total, $width , $height) = @_; my $avg_right = $score / $total; if(($avg_right > 0.95)&&($avg_right != 1.0)) { $avg_right = 0.95; } if($avg_right == 1.0) { $avg_right = 1;} # defaults if(!$width) { $width = 150; } if(!$height) { $height = 10; } $prog_gif = new GD::Image($width,$height); # for now, we will only allow pre-defined colours $white = $prog_gif->colorAllocate(255,255,255); $black = $prog_gif->colorAllocate(0,0,0); $red = $prog_gif->colorAllocate(255,0,0); $lt_red = $prog_gif->colorAllocate(255,160,160); $dk_red = $prog_gif->colorAllocate(170,0,0); $green = $prog_gif->colorAllocate(0,200,0); $lt_green = $prog_gif->colorAllocate(100,255,100); $dk_green = $prog_gif->colorAllocate(0,150,0); #lay down the base image color (the colour of the "done" part) $prog_gif->rectangle(0,0,$width-1,$height-1,$green); $prog_gif->fill(1,1,$green); #now the "wrong" part if($avg_right != 1.0) { $midpoint = $width*$avg_right; } #so we only need to calculate this once if($avg_right != 1) { $prog_gif->rectangle($midpoint,0,$width,$height,$black); $prog_gif->fill($midpoint+1, $height-1, $red); #let's 3-d the red part (add dark lines to bottom and right, light lines to top and left) $prog_gif->line($midpoint+1,1,$width-1,1,$lt_red); $prog_gif->line($midpoint+1,1,$midpoint+1, $height-1, $lt_red); $prog_gif->line($midpoint+2,$height-2,$width-1, $height-2, $dk_red); $prog_gif->line($width-2,2,$width-2,$height-2,$dk_red); #now let's 3-d the green part $prog_gif->line(1,1,$midpoint-1,1,$lt_green); $prog_gif->line(1,1,1,$height-1,$lt_green); $prog_gif->line(1,$height-2,$midpoint-1,$height-2,$dk_green); $prog_gif->line($midpoint-1, 1, $midpoint-1, $height-2,$dk_green); } if($avg_right == 1) { #just make the whole thing green -- we're at full $midpoint = $width; $prog_gif->line(1,1,$midpoint-1,1,$lt_green); $prog_gif->line(1,1,1,$height-1,$lt_green); $prog_gif->line(1,$height-2,$midpoint-1,$height-2,$dk_green); $prog_gif->line($midpoint-1, 1, $midpoint-1, $height-2,$dk_green); } # Put a black frame around the bar $prog_gif->rectangle(0,0,$width-1,$height-1,$black); # fire 'er back binmode STDOUT; #WINDOWS SUCKS! :p print "Content-type: image/gif\n\n"; print STDOUT $prog_gif->gif; exit; } 1; =item Name ProgressBar.pm - a module for making progress bars =item Synopsis use ProgressBar; $my_bar = new ProgressBar; $my_bar->MakeBar(25,50,100,20); #$my_bar->Makebar(score, out-of-total,optional width, optional height) #this will return a binary mode stream (.gif) and appropriate content header Here is an example of how to use this module as a real-time generated progress bar gif Script: (part one -- saved as /cgi-bin/progress.cgi) use ProgressBar; use CGI qw(:standard); if(param()) { my $score = param("score"); my $total = param("total"); $foo = new ProgressBar; $foo->MakeBar($score,$total,"400","10"); #400 pixels wide, 10 pixels high..defaults to 150 x 10 } Image reference: (part two - included in html file that will reference this script as an image) Note: in this case, I am passing score and total as regular old CGI name/value pairs ...your milage may vary =item Author Steve McNabb steve@tralt.com