<?
    
require_once('nanofuncs.php');

    
$width = 140;
    
$height = 325;
    
$border = 2;
    
$novelfile = "/home/blob/nano/Unfathomable.txt";
    
$title = "Unfathomable";
    
$title_color = "#000200";
    
$border_color = "#000200";
    
$image = "/var/www/html/images/2007_participant.gif";
    
$ttfont = "/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf";
    
$fontsize = 8;
    
$title_ttfont = "/usr/share/fonts/truetype/ttf-bitstream-vera/VeraBI.ttf";
    
$title_fontsize = 9;
    
$progress_color = "#000200";
    
$background_color = "#efe603";
    
$icon_offset = 8;
    
$progress_offset = 4;
    
$progress_height = 18;
    
$progress_thickness = 1;
    
$goal_color = "#f01723";

    
$maxx = $width - 1;
    
$maxy = $height - 1;
    switch (
strtolower(substr($image, strlen($image) - 3, 3)) ) {
        case
'peg':
        case
'jpg':
            
$icon = imagecreatefromjpeg($image);
            break;
        case
'png':
            
$icon = imagecreatefrompng($image);
            break;
        case
'gif':
        default:
            
$icon = imagecreatefromgif($image);
            break;
    }

    
$im = imagecreatetruecolor($width, $height);

    
$c = splitcolor($background_color);
    
$bc = imagecolorallocate($im, $c["r"], $c["g"], $c["b"]);

    
$c = splitcolor($border_color);
    
$fc = imagecolorallocate($im, $c["r"], $c["g"], $c["b"]);

    
$c = splitcolor($title_color);
    
$tc = imagecolorallocate($im, $c["r"], $c["g"], $c["b"]);

    
$c = splitcolor($progress_color);
    
$pc = imagecolorallocate($im, $c["r"], $c["g"], $c["b"]);

    
$c = splitcolor($goal_color);
    
$gc = imagecolorallocate($im, $c["r"], $c["g"], $c["b"]);

    
imagefill($im, 0, 0, $bc);

    if (
$border ) {
        
box($im, 0, 0, $maxx, $maxy, $border, $fc);
    }

    if (
$icon ) {
        
$maxw = $width - $border - $border - $icon_offset - $icon_offset;
        
$maxh = $height - $border - $border - $icon_offset - $icon_offset;

        
$iw = imagesx($icon);
        
$ih = imagesy($icon);

        if (
$iw > $maxw || $ih > $maxh ) {
            
$xscale = $maxw / $iw;
            
$yscale = $maxh / $ih;
            
$scale = ( $xscale > $yscale ? $yscale : $xscale );
            
imagecopyresized($im, $icon, $border + $icon_offset,
                
$border + $icon_offset, 0, 0,
                
$iw * $scale, $ih * $scale, $iw, $ih);
        } else {
            
imagecopy($im, $icon, ($maxw - $iw)/2 + $border + $icon_offset,
                
$border + $icon_offset, 0, 0, $iw, $ih);
        }
    }

    
$x1 = $border + $progress_offset;
    
$x2 = $maxx - $border - $progress_offset;
    
$y2 = $maxy - $border;

    if (
intval(Date("m")) == 11 ) {
        
$day = intval(Date("d"));
        
$goal = intval(round(50000 * $day / 30), 0);

        
$goaltext = sprintf("Day %d Goal: %s", $day, number_format($goal,0));

        
$textbox = imagettfbbox($fontsize, 0, $ttfont, $goaltext);

        
$tw = $textbox[2] - $textbox[0];
        
$th = $textbox[1] - $textbox[7];

        
$y2 = $y2 - $progress_offset - $th;

        
imagettftext($im, $fontsize, 0, ($x2 - $x1 - $tw)/2 + $x1, $y2 -
            (
$textbox[7] < 0?$textbox[7]:0), $gc, $ttfont, $goaltext);
    }

    
$wc = exec("wc " . escapeshellarg($novelfile) . " | sed " .
        
escapeshellarg('s/[\t ][\t ]*/,/g'));
    
$wc = split(",", $wc);
    
$words = intval($wc[2]);
    
$wordtext = sprintf("%s / 50,000", number_format($words,0));

    
$textbox = imagettfbbox($fontsize, 0, $ttfont, $wordtext);

    
$tw = $textbox[2] - $textbox[0];
    
$th = $textbox[1] - $textbox[7];

    
$y2 = $y2 - $progress_offset - $th;

    
imagettftext($im, $fontsize, 0, ($x2 - $x1 - $tw)/2 + $x1, $y2 -
        (
$textbox[7] < 0?$textbox[7]:0), $pc, $ttfont, $wordtext);

    if (
$title ) {
        
$textbox = imagettfbbox($title_fontsize, 0, $title_ttfont, $title);

        
$tw = $textbox[2] - $textbox[0];
        
$th = $textbox[1] - $textbox[7];

        
$y2 = $y2 - $progress_offset - $th;

        
imagettftext($im, $title_fontsize, 0, ($x2 - $x1 - $tw)/2 + $x1, $y2 -
            (
$textbox[7] < 0?$textbox[7]:0), $tc, $title_ttfont, $title);
    }

    
$y2 = $y2 - $progress_offset;
    
$y1 = $y2 - $progress_height - $progress_thickness - $progress_thickness;

    
box($im, $x1, $y1, $x2, $y2, $progress_thickness, $pc);

    
$ppct = ($words > 50000?50000:$words) / 50000;

    
$x1 = $x1 + $progress_offset;
    
$y1 = $y1 + $progress_offset;
    
$x2 = $x1 + round(($x2 - $progress_offset - $x1) * $ppct,0);
    
$y2 = $y2 - $progress_offset;

    
imagefilledrectangle($im, $x1, $y1, $x2, $y2, $pc);

    if (
intval(Date("m")) == 11 ) {
        
$x2 = $maxx - $border - $progress_offset - $progress_offset;
        
$ppct = $goal / 50000;
        
$x2 = $x1 + round(($x2 - $progress_offset - $x1) * $ppct,0);

        
$goalpip = ( $progress_offset < 2 ? $progress_offset : 2 );

        
imagefilledrectangle($im, $x2, $y1 - $goalpip, $x2, $y2 + $goalpip, $gc);
    }

    
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    
header("Cache-Control: no-cache, must-revalidate");
    
header("Pragma: no-cache");
    
header('Content-type: image/png');
    
imagepng($im);
?>