<?PHP
include "pplib.inc.php";

pp_loginbysession();

$draw39_seq = array (
"nnnwwnwnnn",
"wnnwnnnnwn",
"nnwwnnnnwn",
"wnwwnnnnnn",
"nnnwwnnnwn",
"wnnwwnnnnn",
"nnwwwnnnnn",
"nnnwnnwnwn",
"wnnwnnwnnn",
"nnwwnnwnnn"
);

$startstop = "nwnnwnwnnn";

$nwidth=5;
$wwidth=14;
$workerid = trim(pp_postfield("workerid"));
$sqlcmd = "SELECT * FROM $dbname.pworker WHERE WORKER_ID='".ppaddslashes($workerid)."' AND WORKER_PlantPTR='".ppaddslashes($PPLANTID)."' LIMIT 1";
$result = mysqli_query($ppconf["mysqli_linkid"], $sqlcmd);
if ($myrow = mysqli_fetch_array($result)) {
	$WORKER_Barcode = $myrow["WORKER_Barcode"];
} else {
	die("Session expired");
}
mysqli_free_result($result);

$seq = "";
for ($i=0;$i < strlen($WORKER_Barcode);$i++) {
	$c = ord($WORKER_Barcode[$i]) - ord(0);
	if ($c < 0) $c = 0;
	if ($c > 9) $c = 0;
	$seq .= $draw39_seq[$c];	
}
$seq = $startstop.$seq.$startstop;

$imgwidth=0;
for ($i=0;$i < strlen($seq); $i++) {
	if ($seq[$i] == "n") {
		$imgwidth += $nwidth;
	} else {
		$imgwidth += $wwidth;
	}
}
$imgheight = 4;
$gif = ImageCreate($imgwidth,$imgheight);
$ca = array();
$ca[0] = ImageColorAllocate($gif,255,255,255);
$ca[1] = ImageColorAllocate($gif,0,0,0);

ImageFilledRectangle($gif,0,0,$imgwidth-1,$imgheight-1,$ca[0]);

$x = 0;
$col = 1;
for ($i=0;$i < strlen($seq); $i++) {
	if ($seq[$i] == "n") {
		$w = $nwidth;
	} else {
		$w = $wwidth;
	}
	ImageFilledRectangle($gif,$x,0,$x+$w-1,$imgheight - 1,$ca[$col]);
	$x += $w;
	$col = 1 - $col;
}
header("content-type: image/png");
ImagePNG($gif);
?>

