Esempio n. 1
0
 /**  
  * 输出验证码并把验证码的值保存的session中  
  */
 public static function entry()
 {
     // 图片宽(px)
     self::$imageL || (self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize * 1.5);
     // 图片高(px)
     self::$imageH || (self::$imageH = self::$fontSize * 2);
     // 建立一幅 self::$imageL x self::$imageH 的图像
     self::$_image = imagecreate(self::$imageL, self::$imageH);
     // 设置背景
     imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]);
     // 验证码字体随机颜色
     self::$_color = imagecolorallocate(self::$_image, mt_rand(1, 120), mt_rand(1, 120), mt_rand(1, 120));
     // 验证码使用随机字体,保证目录下有这些字体集
     $ttf = dirname(__FILE__) . '/ttfs/t' . mt_rand(1, 3) . '.ttf';
     if (self::$useNoise) {
         // 绘杂点
         self::_writeNoise();
     }
     if (self::$useCurve) {
         // 绘干扰线
         self::_writeCurve();
     }
     // 绘验证码
     $code = array();
     // 验证码
     $codeNX = 0;
     // 验证码第N个字符的左边距
     for ($i = 0; $i < self::$length; $i++) {
         $code[$i] = self::$codeSet[mt_rand(0, 28)];
         $codeNX += mt_rand(self::$fontSize * 1.2, self::$fontSize * 1.6);
         imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 40), $codeNX, self::$fontSize * 1.5, self::$_color, $ttf, $code[$i]);
     }
     self::$code = join('', $code);
     // 输出图像
     imageJPEG(self::$_image);
     imagedestroy(self::$_image);
 }