<?php /* vim: set ts=3: */

/**********************************
 *
 * version 1.6
 *
 * Display a "scarf" progress bar using two images and a percentage value.
 *  The percentage value is either derived from a CVS (comma separated value) file
 *  or passed via URL arguments
 *
 * Arguments are passed via "?" for the first one and "&" for the rest.
 *  For example, to display a 42% full bar, flipped and starting from the right:
 *    http://yourdomain.com/path/to/scarf/scarfimg.php?flip=yes&reverse=yes&percent=42
 *
 * Valid arguments:
 *  flip = if set (to anything) the image will be flipped horizontally
 *  percent = if set to a number greater than 0, will be used instead of the value derived from the file
 *  rows = if set, ignore any "percent" and calculate the percent instead
 *  reverse = if set (to anything) the progress will come from the right
 *  season = season which scarf to use.  If not set, default to season 12
 *             valid seasons are 12, 125 (12.5) 13, 14, 15, 16, 18 and wt (whovian times)
 *  showpercent = if set to anything, will display the completion percentage as a number
 *  rotate = if set to anything, will rotate final image 90 degrees clockwise
 *  ss = spreadsheet number, must be one of the keys of the $spreadsheet[] array
 * 
 * variables that can be changed:
 *  $spreadsheet[] = array of url or file locations of CSV file with percentage value on the second line
 *  $importantcells[] = array of important cell numbers to look for.  If not set, use "importantcell"
 *  $usespreadsheet = if TRUE, attempt to get the percentage from a Comma Separate Values file/url
 *        defined in $spreadsheet
 *  $importantcell = default cell number (on second line) that hold the completion percentage
 *  $images[] = array of background and forground images and their flipped versions
 *
 * Original images and idea from http://www.mazzmatazz.co.uk/2008/Who/scarf/
 *  as described at http://community.livejournal.com/who_knits/115656.html
 *
 * Images for other scarf versions from:
 *        http://aerie.to/allscarves.gif created by http://trdsf.insanejournal.com/
 *        http://wittylittleknitter.com/
 * 
 ***********************************/

// set up some functions
 
function textsize($fontsize,$fontangle,$font,$text) { // get text width and height
    
$box imageTTFBbox($fontsize,$fontangle,$font,$text);
    
$textwidth abs($box[4] - $box[0]);
    
$textheight abs($box[5] - $box[1]);
    return array(
$textwidth,$textheight);
 }

// set up some variables
    
$spreadsheet[]="http://path-to-your-spreadsheet-in-csv-format"// set to location of spreadsheet
    
$spreadsheet[]="http://path-to-your-spreadsheet-in-csv-format"// set to location of spreadsheet
    
$spreadsheet["gotscarf"]="http://spreadsheets.google.com/pub?key=pvL6sgq1dSNF0r-YIvfO1YQ&output=csv"// set to location of spreadsheet
    
$importantcells["gotscarf"]=6;
    
$usespreadsheet=FALSE// change to TRUE if you set the $spreadsheet variable
    
$importantcell=6;  // set to cell (on the second line) that includes the percentage done
    
$imgdir="graphics";
// define all the different sets of images, for each season
    
$images[12]=array("bg"=>"scarfbg.png","fg"=>"scarfprogress.png","bgflip"=>"scarfbg-flip.png","fgflip"=>"scarfprogress-flip.png");
    
$images["12.5"]=array("bg"=>"scarfbg12.5.png","fg"=>"scarfprogress12.5.png","bgflip"=>"scarfbg12.5-flip.png","fgflip"=>"scarfprogress12.5-flip.png");
    
$images[13]=array("bg"=>"scarfbg13.png","fg"=>"scarfprogress13.png","bgflip"=>"scarfbg13-flip.png","fgflip"=>"scarfprogress13-flip.png");
    
$images[14]=array("bg"=>"scarfbg14.png","fg"=>"scarfprogress14.png","bgflip"=>"scarfbg14-flip.png","fgflip"=>"scarfprogress14-flip.png");
    
$images[15]=array("bg"=>"scarfbg15.png","fg"=>"scarfprogress15.png","bgflip"=>"scarfbg15-flip.png","fgflip"=>"scarfprogress15-flip.png","rows"=>1258);
    
$images[16]=array("bg"=>"scarfbg16.png","fg"=>"scarfprogress16.png","bgflip"=>"scarfbg16-flip.png","fgflip"=>"scarfprogress16-flip.png");
    
$images[18]=array("bg"=>"scarfbg18.png","fg"=>"scarfprogress18.png","bgflip"=>"scarfbg18-flip.png","fgflip"=>"scarfprogress18-flip.png","rows"=>1274);
    
$images["wt"]=array("bg"=>"scarfbgwt.png","fg"=>"scarfprogresswt.png","bgflip"=>"scarfbgwt-flip.png","fgflip"=>"scarfprogresswt-flip.png");
    
$images["wlk12"]=array("bg"=>"wlk12bg.png","fg"=>"wlk12fg.png","bgflip"=>"wlk12bg-flip.png","fgflip"=>"wlk12fg-flip.png","rows"=>1058);
    
