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 52
| <?php
session_start();
if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/');
function getCode($length) {
$chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$rand_str = '';
for ($i=0; $i<$length; $i++) {
$rand_str .= $chars{ mt_rand( 0, strlen($chars)-1 ) };
}
return $rand_str;
}
$theCode = getCode(5);
$_SESSION['captcha'] = md5($theCode);
$char1 = substr($theCode,0,1);
$char2 = substr($theCode,1,1);
$char3 = substr($theCode,2,1);
$char4 = substr($theCode,3,1);
$char5 = substr($theCode,4,1);
$fonts = glob('fonts/*.ttf');
$image = imagecreatefrompng('captcha.png');
$colors=array ( imagecolorallocate($image, 131,154,255),
imagecolorallocate($image, 89,186,255),
imagecolorallocate($image, 155,190,214),
imagecolorallocate($image, 255,128,234),
imagecolorallocate($image, 255,123,123) );
function random($tab) {
return $tab[array_rand($tab)];
}
imagettftext($image, 28, -10, 0, 37, random($colors), ABSPATH .'/'. random($fonts), $char1);
imagettftext($image, 28, 20, 37, 37, random($colors), ABSPATH .'/'. random($fonts), $char2);
imagettftext($image, 28, -35, 55, 37, random($colors), ABSPATH .'/'. random($fonts), $char3);
imagettftext($image, 28, 25, 100, 37, random($colors), ABSPATH .'/'. random($fonts), $char4);
imagettftext($image, 28, -15, 120, 37, random($colors), ABSPATH .'/'. random($fonts), $char5);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?> |