Voici le code d'un fichier anti-spam.php qui crée une image captcha:
session_start();
if ((!isset($_SESSION['rand_code'])) || ($_SESSION['rand_code']=='')) {
$str = "";
$length = 0;
for ($i = 0; $i < 4; $i++) {
// this numbers refer to numbers of the ascii table (small-caps)
$str .= chr(rand(97, 122));
}
$_SESSION['rand_code'] = $str;
}
$imgX = 100;
$imgY = 40;
$image = imagecreatetruecolor(100, 40);
$backgr_col = imagecolorallocate($image, 238,239,239);
$border_col = imagecolorallocate($image, 208,208,208);
$text_col = imagecolorallocate($image, 46,60,31);
$trait_col = imagecolorallocate($image, 46,60,31);
imagefilledrectangle($image, 0, 0, 100, 40, $backgr_col);
imagerectangle($image, 0, 0, 99, 39, $border_col);
imageline($image, 0, 0, 100, 40, $trait_col);
imageellipse($image, 50, 20, 75, 30, $trait_col);
$font = "ARIAL.TTF";
$font_size = 15;
$angle = 10;
$box = imagettfbbox($font_size, $angle, $font, $_SESSION['rand_code']);
$x = (int)($imgX - $box[4]) / 2;
$y = (int)($imgY - $box[5]) / 2;
imagettftext($image, $font_size, $angle, $x, $y, $text_col, $font, $_SESSION['rand_code']);
header("Content-type: image/png");
imagepng($image);
imagedestroy ($image);
dans ton code de formulaire tu l'appelles comme une image: <img src="anti-spam.php" />
la valeur du code captcha se trouve dans la variable de session: $_SESSION['rand_code']
le code créé est une suite de 4 lettres, il est modifiable a souhait et fonctionne avec la bibliothèque GD de création d'image.
@+