Bonjour,

Je vous présente l'un de mes codes sources en PHP qui affiche un captcha aléatoire assez efficace pour les petits sites web.

Voici le code source :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
 
// --- 1. On démarre les sessions
session_start();
 
// --- 2. On créé le captcha aux dimensions 100x35
$captcha = imagecreatetruecolor(100, 35);
 
// --- 3. On sélectionne une image dont on prendra une partie pour l'arrière-plan du captcha
$fond = imagecreatefromjpeg("fond.jpg");
 
// On découpe un rectangle de dimensions 100x35 quelque part au hasard dans l'image choisie
$ix = mt_rand(0,imagesx($fond)-100);
$iy = mt_rand(0,imagesy($fond)-35);
 
// On place le rectangle pixel par pixel sur le captcha (on aura ainsi l'arrière-plan)
for($i=$ix;$i<100+$ix;$i++)
{
  for($y=$iy;$y<35+$iy;$y++)
  {
    $couleur_pixel = imagecolorat($fond,$i,$y);
    $rouge = ($couleur_pixel>>16)&0xFF;
    $vert = ($couleur_pixel>>8)&0xFF;
    $bleu = $couleur_pixel&0xFF;
 
    imagesetpixel($captcha,$i-$ix,$y-$iy,imagecolorallocate($captcha,$rouge,$vert,$bleu));
  }
}
 
// On n'a plus besoin de l'image qui a servi pour le fond, on la supprime
imagedestroy($fond);
 
// --- 4. On créé le texte du captcha au hasard
$texte = random_int(1000,999);
 
// Si votre version de PHP ne supporte pas la fonction random_int, utilisez :
// mt_rand(1000,9999);
 
// Notez que la fonction random_int() est préférable car elle génère du "hasard" cryptographique :
// http://php.net/manual/fr/function.random-int.php
 
// On enregistre le texte dans la session
$_SESSION["texte_captcha"] = $texte;
 
// --- 5. On place le texte sur notre captcha avec des couleurs aléatoires
for($i=0;$i<4;$i++)
{
  $couleur = imagecolorallocate($captcha, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  imagettftext($captcha, mt_rand(20,25), mt_rand(0,20)-10, 5+$i*22, 30, $couleur, "font.ttf", $texte[$i]);
}
 
// --- 6. Et on applique des filtres sur l'image au hasard
if(mt_rand(0,5)==2) imagefilter($captcha, IMG_FILTER_NEGATE);
if(mt_rand(0,5)==2) imagefilter($captcha, IMG_FILTER_EMBOSS);
if(mt_rand(0,5)==2) imagefilter($captcha, IMG_FILTER_EDGEDETECT);
 
 
// On envoie l'image au navigateur
header('Content-type: image/png');
imagepng($captcha);
 
// On libère les ressources
imagedestroy($captcha);
 
?>
http://pastebin.com/AZWmm9vG




Voici l'image de fond (fond.jpg) que j'ai utilisé pour les exemples qui vont suivre :

http://image.noelshack.com/fichiers/...ptcha-fond.jpg

Voici quelques exemples du rendu du code avec une police de font banale (font.ttf) :

http://image.noelshack.com/fichiers/...80895-cap2.png
http://image.noelshack.com/fichiers/...80896-cap4.png
http://image.noelshack.com/fichiers/...80898-cap4.png

http://image.noelshack.com/fichiers/...80899-cap2.png
http://image.noelshack.com/fichiers/...80899-cap3.png
http://image.noelshack.com/fichiers/...80933-cap2.png

http://image.noelshack.com/fichiers/...80932-cap4.png
http://image.noelshack.com/fichiers/...80932-cap2.png



N'hésitez pas à donner vos avis/remarques/conseils/commentaires constructifs, merci.