<?php
session_start();

// Get the CAPTCHA code from session
$code = $_SESSION['captcha_code'] ?? 'ERROR';

// Create image
$width = 200;
$height = 60;
$image = imagecreate($width, $height);

// Colors
$bgColor = imagecolorallocate($image, 240, 240, 240);
$textColor = imagecolorallocate($image, 0, 0, 0);
$lineColor = imagecolorallocate($image, 200, 200, 200);

// Add some noise lines
for ($i = 0; $i < 5; $i++) {
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor);
}

// Add the text
$fontSize = 5;
$textWidth = imagefontwidth($fontSize) * strlen($code);
$textHeight = imagefontheight($fontSize);
$x = ($width - $textWidth) / 2;
$y = ($height - $textHeight) / 2;

imagestring($image, $fontSize, $x, $y, $code, $textColor);

// Output the image
header('Content-Type: image/png');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Expires: 0');
imagepng($image);
imagedestroy($image);
?>