<?php
session_start();

// Generate random 5-character captcha (letters + numbers)
$captcha_code = substr(str_shuffle("ABCDEFGHJKLMNPQRSTUVWXYZ23456789"), 0, 5);
$_SESSION['captcha'] = $captcha_code;

// Create image
$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);

// Colors
$bg_color   = imagecolorallocate($image, 255, 255, 255); // white background
$text_color = imagecolorallocate($image, 0, 0, 0);       // black text
$line_color = imagecolorallocate($image, 200, 200, 200); // light gray lines

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

// Add random lines for noise
for ($i = 0; $i < 6; $i++) {
    imageline($image, 0, rand() % $height, $width, rand() % $height, $line_color);
}

// Add captcha text
$font_size = 5; // built-in font size (1-5)
$x = 15;
$y = 10;
imagestring($image, $font_size, $x, $y, $captcha_code, $text_color);

// Output image
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
