php captcha code


<?php
session_start();
$font = ‘font.ttf’;
$RandomStr = md5(microtime());// md5 to generate the random string
$code  = substr($RandomStr,0,5);//trim 5 digit

$width = 150;
$height = 50;

$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die(‘Cannot initialize new GD image stream’);
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $font, $code) or die(‘Error in imagettfbbox function’);
$x = ($width – $textbox[4])/2;
$y = ($height – $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code) or die(‘Error in imagettftext function’);
/* output captcha image to browser */

$_SESSION[‘sessionKey’.session_id()] = $code ;// carry the data through session
header(“Content-type: image/jpeg”);// out out the image
imagejpeg($image,”,100);//Output image to browser
?>

Posted in php

Leave a comment