<?php

session_start();

header("Content-type: image/jpeg");

// Draw the Spiral
function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 )
{
  $theta = 1;
  $thetac = 6;  
  $radius = 15;  
  $circles = 10;  
  $points = 35;  
  $lcolor = imagecolorallocate($im, $r, $g, $b);
  for ($i = 0; $i < ($circles * $points) - 1; ++ $i) 
  {
    $theta = $theta + $thetac;
    $rad = $radius * ($i / $points);
    $x = ($rad * cos($theta)) + $origin_x;
    $y = ($rad * sin($theta)) + $origin_y;
    $theta = $theta + $thetac;
    $rad1 = $radius * (($i + 1) / $points);
    $x1 = ($rad1 * cos($theta)) + $origin_x;
    $y1 = ($rad1 * sin($theta)) + $origin_y;
    imageline($im, $x, $y, $x1, $y1, $lcolor);
    $theta = $theta - $thetac;
  }
}

// Load In the Word List
$fp = fopen("Include/dictionary", "r");
while (! feof($fp))
  $text[] = Trim(fgets($fp, 1024));
fclose($fp);

// Pick a Word
$word = "";
while (strlen(Trim($word)) == 0)
{
  $x = rand(0, Count($text));
  $word = $text[$x];
}

$_SESSION['captcha'] = $word;

// Create the Image
$jpg = ImageCreate(280,115);
$bg = ImageColorAllocate($jpg, 255, 255, 255);
$tx = ImageColorAllocate($jpg, 185, 140, 140);
ImageFilledRectangle($jpg, 0, 0, 280, 115, $bg);

$x = rand(0, 200);
$y = rand(0, 115);
spiral($jpg, $x, $y, 225, 190, 190);

$angle = rand(-25, 25);
$size = rand(20, 28);
if ($angle >= 0)
  $y = rand(50, 100);
else 
  $y = rand(25, 50);
$x = rand(10, 150);

imagettftext($jpg, $size, $angle,$x, $y, $tx, "Include/palr46w.ttf", $word);

$x = rand(0, 280);
$y = rand(0, 115);
spiral($jpg, $x, $y, 255,190,190);

/*
$x = rand(0, 280);
$y = rand(0, 115);
spiral($jpg, $x, $y, 255,190,190);
*/


imageline($jpg, 0, 0, 279, 0, $tx);
imageline($jpg, 0, 0, 0, 114, $tx);
imageline($jpg, 0, 114, 280, 114, $tx);
imageline($jpg, 279, 0, 279, 114, $tx);

ImageJpeg($jpg);

?>
