<?php
session_start();

// Generate a random string
//$captcha_text = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0, 4);
$captcha_text = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0, 4);
$_SESSION['captcha'] = $captcha_text;

// Create an image
$width = 120;
$height = 40;
$image = imagecreate($width, $height);

// Colors
$bg_color = imagecolorallocate($image, 255, 255, 255); // White
$text_color = imagecolorallocate($image, 0, 0, 0); // Black
$line_color = imagecolorallocate($image, 100, 100, 100); // Gray

// Add noise
for ($i = 0; $i < 5; $i++) {
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color);
}

// Add text
imagestring($image, 5, 30, 10, $captcha_text, $text_color);

// Output the image
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
