ORIENT/captcha.php

38 lines
934 B
PHP

<?php
session_start();
unset($_SESSION['captcha_code']);
$captcha_code = '';
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$length = 6;
for ($i = 0; $i < $length; $i++) {
$captcha_code .= $characters[rand(0, strlen($characters) - 1)];
}
$_SESSION['captcha_code'] = $captcha_code;
// Create an image
$width = 150;
$height = 50;
$image = imagecreate($width, $height);
$background = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
// Check if TTF font exists
$font_path = realpath(__DIR__ . '/fonts/arial.ttf');
if (file_exists($font_path)) {
// Add text using the TTF font
imagettftext($image, 24, 0, 15, 35, $text_color, $font_path, $captcha_code);
} else {
// Fallback to built-in font
imagestring($image, 5, 30, 15, $captcha_code, $text_color);
}
// Output the image as PNG
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>