From c1cf5a1ae96a24b69b6116aceefa6321e10505fc Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Wed, 28 Jun 2023 17:24:41 +0600 Subject: [PATCH] This change introduces ImageLines, an interface for adding randomized lines to captcha images for enhanced security. The interface has been implemented in the Lines class. The goal is to randomize lines on captcha images to prevent bot reads. --- app/Captcha/Contracts/ImageLines.php | 8 +++++++ app/Captcha/Images/Lines.php | 36 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 app/Captcha/Contracts/ImageLines.php create mode 100644 app/Captcha/Images/Lines.php diff --git a/app/Captcha/Contracts/ImageLines.php b/app/Captcha/Contracts/ImageLines.php new file mode 100644 index 0000000..5657006 --- /dev/null +++ b/app/Captcha/Contracts/ImageLines.php @@ -0,0 +1,8 @@ +getWidth(); + $imageHeight = $image->getHeight(); + for ($i = 0; $i <= $lines; $i++) { + $image->addLine( + x1: mt_rand(0, $imageWidth) + $i * mt_rand(0, $imageHeight), + y1: mt_rand(0, $imageHeight), + x2: mt_rand(0, $imageWidth), + y2: mt_rand(0, $imageHeight), + hexColor: $this->lineColors($colors) + ); + } + + return $image; + } + + private function lineColors(array $colors): string + { + if (!empty($colors)) { + return Arr::random($colors); + } + + return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT); + } +}