首页 > Web开发 > 详细

php简单生成验证码图片

时间:2015-01-19 12:39:44      阅读:216      评论:0      收藏:0      [点我收藏+]
<?php 
$i=imagecreatetruecolor(100, 30);
$iColor=imagecolorallocate($i, 240, 240, 240);
imagefill($i, 0, 0, $iColor);
$str="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPSDFGHJKLZXCVBNM";
$nStr=str_shuffle($str);
$fourStr=substr($nStr, 0,4);

for ($j=0; $j < strlen($fourStr); $j++) { 

    $s=substr($fourStr, $j,1);
    //随机颜色,尽量不要太浅(和底色白色有差别)
    $fColor=imagecolorallocate($i, mt_rand(0,150), mt_rand(0,150), mt_rand(0,150));
    //随机倾斜
    $angle=mt_rand(0,45);

    $x=8+$j*25;
    $y=25;
    //SIMYOU.TTF字体文件可以在C:\WINDOWS\Fonts下找到,多个字体也可以添加到一个数组中,然后随机提取
    imagefttext($i, 20, $angle, $x, $y, $fColor, "SIMYOU.TTF", $s);
}

//添加干扰线 (4条干扰线)
for ($j=0; $j < 4; $j++) { 
    $lColor=imagecolorallocate($i, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
    $x1=mt_rand(0,100);
    $y1=mt_rand(0,30);
    $x2=mt_rand(0,100);
    $y2=mt_rand(0,30);

    imageline($i, $x1, $y1, $x2, $y2, $lColor);
}


//加干扰点(200个干扰点)
for ($j=0; $j < 200; $j++) { 
    $pColor=imagecolorallocate($i, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
    $x=mt_rand(0,100);
    $y=mt_rand(0,30);
    imagesetpixel($i, $x, $y, $pColor);
}
header("content-type:image/png");
imagepng($i);

?>

 

php简单生成验证码图片

原文:http://www.cnblogs.com/xuxyblog/p/4233413.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!