font='./static/simhei.ttf'; $this->code_len=4; $this->width=80; $this->height=32; $this->font_size=20; $this->charset='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; } //生成随机码 private function createCode() { $_len = strlen($this->charset)-1; for ($i=0;$i<$this->code_len;$i++) { $this->code .= $this->charset[mt_rand(0,$_len)]; } } //生成背景 private function createBg() { $this->img = imagecreatetruecolor($this->width, $this->height); $color = imagecolorallocate($this->img,238,238,238); imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); } //生成字体 private function createFont() { $_x = $this->width / $this->code_len; for ($i=0;$i<$this->code_len;$i++) { $this->font_color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imagettftext($this->img,$this->font_size,0,$_x*$i+5,$this->height / 1.2,$this->font_color,$this->font,$this->code[$i]); } } //输出 private function outPut() { header('Content-type:image/png'); imagepng($this->img); imagedestroy($this->img); } //对外生成 public function doimg() { $this->createBg(); $this->createCode(); $this->createFont(); Session::set("img_verify",md5($this->getCode())); $this->outPut(); } //获取验证码 public function getCode() { return strtolower($this->code); } } ?>