PHP 变色验证码实例

PHP如何实现变色验证码,感兴趣的朋友可以参考下
 代码如下:
<?php 
header("Content-type: image/png,charset='utf-8'"); 
$im = imagecreatetruecolor(400, 30); 
//白色 
$white = imagecolorallocate($im, 255, 255, 255); 
//红色 
$red = imagecolorallocate($im, 255, 0, 0); 
//黑色 
$black=imagecolorallocate($im, 0, 0, 0); 
//绿色 
$green=imagecolorallocate($im, 0, 255, 0); 
//蓝色 
$blue=imagecolorallocate($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'; 
imagefilledrectangle($im, 0, 0, 399, 29, $white); 
// 整理: www.jbxue.com
for($i=0;$i<$textlen;$i++) 
{ 
if($i==$p1||$i==$p2) 
{ 
imagettftext($im, 15, 0, 20*($i-1)+20, 20, $color_arr[$color], $font, iconv_substr($text,$i,1,'utf-8')); 
} 
else 
{ 
imagettftext($im, 15, 0, 20*($i-1)+20, 20, $black, $font, iconv_substr($text,$i,1,'utf-8')); 
} 
} 
imagepng($im); 
imagedestroy($im); 
?>
验证码中的字符并不是同一种颜色,让用户输入指定颜色的验证码,这样安全性会更好的。

转载于:https://my.oschina.net/softsky/blog/201315

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值