class CaptchaSecurityImages {
var $font = 'EAN-13.TTF';
function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
//$possible = '0123456789';
$code = '123456789';
//$i = 0;
//while ($i < $characters) {
// $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
// $i++;
//}
return $code;
}
function CaptchaSecurityImages($width='120',$height='40',$characters='13') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
// $noise_color = imagecolorallocate($image, 100, 120, 180);
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}
}
$captcha = new CaptchaSecurityImages($width,$height,$characters);
Viss būtu ok, itkā bracode izveido ar vērtību, kādu nodefinēju mainīgajam $code. Bet man vajadzētu, ka to $code vērtība tiktu ņemta no datu bāzēs.