Like this code?
Show the author your appreciation.
index.php
<?php
session_start ();
///->
if(isset( $_POST [ 'submit' ])){
$captcha = $_POST [ 'captcha' ];
$captcha = strtoupper ( $captcha ); // force to upper cuz captcha characters are uppers and it have to be too, else, the user will have to capitalize it by himself
if( md5 ( $captcha ) == md5 ( $_SESSION [ 'CaptchaCode' ])){
$_SESSION [ 'CaptchaCode' ] = NULL ; // destroy previous captcha code, so, if the user reload, he'll get the error
echo 'Congratulations, Captcha code Passed' ;
}else{
echo 'Error, check the verification code please' ;
}
}
?>
<?=$_SESSION [ 'CaptchaCode' ]; ?>
<form action="<?=$_SERVER [ 'PHP_SELF' ]; ?> " method="post">
Verification Image:
<input type="text" name="captcha" />
<img src="captcha.php" alt="0" />
<input type="submit" name="submit" />
</form>
captcha.php
<?
session_start ();
///->
class Captcha {
var $Image , $CNum , $Width , $Height , $Spacer , $Fonts , $Code , $TTF ;
var $FDir = 'fonts/' ; // Directory for fonts
var $Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ; // Allowed Characters
var $captcha_bg = 'captcha_bg.jpg' ; // Captcha Background Image
var $AngleMin = - 20 ; // min grades letter will be rotated
var $AngleMax = 20 ; // max grades letter will be rotated
///->
function Captcha ( $w = 180 , $h = 50 , $cn = 7 ){
$this -> Image = imagecreatefromjpeg ( $this -> captcha_bg );
$this -> Width = $w ;
$this -> Height = $h ;
$this -> CNum = $cn ;
$this -> Fonts = array( 'Adler.ttf' , 'Adonais.ttf' , 'Alba.ttf' , 'Baveuse.ttf' , 'Heck.ttf' );
//->
$this -> Get_Space ();
$this -> GenerateCode ();
}
///->
function Get_Space (){ $this -> Spacer = (int)( $this -> Width / $this -> CNum ); }
///->
function GenerateCode (){
mt_srand ( microtime () * 1000000 );
$this -> Code = '' ;
//->
for( $i = 0 ; $i < $this -> CNum ; $i ++){
$key = mt_rand ( 0 , strlen ( $this -> Chars )- 1 );
$this -> Code = $this -> Code . $this -> Chars { $key };
}
for( $i = 0 ; $i < $this -> CNum ; $i ++){
$key_1 = mt_rand ( 0 , strlen ( $this -> Code )- 1 );
$key_2 = mt_rand ( 0 , strlen ( $this -> Code )- 1 );
$tmp = $this -> Code { $key_1 };
$this -> Code { $key_1 } = $this -> Code { $key_2 };
$this -> Code { $key_2 } = $tmp ;
}
//->
$this -> Code = htmlentities ( $this -> Code , ENT_QUOTES );
}
///->
function drawLines (){
for( $i = 0 ; $i < $this -> LNum ; $i ++){
$Colour = rand ( 100 , 250 );
$colour = imagecolorallocate ( $this -> Image , $Colour , $Colour , $Colour );
//->
$W = $this -> Width ; $H = $this -> Height ;
imageline ( $this -> Image , rand ( 0 , $this -> Width ), rand ( 0 , $this -> Height ), rand ( 0 , $this -> Width ), rand ( 0 , $this -> Height ), $colour );
}
}
function drawCode (){
for( $i = 0 ; $i < $this -> CNum ; $i ++){
$r1 = rand ( 90 , 120 ); $r2 = rand ( 100 , 130 ); $r3 = rand ( 110 , 140 );
$Letter = substr ( $this -> Code , $i , 1 );
$fcolor = imagecolorallocate ( $this -> Image , $r1 , $r2 , $r3 );
$font = $this -> FDir . $this -> Fonts [ rand ( 0 , count ( $this -> Fonts )- 1 )];
$fsize = rand ( 24 , 36 );
if( $i > 0 && $i < $this -> CNum - 1 ){ $Angle = rand ( $this -> AngleMin , $this -> AngleMax ); }else{ $Angle = 0 ; }
//->
$TB = imagettfbbox ( $fsize , $Angle , $font , $Letter );
$X = $this -> Spacer / 5 + $i * $this -> Spacer ;
$iCharHeight = $TB [ 2 ] - $TB [ 5 ];
$Y = $this -> Height / 2 + $iCharHeight / 4 ;
imagettftext ( $this -> Image , $fsize , $Angle , $X , $Y , $fcolor , $font , $Letter );
}
}
function Draw (){
$this -> drawCode ();
//->
$_SESSION [ 'CaptchaCode' ] = $this -> Code ;
//->
header ( "Expires: 0" );
if( function_exists ( 'imagegif' )){
header ( "Content-type: image/gif" );
header ( 'Cache-control: no-cache, no-store' );
imagegif ( $this -> Image );
}elseif( function_exists ( 'imagejpeg' )){
header ( "Content-type: image/jpeg" );
header ( 'Cache-control: no-cache, no-store' );
imagejpeg ( $this -> Image , null , 100 );
}else{
header ( "Content-type: image/png" );
header ( 'Cache-control: no-cache, no-store' );
imagepng ( $this -> Image );
}
imagedestroy ( $this -> Image );
exit;
}
}
$captcha = new Captcha ;
$captcha -> Draw ();
?>
A damaged image generator (class) for validating text.
CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart Categories : PHP , PHP Classes , Security , GD image library , Security CAPTCHA[Image verification] Categories : PHP , Security , GD image library , Graphics , Sessions Generate image with random number (CAPTCHA) Categories : PHP , GD image library , Graphics , Security Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality. Categories : PHP , GD image library , Graphics Create a Thumbnail Using PHP, GD and Exif Categories : PHP , Graphics , Exif , GD image library PHP Email image generator - hide your email from bots - using the GD Library Categories : PHP , Graphics , GD image library , Beginner Guides Advanced Image WaterMarker Categories : PHP , PHP Classes , GD image library , Graphics , Object Oriented Line graphics generation library written in PHP + GD library (spanish comments) Categories : PHP , Graphics , GD image library Simple PHP Bar Graph using GD library Categories : PHP , GD image library , Graphics , Arrays Image Generation Class ( PNG Format ) Categories : PHP , GD image library , PHP Classes , Graphics PHP Image Compression using GD library Categories : PHP , Compression , GD image library , Graphics a simple pie-chart in php3 (with gd) Categories : PHP , Graphics , GD image library , Charts and Graphs Display a bar chart based on random values. Categories : Graphics , PHP , GD image library , Charts and Graphs EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly. Categories : PHP , PHP Classes , Object Oriented , Graphics , GD image library imageMarker v 3.00 with new advanced features Categories : PHP , PHP Classes , Graphics , GD image library