class.captcha.php:


<?php
error_reporting(E-ALL);
session_start();

////////////////////////////////////////////////////////////////////////////////////////////
// $capcha = new captcha();                                                               //
// $captcha->make_captcha($lengte, $type, $kleur_patroon);                                //
//                                                                                        //
// $lengte bevat de waarde van het gewenst aantal karakters voor je captcha               //
//        standaard staat deze op 5;                                                      //
//                                                                                        //
// $type bevat het type hoe je de captcha wil opbouwen (zie hier beneden)                 //
// 		  standaard staat deze op 5;                                                      //
//			- 0 : alles                                                                   //
//			- 1 : enkel Numeric                                                           //
//			- 2 : enkel Lowercase                                                         //
//			- 3 : enkel Uppercase                                                         //
//			- 4 : Numeric/Lowercase                                                       //
//			- 5 : Numeric/Uppercase                                                       //
//			- 6 : Lowercase/Uppercase                                                     //
//                                                                                        //
// $kleur_patroon bevat het patroon en kan 0 of 1 zijn                                    //
//	      standaard is deze 0 en is zwart de achtergrond met witte boord en witte letters //
//	      standaard is deze 1 en is wit de achtergrond met zwarte boord en zwarte letters //
//                                                                                        //
//                                                                                        //
//    voorbeeld1: $captcha = new captcha();                                               //
//                $captcha->make_captcha();                                               //
//                                                                                        //
//    voorbeeld2: $captcha = new captcha();                                               //
//                $captcha->make_captcha(8);                                              //
//                                                                                        //
//    voorbeeld3: $captcha = new captcha();                                               //
//                $captcha->make_captcha(6, 1);                                           //
//                                                                                        //
//    voorbeeld4: $captcha = new captcha();                                               //
//                $captcha->make_captcha(5, 5, 1);                                        //
////////////////////////////////////////////////////////////////////////////////////////////

class captcha {

	// pas hier het path aan naar het path waar jouw fonts zich bevinden op de server
	var $fonts_path = '../../../resources/fonts/';
	
	// pas hier eventueel de lettertypes aan die je wenst te gebruiken
	// let er wel op dat je bij het kiezen rekening houd met de duidelijkheid!
	var $fonts = array(
			'arialbd.ttf',
			'calibrib.ttf',
			'comicbd.ttf',
			'courbd.ttf',
			'framdit.ttf',
			'georgiab.ttf',
			'impact.ttf',
			'seguibd.ttf',
			'tahomabd.ttf',
			'trebucbd.ttf',
			'verdanab.ttf'
    );
	// hieronder niets meer veranderen.
	
	// definiëren van de variabelen voor het verdere script
	var $chars;
	var $type;
	var $color;
	var $char_array = array();
	var $captcha_code;
	
