Skip to content

Instantly share code, notes, and snippets.

@TorbenKoehn
Created November 14, 2013 12:12
Show Gist options
  • Save TorbenKoehn/7465822 to your computer and use it in GitHub Desktop.
Save TorbenKoehn/7465822 to your computer and use it in GitHub Desktop.
A really old captcha system I found somewhere in my old codes.
<?php
//Simple Captcha Function
//Creates funny and not that easily readable captchas
// ================
// (c) 2009 by DarkDevine
// Modificate as you want, just keep some credits
// Fonts from http://urbanfonts.com , you can get new ones there as well
// ================
// Usage: generate_captcha( $mode ) for generating the captcha,
// check_captcha( $string) for checking with user input
define( 'CAPTCHA_COLOR_POOL', serialize( array( //Array of color values ( array( R, G, B ) )
array( 134, 134, 134 ),
array( 162, 162, 162 ),
array( 189, 189, 189 ),
array( 120, 120, 120 ),
array( 98, 98, 98 ),
array( 60, 60, 60 )
) ) );
if( !defined( 'CAPTCHA_BACKGROUND_POOL' ) )
define( 'CAPTCHA_BACKGROUND_POOL', serialize( array( //Array of color values ( array( R, G, B ) )
array( 243, 243, 243 )
) ) );
define( 'CAPTCHA_CHAR_POOL', 'abcdefghkmnprtwxyABCDEFGHJKLMNPRTWXY346789' ); //Just put the chars you want to allow
define( 'CAPTCHA_SESSION_KEY', '__captcha__' ); //Key of the session variable that will be set
define( 'CAPTCHA_CHECK_CASE', FALSE ); //Choose wether or not it should be checked case-sensitive (true = case-sensitive)
define( 'CAPTCHA_FONTS', serialize( array( //Fonts we want to use (Need to be TTF Files)
'captcha_fonts/adler.ttf',
'captcha_fonts/Beccaria.ttf',
'captcha_fonts/bulkeyRefuse.ttf',
'captcha_fonts/carbon.ttf',
'captcha_fonts/FACERG__.TTF',
'captcha_fonts/HALDANOR.TTF'
)));
define( 'CAPTCHA_WIDTH', 200 ); //Width of the captcha
define( 'CAPTCHA_HEIGHT', 50 ); //Height of the captcha
define( 'CAPTCHA_TEXT_LENGHT', 6 ); //Lenght of the text of the captcha
define( 'CAPTCHA_UNREADABALIZER', 30 ); //Indicates how much we wanna make the captcha...unreadable
//Function.
// $mode 0 = Return as a PNG File with a PNG Header
// $mode 1 = Return as a GIF File with a GIF Header
// $mode 2 = Return as HTML that shows the captcha
// $mode 3 = Return as Path to the image file
// $mode 4 = Return the image handle
function generate_captcha( $mode = 0 ) {
//Checking if we installed the GD
if( !extension_loaded( 'gd' ) )
die( 'Failed to load PHP extension gd' );
//Generate the main image
$img = imagecreatetruecolor( CAPTCHA_WIDTH, CAPTCHA_HEIGHT );
//Create a random hash
$hash = '';
$pool = CAPTCHA_CHAR_POOL;
for( $x = 0; $x < CAPTCHA_TEXT_LENGHT; $x++ )
$hash .= $pool[ rand( 0, strlen( $pool ) - 1 ) ];
//Generate the background
$bgcolors = unserialize( CAPTCHA_BACKGROUND_POOL );
$c = $bgcolors[ rand( 0, count( $bgcolors ) - 1 ) ];
$bgcolor = imagecolorallocate( $img, $c[ 0 ], $c[ 1 ], $c[ 2 ] );
//Background color rectangle
imagefilledrectangle( $img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bgcolor );
//NEED MOAR
//creating some random lines...
for( $x = 0; $x < CAPTCHA_UNREADABALIZER; $x++ ) {
$c = $bgcolors[ rand( 0, count( $bgcolors ) - 1 ) ];
$linecolor = imagecolorallocate( $img, $c[ 0 ], $c[ 1 ], $c[ 2 ] );
//Draw it
imageline( $img, rand( 0, CAPTCHA_WIDTH ), 0, rand( 0, CAPTCHA_WIDTH ), CAPTCHA_HEIGHT, $linecolor );
imageline( $img, 0, rand( 0, CAPTCHA_HEIGHT ), CAPTCHA_WIDTH, rand( 0, CAPTCHA_HEIGHT ), $linecolor );
}
//Every letter will get another font.
//Warning, I don't implement stuff to check if the font exists, I rather write this comment instead
$fontpool = unserialize( CAPTCHA_FONTS );
$fontcolors = unserialize( CAPTCHA_COLOR_POOL );
$offset = CAPTCHA_WIDTH-(CAPTCHA_WIDTH*0.8);
for( $x = 0; $x < strlen( $hash ); $x++ ) {
//What font will we use today?
$font = $fontpool[ rand( 0, count( $fontpool ) - 1 ) ];
//And what color?
$c = $fontcolors[ rand( 0, count( $fontcolors ) - 1) ];
$color = imagecolorallocate( $img, $c[ 0 ], $c[ 1 ], $c[ 2 ] );
//Height modifier
$hm = rand( -((int)(CAPTCHA_UNREADABALIZER/20)), (int)(CAPTCHA_UNREADABALIZER/20) );
//Font size
$fs = rand( (int)(CAPTCHA_WIDTH/10), (int)((CAPTCHA_WIDTH/10)+(CAPTCHA_UNREADABALIZER/7)) );
//Angle
$angle = rand( -15, 15 );
//Get the bounding box
$b = imagettfbbox( $fs, $angle, $font, $hash[ $x ] );
//Calculate the Y Position with the height of the font, the image height and the modifier
$y = (CAPTCHA_HEIGHT/1.5)-((($b[ 1 ]-$b[ 6 ])/2)+$hm);
//Finally...write the char
imagettftext( $img, $fs, $angle, $offset, $y, $color, $font, $hash[ $x ] );
//and move the offset forward
$offset += $b[ 4 ] - $b[ 6 ];
}
//Trash it up a bit more
for( $x = 0; $x < (int)(CAPTCHA_UNREADABALIZER/10); $x++ ) {
$c = $bgcolors[ rand( 0, count( $bgcolors ) - 1 ) ];
$linecolor = imagecolorallocate( $img, $c[ 0 ], $c[ 1 ], $c[ 2 ] );
//Draw it
imageline( $img, rand( 0, CAPTCHA_WIDTH ), 0, rand( 0, CAPTCHA_WIDTH ), CAPTCHA_HEIGHT, $linecolor );
imageline( $img, 0, rand( 0, CAPTCHA_HEIGHT ), CAPTCHA_WIDTH, rand( 0, CAPTCHA_HEIGHT ), $linecolor );
}
//And finally fuck the whole mess even more up
imagefilter( $img, IMG_FILTER_GAUSSIAN_BLUR );
//Saving our nice hash/trash to a session variable
if( !isset( $_SESSION ) )
@session_start();
$_SESSION[ CAPTCHA_SESSION_KEY ] = $hash;
//Do some garbage cleaning
if( $mode == 2 || $mode == 3 ) {
$files = glob( './_cptmp/*_*.png' );
if( count( $files ) ) {
foreach( $files as $file )
if( time()-filemtime( $file ) > 60 )
unlink( $file );
}
}
switch( $mode ) {
case 0:
default:
header( 'Content-type: image/png' );
imagepng( $img );
break;
case 1:
header( 'Content-type: image/gif' );
imagegif( $img );
break;
case 2:
//Save the damn thing somewhere
if( !is_dir( './_cptmp' ) )
mkdir( './_cptmp', 0777, true );
$path = './_cptmp/'.md5( $hash ).'.png';
imagepng( $img, $path );
echo '<img src="'.$path.'" border="0" />';
break;
case 3:
if( !is_dir( './_cptmp' ) )
mkdir( './_cptmp', 0777, true );
$path = './cptmp/'.md5( $hash ).'.png';
imagepng( $img, $path );
imagedestroy( $img );
return $path;
case 4:
return $img;
}
imagedestroy( $img );
}
function check_captcha( $string ) {
if( !isset( $_SESSION[ CAPTCHA_SESSION_KEY ] ) )
return FALSE;
return CAPTCHA_CHECK_CASE === TRUE ? ( strcmp( $string, $_SESSION[ CAPTCHA_SESSION_KEY ] ) != 0 ? FALSE : TRUE ) : ( strtolower( $string ) == strtolower( $_SESSION[ CAPTCHA_SESSION_KEY ] ) ? TRUE : FALSE );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment