<?php

/* CONFIG */
$x	= 150;
$y	= 30;

$bgcol	= array(255, 255, 255);
$focol	= array(0, 0, 0);
$licol1	= array(150, 149, 243);
$licol2	= array(15, 85, 164);

//$fontName = str_replace('capimg.php', 'garr66w.ttf', $_SERVER['SCRIPT_FILENAME']);

$fontAr = array('shoplift.ttf');


$fontName = str_replace('capimg.php', $fontAr[rand(0, count($fontAr) - 1)], $_SERVER['SCRIPT_FILENAME']);


@session_start();

if ($_GET['slave']) {
	$slave = $_GET['slave'];
} else {
	$slave = 'pin_gen_con';
}

if (isset($_SESSION[$slave]) && $_SESSION[$slave] != '') {
	unset($_SESSION[$slave]);
}

function Code($anzahl) {
	$keychars = 'acdefgkmnpqrstuvwxyzABCDEFGHKLMNPQRSTVWXYZ23456789';
	$length = $anzahl;

	$randkey = '';
	$max = strlen($keychars) - 1;

	for ($i = 0; $i <= $length; ++$i) {
		$randkey .= substr($keychars, rand(0, $max), 1);
	}

	return $randkey;
}

function CodeWord() {
	include_once '__swordlist.inc.php';

	$theword = '';

	$randkey = $wordlist[rand(0, count($wordlist) - 1)];

	for ($i = 1; $i <= strlen($randkey); ++$i) {
		$randi = rand(1, 50);

		if ($randi < 26) {
			$theword .= strtolower(substr($randkey, $i - 1, 1));
		} else {
			$theword .= strtoupper(substr($randkey, $i - 1, 1));
		}
	}

	$theword = str_replace('l', 'L', $theword);
	$theword = str_replace('I', 'i', $theword);

	return $theword;
}

#$_SESSION['pin_gen'] = Code(4);
$_SESSION[$slave] = CodeWord();

$im = @imagecreate($x, $y);
$background_color	= imagecolorallocate($im, $bgcol[0], $bgcol[1], $bgcol[2]);
$text_color		= imagecolorallocate($im, $focol[0], $focol[1], $focol[2]);
$licolor[0]		= imagecolorallocate($im, 0, 0, 0);
$licolor[1]		= imagecolorallocate($im, 0, 0, 0);
$licolor[2]		= imagecolorallocate($im, 120, 120, 120);

$licolor[5]		= imagecolorallocate($im, 230, 0, 0);
$licolor[6]		= imagecolorallocate($im, 255, 255, 255);

$a = Code(7);

for ($i = 1; $i <= 30; ++$i) {
	$lix = rand(0, $x);
        $liy = rand(0, $y);

	imagesetthickness($im, 1);
//	imageline($im, $lax, $lay, $lix, $liy, $licolor[rand(0, 1)]);

	$lax = $lix;
	$lay = $liy;
}


for ($i = 1; $i <= 400; ++$i) {
	$lix = rand(0, $x);
        $liy = rand(0, $y);
       	imagefilledellipse($im, $lix, $liy, 1, 1, $licolor[0]);

	$lix = rand(0, $x);
        $liy = rand(0, $y);
//       	imagefilledellipse($im, $lix, $liy, 1, 1, $licolor[0]);

	$lix = rand(0, $x);
        $liy = rand(0, $y);
//       	imagefilledellipse($im, $lix, $liy, 1, 1, $licolor[0]);
}

//$fontSize = ($x / 6 <= $y / 2) ? $x / 6 : $y / 2;

$fontSize = 25;

$posis_chk = 50;
$posis_chk_x = 150;

while ($posis_chk > 45 || $posis_chk_x > 145) {
	--$fontSize;

	$posis = imagettfbbox($fontSize, 0, $fontName, $_SESSION[$slave]);

	$posis_chk = $posis[7];
	$posis_chk *= -1;

	$posis_chk_x = $posis[2];
}

//$posis = imagettfbbox($fontSize, 0, $fontName, $_SESSION['pin_gen']);

imagettftext($im, $fontSize, 0, ($x - $posis[2]) / 2, ($y - $posis[7]) / 2, $text_color, $fontName, $_SESSION[$slave]);

for ($i = 1; $i <= 10; ++$i) {
	$lix = rand(0, $x);
        $liy = rand(0, $y);
       	imagefilledellipse($im, $lix, $liy, 4, rand(1, 2), $background_color);
}

header("Content-type: image/png");
imagepng($im);

?>