Captcha - Générateur d'images

  Information

Générateur automatique d'image pour Captchas en PHP. Le texte du captcha est stocké dans une variable de session, il est donc totalement inaccessible côté client.

N'oubliez pas de mettre un fichier font.ttf au meme niveau que le script.

  code source classé dans  Formulaires

 
 01    
 02    
 03    
 04    
 05    
 06    
 07    
 08    
 09    
 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    
 66    
 67    
 68    
 69    
 70    
 71    
 72    
 73    
 74    
 75    
 76    
 77    
 78    
 79    
 80    
 81    
 82    
 83    
 84    
 85    
 86    
 87    
 88    
 89    
 90    
 91    
 92    
 93    
 94    
 95    
 96    
 97    
 98    
 99    
 100    
 101    
 102    
 103    
 104    
 105    
 106    
 107    
 108    
                               
<?php
/*---------------------------------------------------------------*/
/*
    Titre : Captcha - Générateur d'images                                                                               
                                                                                                                          
    URL   : https://phpsources.net/code_s.php?id=633
    Auteur           : developpeurweb                                                                                     
    Website auteur   : http://rodic.fr                                                                                    
    Date édition     : 15 Avril 2011                                                                                      
    Date mise à jour : 19 Sept 2019                                                                                      
    Rapport de la maj:                                                                                                    
    - fonctionnement du code vérifié                                                                                    
*/
/*---------------------------------------------------------------*/

    //    Paramètres
    $width  1280;
    $height    360;
    $scale  1/4;
    $bg_color array(0,0,0);
    $fg_color array(200,0,0);
    
    
    
    //    Initialisation de l'image
    $img  = @imagecreatetruecolor($width,$height);
    $font dirname(__FILE__)."/font.ttf";
    $img_bg_color imagecolorallocate($img$bg_color[0], $bg_color[1], 
$bg_color[2]);
    $img_fg_color imagecolorallocate($img$fg_color[0], $fg_color[1], 
$fg_color[2]);
    
    
    //    Génération du texte, 
    function random_word($n){
        $charsets array("aeiouy""bcdfghjklmnpqrstvwxz");        
        $word "";
        $i0 rand(0,1);
        for ($i=$i0 $i<$i0+$n $i++){
            $charset $charsets[$i%2];
            $letter $charset{rand(0,strlen($charset)-1)};
            $word.= $letter;
        }
        return ucfirst($word);
    }
    function random_sentence(){
        $n1 rand(3,6);
        $n2 $n1;
        return random_word($n1)."  ".random_word($n2);
    }
    $sentence random_sentence();
    imagefill($img00$img_bg_color);
    imagettftext($img1200128320$img_fg_color$font$sentence);
    
    
    //    Ajout d'une ligne horizontale
    $y rand(238,311);
    imagefilledrectangle($img0$y$width$y+6$img_fg_color);
    
    
    //    Déformation de l'image
    function random_phase(){
        return M_PI rand() / getrandmax();
    }
    $phase1 random_phase();
    $phase2 random_phase();
    function y($x){
        global $phase1$phase2$y;
        return 96 * (sin($x/M_PI/32 $phase1)) * (sin($y/M_PI/17  
$phase2));
    }
    function x($y){
        global $phase1$phase2;
        return 32 * (sin($y/M_PI/32 $phase2)) * (sin($y/M_PI/97  
$phase1));
    }
    for($x=$x<$width $x+=1){
        $y y($x);
        imagecopy($img$img$x0$x$y1$height);
        imagefilledrectangle($img$x$height-$y$x$height$img_bg_color);
    }
    for($y=$y<$width $y+=1){
        $x x($y);
        imagecopy($img$img0$y$x$y$width1);
        imagefilledrectangle($img$width-$x$y$width$x$img_bg_color);
    }
    
    
    //    Redimensionnement de l'image
    $img_resized  = @imagecreatetruecolor($width*$scale,$height*$scale);
    imagecopyresampled($img_resized,$img0,0,0,0$width*$scale,$height*$scale,
 $width,$height);
    imagedestroy($img);
    
    
    //    Le texte sera stocké dans une variable de session pendant 1 minute
    session_cache_expire(1);
    session_start();
    $_SESSION["captcha"] = $sentence;
    
    
    //    Envoi de l'image redimensionnée
    Header("Content-Type: image/png");
    imagepng($img_resizednull9);
    imagedestroy($img_resized);    
?>

          Fonctions du code - Doc officielle PHP

   php.net   Description Versions PHP OUTIL
   array Crée un tableau PHP 4, PHP 5, PHP 7, PHP 8
   dirname Renvoie le nom du dossier PHP 4, PHP 5, PHP 7, PHP 8
   getrandmax Plus grande valeur aléatoire possible PHP 4, PHP 5, PHP 7, PHP 8
   header Envoie un en-tête HTTP PHP 4, PHP 5, PHP 7, PHP 8
   imagecolorallocate Alloue une couleur pour une image PHP 4, PHP 5, PHP 7, PHP 8
   imagecopy Copie une partie d'une image PHP 4, PHP 5, PHP 7, PHP 8
   imagecopyresampled Copie, redimensionne, rééchantillonne une image - (PHP 4 >= 4.0.6, PHP 5, PHP 7) PHP 4, PHP 5, PHP 7, PHP 8
   imagecreatetruecolor Crée une nouvelle image en couleurs vraies - (PHP 4 >= 4.0.6, PHP 5, PHP 7) PHP 4, PHP 5, PHP 7, PHP 8
   imagedestroy Détruit une image PHP 4, PHP 5, PHP 7, PHP 8
   imagefill Remplissage PHP 4, PHP 5, PHP 7, PHP 8
   imagefilledrectangle Dessine un rectangle rempli PHP 4, PHP 5, PHP 7, PHP 8
   imagepng Envoie une image PNG vers un navigateur ou un fichier PHP 4, PHP 5, PHP 7, PHP 8
   imagettftext Dessine un texte avec une police TrueType PHP 4, PHP 5, PHP 7, PHP 8
   rand Génère une valeur aléatoire PHP 4, PHP 5, PHP 7, PHP 8
   return Retourne le controle du programme au module appelant. PHP 4, PHP 5, PHP 7, PHP 8
   session_cache_expire Retourne la configuration actuelle du cache expire - (PHP 4 >= 4.2.0, PHP 5, PHP 7) PHP 4, PHP 5, PHP 7, PHP 8
   session_start Initialise une session PHP 4, PHP 5, PHP 7, PHP 8
   sin Sinus PHP 4, PHP 5, PHP 7, PHP 8
   strlen Calcule la taille d'une chaîne PHP 4, PHP 5, PHP 7, PHP 8
   ucfirst Met le premier caractère en majuscule PHP 4, PHP 5, PHP 7, PHP 8

   Dites merci aux auteurs pour leurs travail, ça ne coûte rien et ça fait toujours plaisir wink

[2]

  • avatar

    Developpeurweb

    30 Mai 2011 à 08:46

    Le bon fonctionnement du script ne dépend pas de l'OS, mais il faut s'assurer que la librairie GD soit correctement installée et configurée.

  • avatar

    Dan4

    30 Mai 2011 à 06:48

    Fonctionne pas sous Windows avec wampserver. Sous linux oui.

Présentation de PHP

PHP débutant et initié 50 Tutoriel

Présentation de MySQL

avatar

Developpeurweb

  15 Avril 2011

  SOURCE   Télécharger

Information sur les mises à jour

Dernière mise à jour :

    19 Sept 2019
    fonctionnement du code vérifié

11 900 Vues
Compatibilité
PHP 5, 7 et 8+