#!/usr/bin/perl use CGI qw(:standard); #use lib '/home/toby/usr/lib', '/home/toby/usr/lib/arch'; use GD; $polls="/home/toby/webs/www.qtq.org/htdocs/mud2/NOW/polls"; if (length(param('question'))<1) { print header(-type=>'image/gif', -expires=>'now'); $smallim = new GD::Image(2,2); $white = $smallim->colorAllocate(255,255,255); $smallim->transparent($white); $smallim->fill(1,1,$white); binmode STDOUT; # Convert the image to GIF and print it on standard output print $smallim->gif; exit; } $pollname=param('question'); $polldir="$polls/$pollname"; $polllock="$polldir/lock"; $answerfile="$polldir/answers.txt"; $questionfile="$polldir/question.txt"; open (QUESTION,"$questionfile"); $question=; chomp($question); close (QUESTION); # Here should check to see if this user # has already voted. # Do this by comparing the cookie containing # his last voting time with the time # this poll started. # Wait until no lock exists. while (-e $polllock) { } # Create a lock. open (LOCK,">$polllock"); print LOCK localtime(); close (LOCK); open (ANSWERS,"$answerfile"); $ansnum=0; while () { chomp; $answer[$ansnum++]=$_; } close (ANSWERS); $maxcount=-1; $maxindex=-1; $total=0; for ($i=0;$i<$ansnum;$i++) { $filenum=$i+1; $countfile="$polldir/a$filenum.txt"; if (open(ANSWER,"$countfile")) { $count[$i]=; chomp($count[$i]); close(ANSWER); } else { $count[$i]=0; } $total+=$count[$i]; if ($count[$i]>$maxcount) { $maxcount=$count[$i]; $maxindex=$i; } } # Remove the lock. unlink($polllock); if ($maxcount==0) { $maxcount=1; } $plot_width=150; $width_unit=$plot_width/($ansnum+(($ansnum+1)*1.00)); ($fontw,$fonth) = (gdSmallFont->width,gdSmallFont->height); ($tinyw,$tinyh) = (gdTinyFont->width,gdTinyFont->height); ($medw,$medh) = (gdMediumBoldFont->width,gdMediumBoldFont->height); $max_bottom_margin=0; for ($i=0;$i<$ansnum;$i++) { ($temp1,$temp2)=&max_dimensions($answer[$i],100); if ($temp1>$max_bottom_margin) { $max_bottom_margin=$temp1; } } for ($i=0;$i<$ansnum;$i++) { ($temp1,$temp2)=&max_dimensions($answer[$i],$max_bottom_margin); $answer_height[$i]=$temp1; $answer_width[$i]=$temp2; } $plot_height=175; $height_unit=$plot_height/$maxcount; $top_margin=($fonth*3)+($fonth/2); $bottom_margin=$max_bottom_margin; $qmargin=$medw*(&calc_question_lines($question)+0.5); # create a new image $im = new GD::Image($plot_width,$plot_height+$top_margin+$bottom_margin+$qmargin); # allocate some colors $white = $im->colorAllocate(255,255,255); $black = $im->colorAllocate(0,0,0); $red = $im->colorAllocate(255,207,96); $tred = $im->colorAllocate(222,0,0); $blue = $im->colorAllocate(0,0,255); $color[0] = $im->colorAllocate(255,207,96); $color[1] = $im->colorAllocate(255,207,96); $color[2] = $im->colorAllocate(255,207,96); $color[3] = $im->colorAllocate(255,207,96); $color[4] = $im->colorAllocate(255,207,96); $color[5] = $im->colorAllocate(255,207,96); $color[6] = $im->colorAllocate(255,207,96); $color[7] = $im->colorAllocate(255,207,96); $color[8] = $im->colorAllocate(255,207,96); $color[9] = $im->colorAllocate(255,207,96); $color[10] = $im->colorAllocate(255,207,96); $color[11] = $im->colorAllocate(255,207,96); # make the background transparent and interlaced $im->transparent($white); $thick_brush = new GD::Image(2,2); $twhite = $thick_brush->colorAllocate(255,255,255); $tblack = $thick_brush->colorAllocate(0,0,0); $thick_brush->transparent($twhite); $thick_brush->fill(1,1,$tblack); # Set the brush $im->setBrush($thick_brush); for ($i=0;$i<$ansnum;$i++) { if ($count[$i]!=0) { &plot_bar($i); } &plot_label($i,$answer[$i]); } &plot_axis; $cookie = cookie( -name=>'CookieCheck', -value=>"Enabled", -path=>'/', -domain=>'.qtq.org', -secure=>0); print header( -cookie=>$cookie, -type=>'image/gif', -expires=>'now'); # make sure we are writing to a binary stream binmode STDOUT; # Convert the image to GIF and print it on standard output print $im->gif; sub plot_axis { $im->line(0,$top_margin+$plot_height+$qmargin,$plot_width,$top_margin+$plot_height+$qmargin,gdBrushed); &plot_question_lines($question); } sub plot_bar { local ($index,$lx,$by,$rx,$ty,$py); $index=$_[0]; $lx=calc_plotx($index); $by=calc_ploty(0); $rx=$lx+$width_unit; $ty=calc_ploty($count[$index]); $im->rectangle($lx,$by,$rx,$ty,gdBrushed); $mx=$lx+(($rx-$lx)/2); $my=$ty+(($by-$ty)/2); $im->fill($mx,$my,$color[$index]); $number=$count[$index]; if ($ansnum>=8) { $py=$ty-$fonth; } else { $py=$ty-($fonth*2); } $im->string(gdSmallFont,$mx-(($fontw*length($number))/2),$py,$number,$black); if ($ansnum<8) { $number=int((($count[$index]/$total)*100.0)+0.5); $number="($number%)"; $im->string(gdSmallFont,$mx-(($fontw*length($number))/2),$ty-$fonth,$number,$black); } } sub calc_plotx { local ($index); $index=$_[0]; return(($width_unit*1.00)+($width_unit*(1+1.00)*$index)); } sub calc_ploty { local ($height); $height=$_[0]; return(($plot_height-($height*$height_unit))+$top_margin+$qmargin); } sub max_dimensions { local ($answer,$max_height,@words,$_,$ptr,$length_word,@lines,$i,$j,$width); $answer=$_[0]; $max_height=$_[1]; @words=split(/ /,$answer); foreach (@words) { if ((length($_)*$fontw)>$max_height) { $max_height=length($_)*$fontw; } } $ptr=0; $i=0; foreach (@words) { $length_word=length($_)*$fontw; if (($ptr+$length_word)>$max_height) { # print "($_ )\n"; $line[$i]=$ptr; # print "max line $i: height=$line[$i]\n"; $i++; $ptr=$length_word+$fontw; $line[$i]=$ptr; } else { $ptr+=$length_word+$fontw; $line[$i]=$ptr; # print "[$_ ]\n"; } } $i++; $max_height=-1; for ($j=0;$j<$i;$j++) { if ($line[$j]>$max_height) { $max_height=$line[$j]; } } $width=($i)*$fonth; # print " -> lines=$i\n"; return ($max_height+2,$width); } sub plot_label { local ($index,$answer,$ptr,$length_word,$y,$x,@words); $index=$_[0]; $answer=$_[1]; @words=split(/ /,$answer); $y=0; $x=&calc_plotx($index)+($width_unit/2)-($answer_width[$index]/2); foreach (@words) { $length_word=length($_)*$fontw; if (($y+$length_word)>$bottom_margin) { # $x+=$fonth; $x+=($fonth/3)*2; $y=0; } $im->stringUp(gdSmallFont,$x,$top_margin+$qmargin+$plot_height+($bottom_margin-$y)-1,$_,$black); $y+=$length_word+$fontw; } } sub calc_question_lines { local ($question,@words,$x,$lines); $question=$_[0]; $x=0; $lines=0; @words=split(/ /,$question); foreach (@words) { if (($x+length($_)*$medw)>$plot_width) { $lines++; $x=(length($_)+1)*$medw; } else { $x+=(length($_)+1)*$medw; } } $lines++; # print "{$lines}\n"; return ($lines); } sub plot_question_lines { local ($question,@words,$x,$y,$i,$index); $question=$_[0]; $x=0; $y=0; $startprint=0; $lastsp=0; for($i=0;$i$plot_width) { $outstring=substr($question,$startprint,$lastsp-$startprint-1); # print "["; # print $outstring; # print "]\n"; $x=($plot_width-(length($outstring)*$medw))/2; $im->string(gdMediumBoldFont,$x,$y,$outstring,$tred); $y+=$medh; $x=0; $startprint=$lastsp; } $lastsp=$i+1; } if ($lasti<$lastsp) { $outstring=substr($question,$startprint,$lastsp-$startprint); $x=($plot_width-(length($outstring)*$medw))/2; $im->string(gdMediumBoldFont,$x,$y,$outstring,$tred); # print "["; # print $outstring; # print "]\n"; } }