[PHP] Image anti spam

Titre: Image anti spam
Date: Août 2006
Language: PHP
Description: Ce code crée une image anti spam utile pour les livres d’or, …

Utilisation:
Si le script est enregistré dans le fichier: anti_spam.php, on appèle l’image comme ceci:

<img src="nti_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.php

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


Publié

dans

par

Étiquettes :

Commentaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *