Файл: adultscript-2.0.3-pro/files/libraries/framework/captcha/image.php
Строк: 180
<?php
defined('_VALID') or die('Restricted Access!');
class VCaptcha_Driver_image extends VCaptcha
{
private $width;
private $height;
private $fonts = array();
private $min_font_size = 18;
private $max_font_size;
private $image;
private $code;
private $spacing;
private $chars = 5;
private $color_max = 127;
private $lines = 150;
private $eclipses = 150;
private $font_pos_min = 0.6;
private $font_pos_max = 0.9;
public function __construct()
{
parent::__construct();
$this->check();
$this->width = $this->cfg['width'];
$this->height = $this->cfg['height'];
$this->setFonts();
$this->setLevel();
$this->setSpacing();
$this->setMaxFontSize();
$this->setCode();
}
private function setFonts()
{
$font_dir = BASE_DIR.DS.'misc'.DS.'fonts';
foreach ($this->cfg['fonts'] as $font) {
$font_file = $font_dir.DS.$font;
if (file_exists($font_file)) {
$this->fonts[] = $font_file;
}
}
if (empty($this->fonts)) {
die('Captcha image driver requires font files!');
}
}
private function getFont()
{
return $this->fonts[rand(0,count($this->fonts)-1)];
}
private function setLevel()
{
switch ($this->cfg['level']) {
case 'easy':
$this->color_max = 127;
$this->chars = 5;
$this->lines = 150;
$this->eclipses = 150;
$this->font_pos_min = 0.6;
$this->font_pos_max = 0.9;
break;
case 'medium':
$this->color_max = 160;
$this->chars = 6;
$this->lines = 150;
$this->eclipses = 150;
$this->font_pos_min = 0.5;
$this->font_pos_max = 0.9;
break;
case 'hard':
$this->color_max = 200;
$this->chars = 7;
$this->lines = 150;
$this->eclipses = 150;
$this->font_pos_min = 0.3;
$this->font_pos_max = 0.7;
break;
}
}
private function setSpacing()
{
$this->spacing = (int)($this->width / $this->chars);
}
private function setMaxFontSize() {
$this->max_font_size = round($this->width / $this->chars);
}
private function setCode()
{
switch ($this->cfg['words']) {
case 'language':
break;
case 'random':
default:
$chars = array();
for ($i=0; $i<$this->chars; $i++) {
$chars['1'] = rand(97,122);
$chars['2'] = rand(65,90);
$this->code .= chr($chars[rand(1,2)]);
}
}
$_SESSION['captcha_code'] = strtoupper($this->code);
}
private function drawBackground() {
$color = imagecolorallocate($this->image, 48, 63, 68);
imagefilltoborder($this->image, 0, 0, $color, $color);
}
private function drawEclipses() {
for ($i=1; $i<=$this->eclipses; $i++) {
$r = round(rand(0, $this->color_max));
$g = round(rand(0, $this->color_max));
$b = round(rand(0, $this->color_max));
$color = imagecolorallocate($this->image, $r, $g, $b);
imagefilledellipse( $this->image,round(rand(0,$this->width)), round(rand(0,$this->height)), round(rand(0,$this->width/16)), round(rand(0,$this->height/4)), $color );
}
}
private function drawLines() {
for ($i=1; $i<=$this->lines; $i++) {
$r = round(rand(0, $this->color_max));
$g = round(rand(0, $this->color_max));
$b = round(rand(0, $this->color_max));
$color = imagecolorallocate($this->image, $r, $g, $b);
imageline($this->image, rand(0, $this->width), rand(0, $this->height), rand(0, $this->width), rand(0, $this->height), $color);
}
}
private function drawCharacters() {
for ($i=0; $i<$this->chars; $i++) {
$r = round(rand(127, 255));
$g = round(rand(127, 255));
$b = round(rand(127, 255));
$x_pos = round($this->max_font_size*$this->font_pos_min) + $i * round($this->max_font_size*$this->font_pos_max);
$y_pos = ($this->height / 2) + round(rand(5, 20));
$fontsize = round(rand($this->min_font_size, $this->max_font_size));
$color = imagecolorallocate( $this->image, $r, $g, $b);
$presign = round(rand(0, 1));
$angle = round(rand(0, 25));
if( $presign == true ) {
$angle = -1*$angle;
}
imagettftext($this->image, $fontsize, $angle, $x_pos, $y_pos, $color, $this->getFont(), substr($this->code,$i,1));
}
}
public function generate()
{
$this->image = imagecreatetruecolor($this->width, $this->height);
$this->drawBackground();
imagealphablending($this->image, 1);
imagecolortransparent($this->image);
$this->drawLines();
$this->drawEclipses();
$this->drawCharacters();
header('Content-type: image/jpeg');
imagejpeg($this->image);
imagedestroy($this->image);
exit;
}
public function compare($code)
{
return $_SESSION['captcha_code'] == $code;
}
private function check()
{
if (!extension_loaded('gd')) {
throw new Exception('Captcha image driver requires GD extension!');
}
if (!function_exists('imagepng')) {
throw new Exception('Captcha image driver requires PNG support!');
}
if (!function_exists('imagejpeg')) {
throw new Exception('Captcha image driver required JPEG support in GD!');
}
if (!function_exists('imageftbbox')) {
throw new Exception('Captcha image driver requires FT fonts support!');
}
}
}
?>