<?php
session_start();

// Function to generate random captcha text
function generateCaptchaText($length = 6) {
    // Exclude confusing characters like O,0,1,l,I
    $characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
    $captchaText = '';
    
    for ($i = 0; $i < $length; $i++) {
        $captchaText .= $characters[rand(0, strlen($characters) - 1)];
    }
    
    return $captchaText;
}

// Generate the captcha text and store in session
$captchaText = generateCaptchaText(4); // Shorter captcha for better readability
$_SESSION['captcha'] = $captchaText;

// Create image
$width = 150;
$height = 50;
$image = imagecreatetruecolor($width, $height);

// Colors
$background = imagecolorallocate($image, 10, 10, 15); // Dark background
$textColor = imagecolorallocate($image, 0, 255, 255); // Cyan text
$lineColor = imagecolorallocate($image, 255, 44, 243); // Pink lines
$dotColor = imagecolorallocate($image, 50, 50, 50); // Dark dots

// Fill background
imagefilledrectangle($image, 0, 0, $width, $height, $background);

// Add random dots
for ($i = 0; $i < 100; $i++) { // Reduced noise
    imagesetpixel($image, rand(0, $width), rand(0, $height), $dotColor);
}

// Add random lines
for ($i = 0; $i < 4; $i++) { // Fewer lines
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor);
}

// Add text - Using a different approach with larger font size
$font = 5; // Built-in font (larger)
$x = 30; // Fixed starting position
$y = 30; // Fixed vertical position

// Add the text with consistent spacing
for ($i = 0; $i < strlen($captchaText); $i++) {
    imagechar($image, $font, $x + ($i * 25), $y, $captchaText[$i], $textColor);
}

// Output the image
header('Content-Type: image/png');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
imagepng($image);
imagedestroy($image);
exit(); // Make sure no other content is sent
?> 