From bc52cdbb24f8549f26f7e787ce9ab81378d24b3a Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sat, 23 May 2026 02:13:41 +0300 Subject: [PATCH] Deploy: 2026-05-23 02:13:41 --- backend/app/Controllers/OTPController.php | 68 ++++++++++++++++++----- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/backend/app/Controllers/OTPController.php b/backend/app/Controllers/OTPController.php index c2fefd3..0d2e938 100644 --- a/backend/app/Controllers/OTPController.php +++ b/backend/app/Controllers/OTPController.php @@ -49,29 +49,69 @@ class OTPController extends BaseController imagesetpixel($img, rand(0, 400), rand(0, 200), $lineColor); } - // Create a temporary small image for the text - $tmp = imagecreatetruecolor(100, 40); - $tmpBg = imagecolorallocate($tmp, $palette['bg'][0], $palette['bg'][1], $palette['bg'][2]); - $tmpText = imagecolorallocate($tmp, $palette['text'][0], $palette['text'][1], $palette['text'][2]); - imagefill($tmp, 0, 0, $tmpBg); + // Get or download a beautiful modern font (Roboto Bold) + $fontDir = __DIR__ . '/../../public/fonts'; + if (!is_dir($fontDir)) { + mkdir($fontDir, 0755, true); + } + $fontPath = $fontDir . '/Roboto-Bold.ttf'; - $spacedCode = implode(' ', str_split($code)); // Slightly wider spacing + if (!file_exists($fontPath)) { + $options = [ + 'http' => [ + 'method' => 'GET', + 'header' => 'User-Agent: PHP' + ] + ]; + $context = stream_context_create($options); + $fontData = @file_get_contents('https://github.com/google/fonts/raw/main/apache/roboto/Roboto-Bold.ttf', false, $context); + if ($fontData) { + file_put_contents($fontPath, $fontData); + } + } - // Draw text multiple times with 1px offsets to create a bold effect - imagestring($tmp, 5, 8, 12, $spacedCode, $tmpText); - imagestring($tmp, 5, 9, 12, $spacedCode, $tmpText); // Bold X - imagestring($tmp, 5, 8, 13, $spacedCode, $tmpText); // Bold Y - imagestring($tmp, 5, 9, 13, $spacedCode, $tmpText); // Bold XY + $spacedCode = implode(' ', str_split($code)); - // Scale up the text image to the main image using resampled for smooth (non-pixelated) edges - imagecopyresampled($img, $tmp, 0, 0, 0, 0, 400, 200, 100, 40); + if (file_exists($fontPath) && function_exists('imagettftext')) { + // Calculate exact text dimensions to center it perfectly + $fontSize = 48; // Large smooth font + $bbox = imagettfbbox($fontSize, 0, $fontPath, $spacedCode); + $textWidth = abs($bbox[2] - $bbox[0]); + $textHeight = abs($bbox[1] - $bbox[7]); + + // If code is too long, shrink font + if ($textWidth > 360) { + $fontSize = 36; + $bbox = imagettfbbox($fontSize, 0, $fontPath, $spacedCode); + $textWidth = abs($bbox[2] - $bbox[0]); + $textHeight = abs($bbox[1] - $bbox[7]); + } + + // Center calculations + $x = (400 - $textWidth) / 2; + $y = ((200 - $textHeight) / 2) + $textHeight; // Y is the baseline of the text + + // Draw beautifully smooth true type text + imagettftext($img, $fontSize, 0, (int)$x, (int)$y, $textColor, $fontPath, $spacedCode); + } else { + // Fallback for missing TTF + $tmp = imagecreatetruecolor(200, 40); + $tmpBg = imagecolorallocate($tmp, $palette['bg'][0], $palette['bg'][1], $palette['bg'][2]); + $tmpText = imagecolorallocate($tmp, $palette['text'][0], $palette['text'][1], $palette['text'][2]); + imagefill($tmp, 0, 0, $tmpBg); + + imagestring($tmp, 5, 5, 12, $spacedCode, $tmpText); + + // Scale up dynamically + imagecopyresampled($img, $tmp, 0, 60, 0, 0, 400, 80, 200, 40); + imagedestroy($tmp); + } ob_start(); imagepng($img); $imageData = ob_get_clean(); imagedestroy($img); - imagedestroy($tmp); return base64_encode($imageData); } else {