<?php
//генерация картинки
$letters = array();
//генерируем ключ
function generate_key(){
global $letters;
$chars = array
(
'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H',
'J', 'K', 'L',
'M', 'N', 'P',
'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X',
'Y', 'Z', '2',
'3', '4', '5', '6',
'7', '8', '9',
);
$count = count($chars) - 1;
$key = array();
for($i = 0; $i < 2; $i++)
{
$key[ $i ] = '';
for($j = 0; $j < 3; $j++)
{
list($usec, $sec) = explode(' ', microtime());
mt_srand((float) $sec + ((float) $usec * 1000000));
$letter = $chars[ mt_rand(0, $count) ];
if ( isset($letters[ $letter ]) )
{
--$j;
}
else
{
$letters[ $letter ] = $letter;
$key[ $i ] .= $letter;
}
}
}
return implode('', $key);
}
$img_x = 150;
$img_y = 50;
$font = 'images/font.ttf';
$content = generate_key();
session_start();
session_register("antibot");
$_SESSION['antibot'] = $content;
$img = imagecreate($img_x, $img_y);
$colours = array();
$colours['white'] = imagecolorallocate($img, 255, 255, 255);
$colours['grey'] = imagecolorallocate($img, 230, 230, 230);
$colours['black'] = imagecolorallocate($img, 0, 0, 0);
$number_of_x_lines = ($img_x - 1) / 1;
$number_of_y_lines = ($img_y - 1) / 1;
for($i = 0; $i < $number_of_x_lines; $i++){imageline($img, $i * $number_of_x_lines, 0, $i * $number_of_x_lines, $img_y, $colours['grey']);}
for($i = 0; $i < $number_of_y_lines; $i++){imageline($img, 0, $i * $number_of_y_lines, $img_x, $i * $number_of_y_lines, $colours['grey']);}
$txt_bbox = imagettfbbox(20, 0, $font, $content);
$sx = ($img_x - ($txt_bbox[2] - $txt_bbox[0])) / 2;
$sy = ($img_y - ($txt_bbox[1] - $txt_bbox[7])) / 2;
$sy -= $txt_bbox[7];
header('Content-type: image/png');
imagettftext($img, 22, 3, $sx, $sy, $colours['black'], $font, $content);
imagepng($img);
imagedestroy($img);
?>