| 1 |
<?php |
| 2 |
session_start(); |
| 3 |
|
| 4 |
//Settings: You can customize the captcha here |
| 5 |
$image_width = 120; |
| 6 |
$image_height = 40; |
| 7 |
|
| 8 |
$characters_on_image = 6; |
| 9 |
if(isset($_SESSION['total_no_of_characters'])) |
| 10 |
$characters_on_image = $_SESSION['total_no_of_characters']; |
| 11 |
|
| 12 |
$font = dirname(__FILE__) . '/monofont.ttf'; |
| 13 |
|
| 14 |
//The characters that can be used in the CAPTCHA code. |
| 15 |
//avoid confusing characters (l 1 and i for example) |
| 16 |
if(isset($_SESSION['captcha_type']) && $_SESSION['captcha_type'] == 'alphanumeric') |
| 17 |
{ |
| 18 |
switch($_SESSION['captcha_letters']) |
| 19 |
{ |
| 20 |
case 'capital': |
| 21 |
$possible_letters = '23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 22 |
break; |
| 23 |
case 'small': |
| 24 |
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz'; |
| 25 |
break; |
| 26 |
case 'capitalsmall': |
| 27 |
$possible_letters = '23456789bcdfghjkmnpqrstvwxyzABCEFGHJKMNPRSTVWXYZ'; |
| 28 |
break; |
| 29 |
default: |
| 30 |
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz'; |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
elseif(isset($_SESSION['captcha_type']) && $_SESSION['captcha_type'] == 'alphabets') |
| 35 |
{ |
| 36 |
switch($_SESSION['captcha_letters']) |
| 37 |
{ |
| 38 |
case 'capital': |
| 39 |
$possible_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 40 |
break; |
| 41 |
case 'small': |
| 42 |
$possible_letters = 'bcdfghjkmnpqrstvwxyz'; |
| 43 |
break; |
| 44 |
case 'capitalsmall': |
| 45 |
$possible_letters = 'bcdfghjkmnpqrstvwxyzABCEFGHJKMNPRSTVWXYZ'; |
| 46 |
break; |
| 47 |
default: |
| 48 |
$possible_letters = 'abcdefghijklmnopqrstuvwxyz'; |
| 49 |
break; |
| 50 |
} |
| 51 |
} |
| 52 |
elseif(isset($_SESSION['captcha_type']) && $_SESSION['captcha_type'] == 'numbers') |
| 53 |
{ |
| 54 |
$possible_letters = '0123456789'; |
| 55 |
} |
| 56 |
else |
| 57 |
{ |
| 58 |
$possible_letters = '0123456789'; |
| 59 |
} |
| 60 |
$random_dots = 0; |
| 61 |
$random_lines = 20; |
| 62 |
$captcha_text_color="0x142864"; |
| 63 |
$captcha_noice_color = "0x142864"; |
| 64 |
|
| 65 |
$code = ''; |
| 66 |
|
| 67 |
|
| 68 |
$i = 0; |
| 69 |
while ($i < $characters_on_image) |
| 70 |
{ |
| 71 |
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1); |
| 72 |
$i++; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
$font_size = $image_height * 0.75; |
| 77 |
$image = @imagecreate($image_width, $image_height); |
| 78 |
|
| 79 |
|
| 80 |
/* setting the background, text and noise colours here */ |
| 81 |
$background_color = imagecolorallocate($image, 255, 255, 255); |
| 82 |
|
| 83 |
$arr_text_color = hexrgb($captcha_text_color); |
| 84 |
$text_color = imagecolorallocate($image, $arr_text_color['red'], |
| 85 |
$arr_text_color['green'], $arr_text_color['blue']); |
| 86 |
|
| 87 |
$arr_noice_color = hexrgb($captcha_noice_color); |
| 88 |
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], |
| 89 |
$arr_noice_color['green'], $arr_noice_color['blue']); |
| 90 |
|
| 91 |
|
| 92 |
/* generating the dots randomly in background */ |
| 93 |
for( $i=0; $i<$random_dots; $i++ ) |
| 94 |
{ |
| 95 |
imagefilledellipse($image, mt_rand(0,$image_width), mt_rand(0,$image_height), 2, 3, $image_noise_color); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/* generating lines randomly in background of image */ |
| 100 |
for( $i=0; $i<$random_lines; $i++ ) { |
| 101 |
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height), mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/* create a text box and add 6 letters code in it */ |
| 106 |
$textbox = imagettfbbox($font_size, 0, $font, $code); |
| 107 |
$x = ($image_width - $textbox[4])/2; |
| 108 |
$y = ($image_height - $textbox[5])/2; |
| 109 |
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code); |
| 110 |
|
| 111 |
|
| 112 |
/* Show captcha image in the page html page */ |
| 113 |
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow |
| 114 |
imagejpeg($image);//showing the image |
| 115 |
imagedestroy($image);//destroying the image instance |
| 116 |
$_SESSION['captcha_code'] = $code; |
| 117 |
|
| 118 |
function hexrgb ($hexstr) |
| 119 |
{ |
| 120 |
$int = hexdec($hexstr); |
| 121 |
|
| 122 |
return array("red" => 0xFF & ($int >> 0x10), |
| 123 |
"green" => 0xFF & ($int >> 0x8), |
| 124 |
"blue" => 0xFF & $int); |
| 125 |
} |
| 126 |
?> |