验证码介绍下PHP如何实现变色验证码。
代码:
<?php header("Content-type: p_w_picpath/png,charset='utf-8'"); $im = p_w_picpathcreatetruecolor(400, 30); //白色 $white = p_w_picpathcolorallocate($im, 255, 255, 255); //红色 $red = p_w_picpathcolorallocate($im, 255, 0, 0); //黑色 $black=p_w_picpathcolorallocate($im, 0, 0, 0); //绿色 $green=p_w_picpathcolorallocate($im, 0, 255, 0); //蓝色 $blue=p_w_picpathcolorallocate($im, 0, 0, 255); $color_arr=array($green,$blue,$red); $color=array_rand($color_arr); $text = '我靠这验证码太变态啦'; $textlen=iconv_strlen($text,'utf-8');//计算字符串长度 //随机截取两个字符,变色显示 $p1=rand(1,$textlen)-1; while(($p2=rand(1,$textlen)-1)==$p1); $w1=iconv_substr($text,$p1,1,'utf-8'); $w2=iconv_substr($text,$p1,1,'utf-8'); //字体文件 (PS:T不错的php Q扣峮:276167802,验证:csl) $font = 'simkai.ttf'; p_w_picpathfilledrectangle($im, 0, 0, 399, 29, $white); for($i=0;$i<$textlen;$i++) { // www.jbxue.com if($i==$p1||$i==$p2) { p_w_picpathttftext($im, 15, 0, 20*($i-1)+20, 20, $color_arr[$color], $font, iconv_substr($text,$i,1,'utf-8')); } else { p_w_picpathttftext($im, 15, 0, 20*($i-1)+20, 20, $black, $font, iconv_substr($text,$i,1,'utf-8')); } } p_w_picpathpng($im); p_w_picpathdestroy($im); ?>
验证码中的字符并不是同一种颜色,让用户输入指定颜色的验证码,这样安全性会更好的。