76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
class NabihCaptchaService
|
|
{
|
|
/**
|
|
* Generates a unique, distorted Captcha OTP image with noise to bypass Meta template restrictions.
|
|
* Returns the raw PNG binary or base64 encoded data.
|
|
*/
|
|
public static function generateOtpImage(string $code, int $width = 240, int $height = 80): string
|
|
{
|
|
$image = imagecreatetruecolor($width, $height);
|
|
|
|
// 1. Generate randomized subtle background tint
|
|
$bgR = random_int(235, 250);
|
|
$bgG = random_int(245, 255);
|
|
$bgB = random_int(240, 250);
|
|
$bgColor = imagecolorallocate($image, $bgR, $bgG, $bgB);
|
|
imagefilledrectangle($image, 0, 0, $width, $height, $bgColor);
|
|
|
|
// 2. Add random noise dots
|
|
for ($i = 0; $i < 120; $i++) {
|
|
$dotColor = imagecolorallocate(
|
|
$image,
|
|
random_int(120, 200),
|
|
random_int(180, 230),
|
|
random_int(150, 210)
|
|
);
|
|
imagesetpixel($image, random_int(0, $width), random_int(0, $height), $dotColor);
|
|
}
|
|
|
|
// 3. Add random interference crossing lines
|
|
for ($i = 0; $i < 6; $i++) {
|
|
$lineColor = imagecolorallocate(
|
|
$image,
|
|
random_int(80, 160),
|
|
random_int(180, 220),
|
|
random_int(140, 190)
|
|
);
|
|
imagesetthickness($image, random_int(1, 2));
|
|
imageline(
|
|
$image,
|
|
random_int(0, $width),
|
|
random_int(0, $height),
|
|
random_int(0, $width),
|
|
random_int(0, $height),
|
|
$lineColor
|
|
);
|
|
}
|
|
|
|
// 4. Render Digits with randomized spacing and slight position jitter
|
|
$textColor = imagecolorallocate($image, 15, 60, 50); // Deep Teal / Charcoal
|
|
$len = strlen($code);
|
|
$spacing = (int)($width / ($len + 1));
|
|
|
|
for ($idx = 0; $idx < $len; $idx++) {
|
|
$char = $code[$idx];
|
|
$x = ($idx + 1) * $spacing - 10 + random_int(-3, 3);
|
|
$y = (int)($height / 2) - 10 + random_int(-4, 4);
|
|
|
|
// Using standard built-in font for zero dependencies
|
|
imagestring($image, 5, $x, $y, $char, $textColor);
|
|
}
|
|
|
|
ob_start();
|
|
imagepng($image);
|
|
$imageData = ob_get_clean();
|
|
imagedestroy($image);
|
|
|
|
return (string)$imageData;
|
|
}
|
|
}
|