Titre: Image anti spam V2
Date: Avril 2007
Language: PHP
Description: Ce code crée une image anti spam utile pour les livres d’or, … L’image est plus difficile à lire que la version 1 (certains robots la contournaient trop facilement)

Utilisation:
Si le script est enregistré dans le fichier: anti_spam.php, on appèle l’image comme ceci:
<img src="anti_spam.php?name=livreor&strlen=4" alt="anti-flood">
>> le contenu dans l’image sera dans $_SESSION[‘livreor’]
>> 4 caractères
Si $spam représente l’entrée utilsateur, le test se fait comme ceci:
if( $_SESSION[‘livreor’] != strtoupper( $spam ) )
// erreur ici
Resources:
Si le script ne fonctionne pas, mettez cette police dans le répertoire du script.
Code de anti_spam_v2.php
<?php
// anti_spam_v2.php
session_start();
// type de flood
$name = $_GET['name'];
// nb de caractères
$strlen = (int) $_GET['strlen'];
// taille de l'image ( width )
$width = $strlen * 23 + 20;
$height = 60;
// taille de chaque zone de couleur
$widthColor = $width / 4;
// création
$img = imagecreatetruecolor( $width, $height );
// antialising, c'est plus bô! :-)
imageantialias( $img, 1 );
// chaine
$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$chaine = '';
for( $i = 0; $i < $strlen; $i++ )
$chaine .= $string[ mt_rand( 0, 35 ) ];
$_SESSION[ $name ] = $chaine;
// couleur de départ
$c1 = array( mt_rand( 200, 255), mt_rand( 200, 255), mt_rand( 200, 255) );
// couleur finale
$c2 = array( mt_rand( 70, 180), mt_rand( 70, 180), mt_rand( 70, 180) );
// pas pour chaque composante de couleur
$diffsColor = array( ( $c1[0] - $c2[0] ) / $widthColor, ( $c1[1] - $c2[1] ) / $widthColor, ( $c1[2] - $c2[2] ) / $widthColor );
$start = 0;
$end = $widthColor;
for( $j = 0; $j < 4; $j++ ) // boucle pour chacune des 4 zones
{
$r = $j % 2 == 0 ? $c1[0] : $c2[0]; // composante r de départ
$v = $j % 2 == 0 ? $c1[1] : $c2[1]; // idem v
$b = $j % 2 == 0 ? $c1[2] : $c2[2]; // idem b
// création des lignes
for( $i = $start; $i < $end; $i++ )
{
if( $j % 2 == 0 )
{
$r -= $diffsColor[0];
$v -= $diffsColor[1];
$b -= $diffsColor[2];
}
else
{
$r += $diffsColor[0];
$v += $diffsColor[1];
$b += $diffsColor[2];
}
$color = imagecolorallocate( $img, $r, $v, $b );
imageline( $img, $i, 0, $i, $height, $color );
}
$start += $widthColor;
$end += $widthColor;
}
$colorsChar = array( ); // on va mémoriser les couleurs des caractères
// caractères
for( $i = 0; $i < $strlen; $i++ )
{
$colorsChar[$i] = imagecolorallocate( $img, mt_rand( 0, 120 ), mt_rand( 0, 120 ), mt_rand( 0, 120 ) );
imagettftext( $img, mt_rand( 20, 25 ), mt_rand( -35, 35 ), 10 + $i * 23, 35, $colorsChar[$i], 'comic.ttf', $chaine[ $i ] );
}
// quelques lignes qui embêtent
for( $i = 0; $i < 10; $i++ )
{
imageline( $img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $colorsChar[mt_rand( 0, $strlen - 1 )] );
}
$noir = imagecolorallocate( $img, 0, 0, 0 );
// bordure
imageline( $img, 0, 0, $width, 0, $noir );
imageline( $img, 0, 0, 0, $height, $noir );
imageline( $img, $width - 1, 0, $width - 1, $height, $noir );
// header: image
header("Content-type: image/png");
imagepng( $img );
imagedestroy( $img );
?>
Laisser un commentaire