Image anti-spam

Description

Un petit script tout simple qui crée une image anti-spam...

Deux versions (correspondant aux deux zones de la capture):
- une simple (mais plus facilement lisible par des scripts OCR)
- et la seconde ben... moins lisibles (pour les gens comme pour les robots, ^^)

Dans le cas d'une utilisation de la seconde version, il serait pas mal de remplir le formulaire avec les valeurs déjà entrées en cas d'erreur de code car ca risque de se produire (on peut aussi enlenver le 0, le O et le D des caractères disponibles).

Source / Exemple :


<?php
session_start();

// type de flood
$name = $_GET['name'];
// nb de caractères
$strlen = (int) $_GET['strlen'];

// taille de l'image ( width )
$width = $strlen * 30 + 20;
$height = 60;

// 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( 150, 200), mt_rand( 150, 200), mt_rand( 150, 200) );

$colors = array( imagecolorallocate( $img, 70, 130, 255 ) , imagecolorallocate( $img, 255, 237, 175 ), imagecolorallocate( $img, 166, 250, 186 ), imagecolorallocate( $img, 253, 188, 251 ), imagecolorallocate( $img, 255, 255, 255 ) );

// création de l'image
for( $i = 0; $i < $width; $i++ )
{
	$r = $c1[0] + $i * ( $c2[0] - $c1[0] ) / $width;
	$v = $c1[1] + $i * ( $c2[1] - $c1[1] ) / $width;
	$b = $c1[2] + $i * ( $c2[2] - $c1[2] ) / $width;
	$color = imagecolorallocate( $img, $r, $v, $b );
	
	imageline( $img, $i, 0, $i, $height, $color );
}

// caractères
for( $i = 0; $i < $strlen; $i++ )
{
	$col = imagecolorallocate( $img, mt_rand( 0, 120 ), mt_rand( 0, 120 ), mt_rand( 0, 120 ) );
	imagettftext( $img, mt_rand( 20, 25 ), mt_rand( -30, 30 ), 10 + $i * 30, 35, $col, 'comic.ttf', $chaine[ $i ] );
}

// quelques lignes qui embêtent
for( $i = 0; $i < 8; $i++ )
{
	imageline( $img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $colors[mt_rand( 0, 4 )] );
}

$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 );
?>

########################################################################
Seconde version:
<?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 );
?>

Conclusion :


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

Voilà, j'attends vos comments sur ce petit script tout simple!

P.S. Ne pas oublier le session_start() sur la page qui vérifie les données! :-)

Codes Sources

A voir également

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.