$images["wlk13"]=array("bg"=>"wlk13bg.png","fg"=>"wlk13fg.png","bgflip"=>"wlk13bg-flip.png","fgflip"=>"wlk13fg-flip.png","rows"=>906);
    
$images["wlk14"]=array("bg"=>"wlk14bg.png","fg"=>"wlk14fg.png","bgflip"=>"wlk14bg-flip.png","fgflip"=>"wlk14fg-flip.png","rows"=>838);
    
$season=($_REQUEST["season"]);  // find out the season requested
    
if (!isset($images[$season])) { // if they asked for an invalid season or didn't ask at all
        
$season=12;
    }

    if (
$_REQUEST["ss"]) { // if they asked for a specific spreadsheet
        
$ss=$_REQUEST["ss"];
        if (!
array_key_exists($ss,$spreadsheet)) { // make sure the choice is valid
            
$ss=0;
        }
        if (
array_key_exists($ss,$importantcells)) { // if a custom important cell is defined, override default
            
$importantcell=$importantcells[$ss];
        }
    } else {
        
$ss=0;
    }

    if (
$_REQUEST["flip"]) { // if we want to flip it
        
$background=$images[$season]["bg"];
        
$foreground=$images[$season]["fg"];
    } else {
        
$background=$images[$season]["bgflip"];
        
$foreground=$images[$season]["fgflip"];
    }

  if (isset(
$_REQUEST["percent"])) { // if a percent was asked for
        
$percent=$_REQUEST["percent"];
    } elseif (
$usespreadsheet) { // read in the spreadsheet
        
$handle fopen($spreadsheet[$ss],"r");
        
$discardrow=fgetcsv($handle,1000,",");
        
$importantrow=fgetcsv($handle,1000,",");
        
fclose($handle);
        
$percent=$importantrow[$importantcell];
    } else {
        
$percent=0;
    }

   if (isset(
$_REQUEST["debug"])) { // if debugging, print the input and then quit
        
echo "ss='$ss'<br>importantcell='$importantcell'<br>";
        echo 
"spreadsheet='<a href='".$spreadsheet[$ss]."'>".$spreadsheet[$ss]."</a>'<br>";
        echo 
"percent='$percent'<br>";
        echo 
"ut<p>";
        echo 
"discardrow='<pre>";
        
var_dump($discardrow);
        echo 
"</pre>'<br>";
        echo 
"importantrow='<pre>";
        
var_dump($importantrow);
        echo 
"</pre>'<br>";
        exit;
    }
// read in the background image
    
$img=imagecreatefrompng("$imgdir/$background");
    
$bg=imagecreatefrompng("$imgdir/$background");
// read in the foreground image
    
$fg=imagecreatefrompng("$imgdir/$foreground");
// figure out how wide and tall the foreground is
    
$fgwidth=imagesx($img);
    
$fgheight=imagesy($img);

// if they asked for rows, ignore any percent passed and just calculate it
    
if (isset($_REQUEST["rows"])) { // if they asked for a specific number of rows
        
if (isset($images[$season]["rows"])) { // if there is an override row
            
$rows_total=$images[$season]["rows"];
        } else { 
// get the row total from the file size
            
$rows_total=$fgwidth*2// most of the patterns have 2 rows a pixel
        
}
        
$rows_done=$_REQUEST["rows"];
        
$percent=($rows_done/$rows_total)*100;
    }

    if (
$percent>100) { // do a little validation so we don't get bogus numbers passed
        
$percent=100;
    }

// change percentage to 2 digets after the decimal point max
    
$percent=round($percent*100)/100;

// figure out the width in pixels that goes with the percent completed
    
$copywidth=floor($fgwidth*($percent/100));
    if(
$_REQUEST["reverse"]) { // if we want to count from the right instead of left
        
$startx=$fgwidth-$copywidth;
    } else {
        
$startx=0;
    }

// copy the forground over the back
    
imagecopy($img,$fg,$startx,0,$startx,0,$copywidth,$fgheight);

// if they request it, overlay some text on the image
    
if ($_REQUEST["showpercent"]) {
        
$black imagecolorallocate($img,0,0,0);
        
$font 'arial';
        
$fontsize 20;
        
$fontangle 0;
        
$text="$percent%"

        
$sizea1 textsize($fontsize,$fontangle,$font,$text);

        
$xcord1 = ($fgwidth-$sizea1[0])/2;
        
$ycord1 = ($fgheight/2)+($sizea1[1]/2);

        
// copy a box of the bg
        
imagecopy($img,$bg,$xcord1,4,$xcord1,4,$sizea1[0]+1,$fgheight-8);
        
ImageTTFText ($img$fontsize$fontangle$xcord1$ycord1$black$font$text);
    }
    
// if they request it, rotate the image 90 degrees.
    
if ($_REQUEST["rotate"]) {
        
$img =  imagerotate($img,-90,0);
    }

// output a png version of the new image
    
header('Content-Type: image/png');
    
imagepng($img);
// clean up memory
    
imagedestroy($img);
    
imagedestroy($fg);
    
imagedestroy($bg);
?>