game/_incl_data/class/Captcha/Captcha.php

81 lines
2.6 KiB
PHP
Raw Normal View History

2023-08-28 11:23:41 +00:00
<?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;
}
}