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);
|
imagesetpixel($img, rand(0, 400), rand(0, 200), $lineColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a temporary small image for the text
|
// Get or download a beautiful modern font (Roboto Bold)
|
||||||
$tmp = imagecreatetruecolor(100, 40);
|
$fontDir = __DIR__ . '/../../public/fonts';
|
||||||
$tmpBg = imagecolorallocate($tmp, $palette['bg'][0], $palette['bg'][1], $palette['bg'][2]);
|
if (!is_dir($fontDir)) {
|
||||||
$tmpText = imagecolorallocate($tmp, $palette['text'][0], $palette['text'][1], $palette['text'][2]);
|
mkdir($fontDir, 0755, true);
|
||||||
imagefill($tmp, 0, 0, $tmpBg);
|
}
|
||||||
|
$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
|
$spacedCode = implode(' ', str_split($code));
|
||||||
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
|
if (file_exists($fontPath) && function_exists('imagettftext')) {
|
||||||
imagecopyresampled($img, $tmp, 0, 0, 0, 0, 400, 200, 100, 40);
|
// 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();
|
ob_start();
|
||||||
imagepng($img);
|
imagepng($img);
|
||||||
$imageData = ob_get_clean();
|
$imageData = ob_get_clean();
|
||||||
|
|
||||||
imagedestroy($img);
|
imagedestroy($img);
|
||||||
imagedestroy($tmp);
|
|
||||||
|
|
||||||
return base64_encode($imageData);
|
return base64_encode($imageData);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user