Ejemplo n.º 1
0
 /**
  * @param $args
  * GET: /misc/captcha
  */
 public function captcha($args)
 {
     $letters = 'qwertyuiopasdfghjklzxcvbnm1234567890';
     $captchaLen = 6;
     $width = 120;
     $height = 30;
     $font = $_SERVER['DOCUMENT_ROOT'] . Config::$SUB_FOLDER . "/content/fonts/consolas.ttf";
     $fontSize = 16;
     header('Content-type: image/png');
     $image = imagecreatetruecolor($width, $height);
     imagesavealpha($image, true);
     $bg = imagecolorallocate($image, 190, 190, 190);
     imagefill($image, 0, 0, $bg);
     $captcha = '';
     for ($i = 0; $i < $captchaLen; $i++) {
         $captcha .= $letters[rand(0, strlen($letters) - 1)];
         $x = ($width - 20) / $captchaLen * $i + 10;
         $x = rand($x, $x + 4);
         $y = $height - ($height - $fontSize) / 2;
         $curColor = imagecolorallocate($image, 255, 255, 255);
         //rand(0, 100), rand(0, 100), rand(0, 100));
         $angle = rand(-25, 25);
         imagettftext($image, $fontSize, $angle, $x, $y, $curColor, $font, $captcha[$i]);
     }
     Captcha::setCaptcha($args[0], $captcha);
     imagepng($image);
     imagedestroy($image);
 }