summaryrefslogtreecommitdiff
path: root/captcha_image.php
blob: 872e9c9287eb0e1b4c8df9c4594c21bfaf9f019c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
 * captcha image
 *
 * @copyright (c) 2004-15 bitweaver.org
 *
 * @package users
 * @subpackage functions
 */

/**
 * required setup
 */
require_once '../kernel/includes/setup_inc.php';

// dimensions
$width  = BitBase::verifyId( $_REQUEST['width'] ) ? $_REQUEST['width'] : 140;
$height = BitBase::verifyId( $_REQUEST['height'] ) ? $_REQUEST['height'] : 35;

$img = @imagecreate( $width, $height ) or die( "The GD image library doesn't seem to be available or doesn't have JPG support." );

// colours
$text[] = imagecolorallocate( $img, 0  , 0  , 128 );
$text[] = imagecolorallocate( $img, 0  , 128, 0   );
$text[] = imagecolorallocate( $img, 128, 0  , 0   );
$text[] = imagecolorallocate( $img, 0  , 128, 128 );
$text[] = imagecolorallocate( $img, 128, 128, 0   );
$text[] = imagecolorallocate( $img, 128, 0  , 128 );
$border = imagecolorallocate( $img, 0  , 0  , 0   );
$bg     = imagecolorallocate( $img, 230, 240, 250 );

imagefill( $img, 0, 0, $bg );
imagerectangle( $img, 1, 1, $width - 1, $height - 1, $border );
srand( time() );
$number = rand( 10000, 99999 );
$_SESSION['captcha'] = md5( $number );
for( $i = 0; $i < 5; $i++ ) {
	imagestring( $img, 10, ( $width / 6 ) - 5 + ( $width / 6 ) * $i + rand( 0, 2 ), 2 + rand( 0, $height - 22 ), substr( $number, $i, 1 ), $text[rand( 0, count( $text ) - 1 )] );
}

$gd = gd_info();
if( $gd["PNG Support"] ) {
	header( "Content-type: image/png" );
	imagepng( $img );
} elseif( $gd["GIF Create Support"] ) {
	header( "Content-type: image/gif" );
	imagegif( $img );
} else {
	header( "Content-type: image/jpeg" );
	imagejpeg( $img );
}