PHP实现变色验证码实例

yipeiwu_com5年前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);
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);
?>

验证码中的字符并不是同一种颜色,让用户输入指定颜色的验证码,这样安全性会更好的。

相关文章

基于php split()函数的用法详解

PHP函数split()的基本语法为:array split ( string $pattern, string $string [, int $limit] )。我们向大家举了两个例子...

浅谈PHP中类和对象的相关函数

浅谈PHP中类和对象的相关函数

class_exists 判断一个类是否存在,参数为一个名字! interface_exists 判断一个接口是否存在,参数也是为一个名字! method_exists 判断一个方...

php数组函数序列之next() - 移动数组内部指针到下一个元素的位置,并返回该元素值

next() 定义和用法 next() 函数把指向当前元素的指针移动到下一个元素的位置,并返回该元素的值。 如果内部指针已经超过数组的最后一个元素,函数返回 false。 语法 next...

php实现的生成排列算法示例

本文实例讲述了php实现的生成排列算法。分享给大家供大家参考,具体如下: <?php function perm($s, $n, $index) { if($n =...

php实现数组按拼音顺序排序的方法 原创

本文实例讲述了php实现数组按拼音顺序排序的方法。分享给大家供大家参考,具体如下: 一、问题: 给定数组要求实现按照其汉字首字母排序: $pinyin = array( arra...