Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement