<?php
header('Content-Type: text/html; charset=utf-8');
class captcha {
private static $options = array(
'language' => 'eng',
'dictionary' => false,
'length' => 3,
'lines' => true,
'bitmap' => array(
'width' => 100,
'height' => 45,
'r' => 233,
'g' => 233,
'b' => 233,
),
'size' => 16,
);
private static $bitmap = NULL;
public static function setOption($option, $value = false) {
if (is_array($option)) {
self::$options = $option;
return 1;
}
self::$options[$option] = $value;
}
public static function setConfig($options) {
self::$options = $options;
}
public static function generate() {
self::createBitMap();
if (self::$options['lines']) self::createLines();
$string_captcha = self::createString();
$x = rand(0, 35);
for($i = 0; $i < strlen($string_captcha); $i++) {
$x+=25;
$letter=substr($string_captcha, $i, 1);
$red = rand(100,255);
$green = rand(100,255);
$blue = rand(100,255);
$angle = rand(0,15);
$color = imagecolorallocate(self::$bitmap,255-$red,255-$green,255-$blue);
imagettftext (self::$bitmap, self::$options['size'], rand(0, 25), $x, rand(50, 55), $color, 'fonts/times.ttf', $letter);
}
imagejpeg(self::$bitmap);
}
private static function createBitMap() {
$bitmap = imagecreatetruecolor (self::$options['bitmap']['width'], self::$options['bitmap']['height']);
$color_bitmap = imagecolorallocate($bitmap, self::$options['bitmap']['r'], self::$options['bitmap']['g'], self::$options['bitmap']['b']);
imagefill($bitmap, 0, 0, $color_bitmap);
self::$bitmap = $bitmap;
}
private static function createLines() {
$clinea = imagecolorallocate(self::$bitmap,63,63,63);
imageline(self::$bitmap,rand(0,self::$options['bitmap']['width']-40),0,rand(0,self::$options['bitmap']['width']-30),self::$options['bitmap']['height'],$clinea);
imageline(self::$bitmap,rand(0,self::$options['bitmap']['width']-50),0,rand(0,self::$options['bitmap']['width']-50),self::$options['bitmap']['height'],$clinea);
imageline(self::$bitmap,rand(0,self::$options['bitmap']['width']-50),0,rand(0,self::$options['bitmap']['width']-70),self::$options['bitmap']['height'],$clinea);
}
private static function createString() {
$length = self::$options['length'];
$language = self::$options['language'];
if ($language == 'rus') {
$chars = array('й', 'ц', 'у', 'к', 'е', 'н', 'г', 'ш', 'щ', 'з', 'х', 'ъ', 'ф', 'ы', 'в', 'а', 'п', 'р', 'о', 'л', 'д', 'ж', 'э', 'я', 'ч', 'с', 'м', 'и', 'т', 'ь', 'б', 'ю',
1,2,3,4,5,6,7,8,9,0);
}
else {
$chars = array('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm',
1,2,3,4,5,6,7,8,9,0);
}
$useDic = rand(0, 1);
if (self::$options['dictionary'] && $useDic) {
$words = file('dic.txt');
$word = $words[rand(0, count($words))];
$_SESSION['captcha'] = $word;
return $word;
}
$chars_length = (count($chars) - 1);
$string = array();
$string[] = $chars[rand(0, $chars_length)];
for ($i = 1; $i < $length; $i++) {
$r = $chars[rand(0, $chars_length)];
if ($r != $string[$i - 1]) $string[] = $r;
}
$s = '';
foreach ($string as $letter) {
$s .= $letter;
}
$_SESSION['captcha'] = $s;
return $s;
}
}
?>