本帖最后由 ziluoapo4 于 2014-10-12 17:33:16 编辑 一般验证码不显示,无处乎是因为:1 有
修改了一下,ok了,主要是字体那里,你没有定义ROOT_PATH,导致获取不到 $this->font = ROOT_PATH.'/font/elephant.ttf';
<br>//验证码类<br>define('ROOT_PATH', dirname(__FILE__));<br><br>//验证码类<br>class ValidateCode {<br> private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子<br> private $code; //验证码<br> private $codelen = 4; //验证码长度<br> private $width = 130; //宽度<br> private $height = 50; //高度<br> private $img; //图形资源句柄<br> private $font; //指定的字体<br> private $fontsize = 20; //指定字体大小<br> private $fontcolor; //指定字体颜色<br> <br> //构造方法初始化<br> public function __construct() {<br> $this->font = ROOT_PATH.'/font/elephant.ttf';<br> }<br> <br> //生成随机码<br> private function createCode() {<br> $_len = strlen($this->charset)-1;<br> for ($i=0;$icodelen;$i++) {<br> $this->code .= $this->charset[mt_rand(0,$_len)];<br> }<br> }<br> <br> //生成背景<br> private function createBg() {<br> $this->img = imagecreatetruecolor($this->width, $this->height);<br> $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));<br> imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);<br> }<br> <br> //生成文字<br> private function createFont() { <br> $_x = $this->width / $this->codelen;<br> for ($i=0;$icodelen;$i++) {<br> $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));<br> imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);<br> }<br> }<br> <br> //生成线条、雪花<br> private function createLine() {<br> for ($i=0;$i $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));<br> imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);<br> }<br> for ($i=0;$i
登入後複製