<?php
class captcha {
/**
* @author Mustafayev Mirza
* @version 1.1
*
* Пример использования
*
* $captcha = new capthca();
* $captcha->makeimg();
*
* несколько шрифтов
*
* $captcha = new capthca( $font = array('font.ttf','font1.ttf','font2.ttf','font3.ttf','font4.ttf'));
* $captcha->makeimg();
*/
public $length = 5; // длина строки (int)
public $width = 204; // ширина изображения (int)
public $height = 60; // высота изображения (int)
public $size = 26; // размер шрифта (int)
private $font = 'font.ttf'; // ttf шрифт
private $alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
private $allowed_symbols = "23456789abcdeghkmnpqsuvxyz";
private $keystring = array();
function __construct( $font = false )
{
if(!isset($_SESSION))
session_start();
if( $font != false && is_file($file) )
{
if(is_array($font))
{
$rand_keys = array_rand( $font, 1);
$this->font = $rand_keys[0];
}
else
{
$this->font = $font;
}
}
}
function makeimg() {
$image = '';
$image = imagecreatetruecolor ($this->width, $this->height);
$bg = imagecolorallocate($image, rand(200,255), rand(200, 255), rand(200, 255));
$fg = imagecolorallocate($image, rand(0,200), rand(0, 200), rand(0, 200));
imagefill($image, 0, 0, $bg);
$alphabet_length = strlen($this->alphabet);
for($i = 0; $i < $this->length; $i++)
{
$this->keystring[$i] = $this->allowed_symbols{mt_rand(0,strlen($this->allowed_symbols)-1)};
ImageTTFText($image, $this->size, rand(-29, 29), (($this->width/$this->length)+$this->size*$i), ($this->height*1.2 - $this->size), imagecolorallocate($image, rand(0,111), rand(0, 111), rand(0, 111)), $this->font, $this->keystring[$i]);
}
$dc = ImageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
ImageArc($image, rand(0, $this->width ), rand(0, $this->height ), rand($this->width / 2, $this->width), rand($this->height / 2, $this->height), 0, 360, $dc);
$dc = ImageColorAllocate($image, rand(0,255), rand(0, 255), rand(0, 255));
ImageArc($image, rand(0, $this->width ), rand(0, $this->height ), rand($this->width / 3, $this->width), rand($this->height / 2, $this->height), 0, 360, $dc);
$dots = $this->width * $this->height / 10;
for ($i=0; $i < $dots; $i++) {
$dc = ImageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
ImageSetPixel($image, rand(0, $this->width), rand(0, $this->height), $dc);
}
header("Content-Type: image/png");
ImagePNG($image);
$_SESSION['captcha'] = join("", $this->keystring);
}
}
?>