ptest/src/M/CAPTCHAModel.php
2017-01-26 06:45:35 +03:00

79 строки
2.1 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Игорь
* Date: 24.01.2017
* Time: 00:37
*/
namespace PTEST\M;
class CAPTCHAModel {
/**
* Проверка даных капчи
*
* @param string $code
*
* @return bool|string
*/
public function verify( $code ) {
return strtoupper( $code ) == $_SESSION['CAPTCHA'] ? true : "Неверный код с картинки";
}
/**
* Генерирование изображения
*
* @return string
*/
public function getImage() {
global $app;
$letters = 'ABDEFABDEFGKLMNPRSTWXYZ482GKLMNABDEFGKLMNPRSTWXYZ482PRSTWXYZ482'; // алфавит
$length = 6; //длина текста
$height = 60*2;
$fonts_arr = [
$app->config->public_dir . 'font/PTF55F.ttf',
$app->config->public_dir . 'font/PTF56F.ttf',
$app->config->public_dir . 'font/PTF75F.ttf',
$app->config->public_dir . 'font/PTF76F.ttf',
$app->config->public_dir . 'font/PTZ55F.ttf',
$app->config->public_dir . 'font/PTZ56F.ttf',
];
$fontsize_base = 36;// размер текста
$image = imagecreatefrompng($app->config->public_dir . 'image/CAPTCHA_' . rand(0,4) .'.png');
$captcha = '';
for ($i = 0; $i < $length; $i++)
{
$fontsize = rand($fontsize_base - 2, $fontsize_base + 2) ;
$captcha .= $letters[ rand(0, strlen($letters)-1) ];
$x = ($i + 1) * ($fontsize + 5 );
$x = rand($x, $x+6);
$y = $height - ( ($height - $fontsize) / 2 ) + rand(-8,16);
$curcolor = imagecolorallocate( $image, rand(50, 127), rand(0, 127), rand(0, 127) );
//шум
imageellipse($image, rand($x - 32 ,$x + 32), $y, rand(16,32), rand(16,52), $curcolor);
imageellipse($image, $x, rand($y - 16 ,$y + 16), rand(16,52), rand(16,32), $curcolor);
$angle = rand(-25, 25);
imagettftext($image, $fontsize, $angle, $x, $y, $curcolor, $fonts_arr[rand(0,5)], $captcha[$i]);
}
ob_start();
imagepng($image);
$image_data = ob_get_contents();
ob_end_clean();
$_SESSION['CAPTCHA'] = $captcha;
return $image_data;
}
}