Beispiel #1
0
 protected function doCaptcha()
 {
     //Generate a number and save it in session
     $sCaptcha = (string) rand(1000, 9999);
     //replace "1" or "7" as it's difficult to make the difference
     for ($i = 0; $i < strlen($sCaptcha); $i++) {
         if ($sCaptcha[$i] == "1" || $sCaptcha[$i] == "7") {
             $sCaptcha[$i] = (string) rand(2, 6);
         }
     }
     AnwCurrentSession::setCaptcha($sCaptcha);
     //captcha settings
     $nMaxAngle = 20;
     $nFontSizeMin = 13;
     $nFontSizeMax = 14;
     $height = 20;
     $width = 80;
     $nCharSpace = 17;
     $font = ANWPATH_LIB . 'fonts/lazy_dog.ttf';
     $nNoisePxMin = 20;
     $nNoisePxMax = 40;
     header("Content-type: image/png");
     //Create image
     $im = imagecreate($width, $height);
     imagecolorallocate($im, 255, 255, 255);
     //white background
     //write numbers
     $len = strlen($sCaptcha);
     $x = 10;
     for ($i = 0; $i < $len; $i++) {
         $nTmpAngle = rand(0, 1) == 1 ? rand(0, $nMaxAngle) : rand(360 - $nMaxAngle, 360);
         //random angle
         $nTmpSize = rand($nFontSizeMin, $nFontSizeMax);
         //random size
         $nTmpTop = $height * 0.8 + rand(0, $height / 8);
         //random position from top
         $nTmpColor = imagecolorallocate($im, rand(0, 210), rand(0, 210), rand(0, 210));
         //random color
         imagettftext($im, $nTmpSize, $nTmpAngle, $x, $nTmpTop, $nTmpColor, $font, $sCaptcha[$i]);
         $x += $nCharSpace;
     }
     //add noise
     $nbpx = rand($nNoisePxMin, $nNoisePxMax);
     for ($i = 1; $i < $nbpx; $i++) {
         $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
         imagesetpixel($im, rand(0, $width - 1), rand(0, $height - 1), $color);
     }
     //Output image
     imagepng($im);
     imagedestroy($im);
     exit;
 }