	function generate_code() {
		$this->make_char_array();
		// We maken een for loop om een random code te maken
		for($i = 0; $i < $this->chars; $i++) {
	    	// we pakken random getallen uit de array $this->char_array, -1 aangezien een array begint bij 0;
			$count = count($this->char_array);
			$rand_i = mt_rand(0, $count-1);
    	    // we stoppen de gegenereerde code in de variabele $this->captcha_code
			$this->captcha_code .= $this->char_array[$rand_i];
		}
	}
	function make_char_array() {
		// aanmaken van de verschillende array's voor type keuze.
		// uit de array's zijn verwarrende karakters zoals 0 en O en i en 1 en G eruit gelaten
		// dit kan je evnetueel nog zelf wat aanpassen.
		$low_chars = array_merge(range('a', 'f'), array('h', 'j', 'k', 'm', 'n'), range('p', 'z'));
		$upp_chars = array_merge(range('A', 'F'), array('H', 'J', 'K', 'M', 'N'), range('P', 'Z'));
		$dec_chars = array_merge(range(2, 8), range(2, 8));
		
		// bepalen welk type er gekozen werd en de array met de correcte characters vullen.
		switch ($this->type) {
			case 0:
				$this->char_array = array_merge($low_chars, $upp_chars, $dec_chars, $dec_chars);
				break;
			case 1:
				$this->char_array = $dec_chars;
				break;
			case 2:
				$this->char_array = $low_chars;
				break;
			case 3:
				$this->char_array = $upp_chars;
				break;
			case 4:
				$this->char_array = array_merge($low_chars, $dec_chars);
				break;
			case 6:
				$this->char_array = array_merge($low_chars, $upp_chars);
				break;
			default:
				$this->char_array = array_merge($upp_chars, $dec_chars);
		}
	}
	function make_captcha($chars=5, $type=5, $color=0) {
		$this->chars = $chars;
		$this->type = $type;
		$this->color = $color;
		$this->generate_code();
		
		// We laten het script weten dat we een plaatje gaan maken
		header('Content-type: image/png');	
				
		// we geven de breedte van de afbeelding op
		$img_width = $this->chars * 23;
		
		// we maken het plaatje aan
		$image = imagecreatetruecolor($img_width, 46);

		// we bepalen de kleuren die we nodig hebben		
		if ($this->color == 1) {
			$noise_colors = array(51, 102, 153, 204);
			$bg_color = imagecolorallocate($image, 255, 255, 255);
			$color = imagecolorallocate($image, 0, 0, 0);
		} else {
			$noise_colors = array(51, 102, 153, 204);
			$bg_color = imagecolorallocate($image, 0, 0, 0);
			$color = imagecolorallocate($image, 255, 255, 255);
		}
		
		// achtergrond instellen en boorden aanmaken
		imagefill($image, 0, 0, $bg_color);
		imageline($image, 0, 0, 0, 46, $color); //een zwart lijntje links
		imageline($image, 1, 0, $img_width, 0, $color); //een zwart lijntje boven
		imageline($image, $img_width-1, 0, $img_width-1, 46, $color); //een zwart lijntje rechts
		imageline($image, 1, 45, $img_width-1, 45, $color); //en een zwart lijntje onder

		// random dots genereren op de achtergrond in random kleuren
		for( $i=0; $i<($img_width*44)/2; $i++ ) {
			$noise_color = imagecolorallocate($image, $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)]);
			imagefilledellipse($image, mt_rand(2,$img_width-2), mt_rand(2,44), 1, 1, $noise_color);
		}
		// random lijnen genereren op de achtergrond in random kleuren
		for( $i=0; $i<($img_width*44)/100; $i++ ) {
			$noise_color = imagecolorallocate($image, $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)], $noise_colors[array_rand($noise_colors)]);
			imageline($image, mt_rand(2,$img_width-2), mt_rand(2,44), mt_rand(2,$img_width-2), mt_rand(2,44), $noise_color);
		}

		// we maken een for loop voor de letters
		for($i = 0; $i < $this->chars; $i++) {
		    // we zetten de letters op de juiste plaats
    		$font =	$this->fonts_path . $this->fonts[array_rand($this->fonts)];
			$pos_h = $i * 21 + 6;
			imagettftext($image, 
                19, 
                mt_rand(-20,20), //willekeurige rotatie
                $pos_h, //plaats horizontaal
                mt_rand(20,40), //random plaats verticaal
                $color, // voorkleur
                $font, //willekeurig lettertype
                $this->captcha_code[$i]); //met de letter
		}
		// We laten het script weten dat we een plaatje gaan maken
		header('Content-type: image/png');
		imagepng($image);
		imagedestroy($image);
		$_SESSION['captcha_code'] = sha1(md5($this->captcha_code));
	}	
}
?>


voorbeelden van gebruik: (andere voorbeelden in zipje te downloaden)

voorbeeld 1: (standaard gebruik zonder parameters)

<?php
include('class.captcha.php');

$captcha = new captcha();
$captcha->make_captcha();
?>


voorbeeld 2: (gebruik met alle parameters)

<?php
include('class.captcha.php');

$captcha = new captcha();
$captcha->make_captcha(8, 1, 1);
?>


voorbeeld van gebruik in formulier:

<?
// We willen op de hoogte gehouden worden van alle errors / notices
ini_set('display_errors', 1);
error_reporting(E_ALL);

// we gaan met sessies werken
session_start();
?>
<html>
    <head>
        <title>Captcha Class</title>
    </head>
<body>

<?
// controlen of het formulier gesubmit is
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(!empty($_POST['captcha_code']) && sha1(md5($_POST['captcha_code'])) == $_SESSION['captcha_code'])
    {
        echo 'De code is correct, Gefeliciteerd!';
    }
    else {
        echo 'De ingevoerde code is niet juist!';
    }
}
else {
    $i = range(1, 8);
	echo '
        Je kan nu html plaatsen zonder probleem en pas verder op je pagina de captcha image gebruiken.
		<form method="post" action="'.$_SERVER['PHP_SELF'].'">
        <p>
			<img src="c' . $i[array_rand($i)] . '.php" />
        </p>
        <p>
            Neem de code precies zo over zoals aangegeven hierboven.<br />
			Hou dus rekening met Hoofdletter en Kleine letters.<br />
            <label for="captcha_code">Code:</label>
            <input type="text" name="captcha_code">
        </p>
        <p>
            <input type="submit" name="submit" value="Controleer">
        </p>
        </form>
    ';
}
?>
</body>
</html>
