<?php
session_start();

// Crea un'immagine PNG per le linee del captcha
$image_width = 120;
$image_height = 30;
$image = imagecreatetruecolor($image_width, $image_height);
$linesColor = imagecolorallocate($image, 100, 100, 100);

for ($i = 1; $i <= 5; $i++) {
    imagesetthickness($image, rand(1, 2));
    imageline($image, 0, rand(0, $image_height), $image_width, rand(0, $image_height), $linesColor);
}

$captcha = '';
$textColor = imagecolorallocate($image, 0, 0, 0);

for ($x = 15; $x <= 95; $x += 20) {
    $value = rand(0, 9);
    imagechar($image, rand(3, 5), $x, rand(2, 14), $value, $textColor);
    $captcha .= $value;
}

// Invia l'immagine PNG
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

// Salva il captcha generato in una variabile di sessione
$_SESSION["bismartadmin"]["captcha_code"] = $captcha;

// Crea un'immagine JPEG per il codice captcha
$target_layer = imagecreatetruecolor(160, 50);
$captcha_background = imagecolorallocate($target_layer, 224, 236, 255);
imagefill($target_layer, 0, 0, $captcha_background);
$captcha_text_color = imagecolorallocate($target_layer, 0, 0, 0);
imagestring($target_layer, 10, 35, 15, $captcha, $captcha_text_color);

// Invia l'immagine JPEG
header("Content-type: image/jpeg");
imagejpeg($target_layer);
imagedestroy($target_layer);
?>
