Update OTP image generation to 3 digits, no caption, random labels and fonts, big numbers

This commit is contained in:
Hamza-Ayed
2026-05-23 18:15:51 +03:00
parent 30de96d481
commit 58e8128b65
2 changed files with 52 additions and 20 deletions

View File

@@ -115,34 +115,66 @@ class WhatsAppClient {
// Colors
$bgColor = imagecolorallocate($im, 240, 244, 248); // Soft grey-blue
$textColor = imagecolorallocate($im, 33, 37, 41); // Dark charcoal
$accentColor = imagecolorallocate($im, 13, 110, 253); // Premium blue
$noiseColor = imagecolorallocate($im, 200, 210, 220); // Light noise
// Fill background
imagefill($im, 0, 0, $bgColor);
// Draw some obfuscation lines / background noise
// --- 1. Draw Big OTP Text by Scaling ---
// Create a small image for the OTP
$otpWidth = 45; // 3 chars * 15px width roughly
$otpHeight = 20;
$otpIm = imagecreatetruecolor($otpWidth, $otpHeight);
$otpBg = imagecolorallocate($otpIm, 240, 244, 248);
$otpFg = imagecolorallocate($otpIm, 13, 110, 253);
imagefill($otpIm, 0, 0, $otpBg);
$chars = str_split($otp);
$x = 2;
foreach ($chars as $char) {
$y = random_int(0, 5); // Slight vertical jitter
imagestring($otpIm, 5, $x, $y, $char, $otpFg);
$x += 14; // Font 5 width is approx 9px, leaving some space
}
// Scale it up by 3x onto the main image
$scale = 3;
$dstWidth = $otpWidth * $scale;
$dstHeight = $otpHeight * $scale;
// Place it randomly in the bottom right-ish area
$dstX = random_int(80, 150);
$dstY = random_int(30, 40);
imagecopyresampled($im, $otpIm, $dstX, $dstY, 0, 0, $dstWidth, $dstHeight, $otpWidth, $otpHeight);
imagedestroy($otpIm);
// --- 2. Add Background Noise (Lines & Dots) ---
// Drawing noise *after* the OTP helps to obstruct it slightly from OCR
for ($i = 0; $i < 6; $i++) {
imageline($im, random_int(0, 300), random_int(0, 100), random_int(0, 300), random_int(0, 100), $noiseColor);
}
// Draw some random dots
for ($i = 0; $i < 100; $i++) {
imagesetpixel($im, random_int(0, 300), random_int(0, 100), $noiseColor);
}
// Header text (smaller)
imagestring($im, 3, 20, 15, "Verification Code:", $textColor);
// --- 3. Draw Random Header Label with Variable Font ---
$labels = [
'Verification Code:',
'Your OTP:',
'Security Key:',
'Access Number:',
'Auth Code:',
'Login Pin:',
'Secret Key:',
'Your Number:',
'One Time Pass:',
'Code:'
];
$label = $labels[array_rand($labels)];
$labelFont = random_int(3, 5); // Random built-in font (3, 4, or 5)
// Large OTP text (using larger font index 5 or custom size if possible)
// Split OTP and draw characters with varying Y positions and styling to make OCR harder
$chars = str_split($otp);
$x = 90;
foreach ($chars as $char) {
$y = random_int(35, 45);
imagestring($im, 5, $x, $y, $char, $accentColor);
$x += 30;
}
imagestring($im, $labelFont, 20, 10, $label, $textColor);
// Draw a bounding border
imagerectangle($im, 0, 0, 299, 99, $noiseColor);