深圳市一览网络股份有限
公司
ShenZhen Elanw Network
Co.,Ltd.
地址:南山科技园南区数字技术园
B2 栋 4A
Add: Room A,4th Floor,Block
B2,Digi-Tech Park ,
电话(Tel):0755-86133725
传真(Fax):0755-22632616
网址
(Web):www.YL1001.com
验证码想必大家都有见到过吧,在本文为大
家介绍下
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
);
?>