Encore un captcha.
Pour celui-ci, je ne me suis pas vraiment foulé ;-)
J'ai pris la base du code de Coockiesch :
http://www.phpcs.com/codes/IMAGE-ANTI-SPAM_38969.aspx
et l'algorithme de la classe Kaptcha de Kruglov Sergei :
http://www.phpclasses.org/browse/package/3193.html pour les vagues.
L'utilisation est comme d'hab : on définit les variables au début du fichier pour personnaliser.
On récupère dans la variable de session $_SESSION['captcha'] la chaîne.
Voilà :-)
Source / Exemple :
<?php
/**
// definitions
$foreground_color = array(mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
$background_color = array(mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
$strlen = 5;
$width = $strlen * 30 + 20;
$height = 60;
$iLinesAndArcs = 10;
$iFontMax = 20;
$iFontMin = 25;
$iFontRotationMin = -30;
$iFontRotationMax = 30;
$iFontBaseLine = $height - 15;
$iFontSpace = 20;
$iFontMult = 30;
$rMin = 180;
$rMax = 255;
$vMin = 180;
$vMax = 255;
$bMin = 180;
$bMax = 255;
$sTTFFontPath = 'verdana.ttf';
// création
$img = imagecreatetruecolor($width, $height);
$img2 = imagecreatetruecolor($width, $height);
imageantialias( $img, 1 );
$foreground = imagecolorallocate($img, $foreground_color[0], $foreground_color[1], $foreground_color[2]);
$background = imagecolorallocate($img, $background_color[0], $background_color[1], $background_color[2]);
imagefill ($img, 0,0,$background);
$rand1=mt_rand(900000,1000000)/10000000;
$rand2=mt_rand(900000,1000000)/10000000;
$rand3=mt_rand(900000,1000000)/10000000;
$rand4=mt_rand(900000,1000000)/10000000;
// phases
$rand5=mt_rand(0,3141592)/500000;
$rand6=mt_rand(0,3141592)/500000;
$rand7=mt_rand(0,3141592)/500000;
$rand8=mt_rand(0,3141592)/500000;
// amplitudes
$rand9=mt_rand(330,420)/110;
$rand10=mt_rand(330,450)/110;
for( $i = 0; $i < $iLinesAndArcs; $i++ ) {
$r = mt_rand ($rMin, $rMax);
$v = mt_rand ($vMin, $vMax);
$b = mt_rand ($bMin, $bMax);
$color = imagecolorallocate($img, $r, $v, $b);
imageline( $img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
imagearc($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), 0, 360, $color);
}
// chaine
$string = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
$chaine = '';
for( $i = 0; $i < $strlen; $i++ )
$chaine .= $string[ mt_rand( 0, strlen ($string) - 1 ) ];
$_SESSION['test'] = $chaine;
for( $i = 0; $i < $strlen; $i++ ) {
imagettftext( $img, mt_rand($iFontMax, $iFontMin), mt_rand($iFontRotationMin, $iFontRotationMax), $i * $iFontMult + $iFontSpace, $iFontBaseLine, $foreground, $sTTFFontPath, $chaine[$i] );
}
//wavy stuff
for($x=0;$x<$width;$x++){
for($y=0;$y<$height;$y++){
$sx=$x+(sin($x*$rand1+$rand5)+sin($y*$rand3+$rand6))*$rand9-$width/2+($width/2)+1;
$sy=$y+(sin($x*$rand2+$rand7)+sin($y*$rand4+$rand8))*$rand10;
if($sx<0 || $sy<0 || $sx>=$width-1 || $sy>=$height-1){
$color=255;
$color_x=255;
$color_y=255;
$color_xy=255;
}else{
$color=imagecolorat($img, $sx, $sy) & 0xFF;
$color_x=imagecolorat($img, $sx+1, $sy) & 0xFF;
$color_y=imagecolorat($img, $sx, $sy+1) & 0xFF;
$color_xy=imagecolorat($img, $sx+1, $sy+1) & 0xFF;
}
if($color==0 && $color_x==0 && $color_y==0 && $color_xy==0){
$newred=$foreground_color[0];
$newgreen=$foreground_color[1];
$newblue=$foreground_color[2];
}else if($color==255 && $color_x==255 && $color_y==255 && $color_xy==255){
$newred=$background_color[0];
$newgreen=$background_color[1];
$newblue=$background_color[2];
}else{
$frsx=$sx-floor($sx);
$frsy=$sy-floor($sy);
$frsx1=1-$frsx;
$frsy1=1-$frsy;
$newcolor=(
$color*$frsx1*$frsy1+
$color_x*$frsx*$frsy1+
$color_y*$frsx1*$frsy+
$color_xy*$frsx*$frsy);
if($newcolor>255) $newcolor=255;
$newcolor=$newcolor/255;
$newcolor0=1-$newcolor;
$newred=$newcolor0*$foreground_color[0]+$newcolor*$background_color[0];
$newgreen=$newcolor0*$foreground_color[1]+$newcolor*$background_color[1];
$newblue=$newcolor0*$foreground_color[2]+$newcolor*$background_color[2];
}
imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newred, $newgreen, $newblue));
}
}
// bordure
$noir = imagecolorallocate ($img2,0,0,0);
imageline( $img2, 0, 0, $width, 0, $noir );
imageline( $img2, 0, $height - 1, $width, $height - 1, $noir );
imageline( $img2, 0, 0, 0, $height, $noir );
imageline( $img2, $width - 1, 0, $width - 1, $height, $noir );
// header: image
header("Content-type: image/png");
imagepng( $img2 );
imagedestroy( $img2 );
?>
je suis debutant en web, et je désire utiliser un captcha pour la protection de form,
pouriez vous me faire une petite explication (dans mon box) du fonctionnement et de l'utlisation des codes captcha.
merci
pour passer a 100% de fiabilite, il suffirait de raprocher tes lettres, pour qu'on ne puisse plus les separer.
Ce code est plutôt pas mal! ;D Fiable à 90% c'est pour moi un bon résultat. Mais comme tu le proposes sur ton commentaire du 05 février, serait-il possible de voir ton autre captcha beaucoup plus simple et "sûr" ? Merci.
non, GD est présent, sinon il aurait planté juste avant.
Commente simplement la ligne avec imageantialias().
Ce sera moins...aliasé...c'est tout :-)
Donc si j'ai bie compris je ne peux pas utiliser ce script avec mon serveur actuel? :) ( ou j'ai rien compris du tout...lol)
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.