Unified Captcha

This commit is contained in:
2023-08-28 14:23:41 +03:00
parent 294575021a
commit 045803087d
35 changed files with 374 additions and 484 deletions
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace Captcha;
class Captcha
{
private int $width;
private int $height;
private string $sum;
public function width(int $width): Captcha
{
$this->width = max($width, 1);
return $this;
}
public function height(int $height): Captcha
{
$this->height = max($height, 1);
return $this;
}
public function newImage()
{
if ($this->width < 1 || $this->height < 1) {
return;
}
$img = imagecreatetruecolor($this->width, $this->height) or die('Cannot create image'); // создаем картинку
imagefill($img, 0, 0, 0x616161); // заполняем фон картинки
$x = -20;
$i = 1;
$sum = "";
$colorRGB = rand(180, 200); //цвет текста
while ($i++ <= 5000) {
imageSetPixel($img, rand(0, 105), rand(0, 24), 0x515151);
}
//рисуем 2 линии
imageLine($img, rand(0, 10), rand(0, 50), rand(95, 105), rand(0, 26), 0x909090);
imageLine($img, rand(0, 10), rand(0, 50), rand(95, 105), rand(0, 26), 0x909090);
//рамка
imageRectangle($img, 0, 0, 105, 24, 0x343434);
$fonts = [
'fonts/FRSCRIPT.ttf',
'fonts/CHILLER.ttf',
'fonts/Bradley Hand ITC.ttf',
'fonts/de_Manu_2_Regular.ttf',
'fonts/Edgar_da_cool_Regular.ttf',
'fonts/Hurryup_Hurryup.ttf',
'fonts/Fh_Script_Regular.ttf',
'fonts/Gabo4_Gabo4.ttf',
'fonts/JAMI_Regular.ttf',
'fonts/Justy1_Regular.ttf',
];
$font = '../' . $fonts[rand(0, sizeof($fonts) - 1)];
$i = 1;
while ($i++ <= 4) { // выводим одну цифру за один проход цикла
imagettftext($img, 15, 0, $x = $x + 25, 20, imagecolorallocate($img, $colorRGB, $colorRGB, $colorRGB), $font, $rnd = mt_rand(0, 9)); // выводим текст поверх картинки
$sum = $sum . $rnd; // Собираем в одну строку все символы на картинке
}
ob_start();
imagepng($img); // выводим готовую картинку в формате PNG
$imgData = ob_get_clean();
imagedestroy($img); // освобождаем память, выделенную для картинки
echo '<img src="data:image/png;base64,' . base64_encode($imgData) . '" alt="captcha">';
$this->sum = $sum;
}
/**
* @return string
*/
public function getSum(): string
{
return $this->sum;
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+8 -5
View File
@@ -1197,14 +1197,17 @@ class FightRequest
HTML;
if ($r === self::BATTLE_RAZDEL_CHAOTIC) {
if (!$this->u->info['no_zv_key']): ?>
if (!$this->u->info['no_zv_key']):
$security = new Captcha\Captcha();
?>
<div style="float:left;">
<form method="post" style="margin:0px;padding:0px;"
action="/main.php?zayvka=1&r=<?= $r ?>&rnd=<?= $code ?>"><br>
<img src="/show_reg_img/security2.php?id='<?= time() ?>" width="70" height="20">
<form method="post" style="margin:0px;padding:0px;" action="/main.php?zayvka=1&r=<?= $r ?>&rnd=<?= $code ?>"><br>
<?php $security->width(70)->height(20)->newImage();
$_SESSION['code'] = $security->getSum(); ?>
Код подтверждения: <input style="width:40px;" type="text" name="code21">
<input class="btn" type="submit" value="Принять вызов"><br><?= $zvb ?>
<img src="/show_reg_img/security2.php?id=<?= time() ?>" width="70" height="20">
<?php $security->width(70)->height(20)->newImage();
$_SESSION['code'] = $security->getSum(); ?>
Код подтверждения: <input style="width:40px;" type="text" name="code22">
<input class="btn" style="margin-top:1px;" type="submit" value="Принять вызов">
</form>