<?php
/*  Image captcha script by FiSh of 13337.org. Feel free to re-distribute and modify freely as long as copyright remains intact  */

$numlines 20// number of random lines
$numletters 6// number of letters to display
$font 'font.ttf'// font (must be in current directory)
$fontsize 20// size of display font
$image_height 100// captcha image height
$image_width 250// captcha image width
$image imagecreatetruecolor($image_width$image_height); // create the image
$bg_c imagecolorallocate($image255255255); // create the background color
$text_c imagecolorallocate($image127127127); // create the text color
$shadow_c imagecolorallocate($image000); // create the text shadow color

// Uncomment this line if you want a random color for your text:
// $text_c = randcolor($image);

/* random color generation */
function randcolor($image){ 
  
$color imagecolorallocate($imagemt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  return 
$color;
}

if(
$_GET['r']){
  
header('Content-type: image/png'); // send the browser a header for an image
  // string to select text from with hard to read characters removed
  
$rand 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
  for(
$i=0;$i<$numletters;$i++){
    
$x mt_rand(0strlen($rand)-1); // generate a random letter
    
$ver .= substr($rand$x1); // add to the string
  
}

  
imagefilledrectangle($image00$image_width$image_height$bg_c); // create the captcha box

  
$tilt mt_rand(-10,10); // random tilt for the text
  
$x_coor mt_rand($image_width 10$image_width 2); // random x coordinate for the text
  
$y_coor mt_rand($image_height 3$image_height 1.5); // random y coordinate for the text
  
  
imagettftext($image$fontsize$tilt$x_coor+1$y_coor+1$shadow_c$font$ver); // create the shadow text
  
  /*  horizontal gridlines  */
  
for($i=0;$i<$image_height;$i+=9){
    
imageline($image0$i$image_width$i$text_c); 
  }
  
  
/*  vertical gridlines  */
  
for($i=0;$i<$image_width;$i+=9){
    
imageline($image$i0$i$image_height$text_c); 
  }
  
imagettftext($image$fontsize$tilt$x_coor$y_coor$text_c$font$ver); // print the text
  
  /* random colored lines */
  
for($i=0;$i<$numlines;$i++){
    
$x1 mt_rand(0,$image_width);  // random coordinates for each line
    
$y1 mt_rand(0,$image_height);
    
$x2 mt_rand(0,$image_width);
    
$y2 mt_rand(0,$image_height);
    
imageline($image$x1$y1$x2$y2randcolor($image));
  }
  
  
imagepng($image);
  
imagedestroy($image);
}

function 
display_captcha(){ // output the captcha
  
global $image_width$image_height;
  echo(
"<img src='" $_SERVER['PHP_SELF'] . "?r=1' width='" $image_width "' height='" $image_height "' alt='Image captcha' title='Image captcha' name='captcha'>");
}
?>