Deploy: 2026-05-23 02:13:41
This commit is contained in:
@@ -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);
|
||||
// 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';
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
$spacedCode = implode(' ', str_split($code));
|
||||
|
||||
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);
|
||||
|
||||
$spacedCode = implode(' ', str_split($code)); // Slightly wider spacing
|
||||
imagestring($tmp, 5, 5, 12, $spacedCode, $tmpText);
|
||||
|
||||
// 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
|
||||
|
||||
// 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);
|
||||
// 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 {
|
||||
|
||||
Reference in New Issue
Block a user