php图片验证码制作

作者在 2010-09-19 10:53:42 发布以下内容
<?php
/*img.php
 * Created on 2009-2-9
 * Created by yunqing
 * yunqing's personal page:remindme.blogbus.com
 * yunqing's QQ:493568395
 * yunqing's Email:syq1357@qq.com
 */
    session_start();
    for($i=1;$i<=4;$i++){
         $rand.=dechex(rand(1,15));
    }
    $_SESSION['check_img']=$rand;
     $img=imagecreatetruecolor(100,30);
     imagecolorallocate($img,0,0,0);
     $color=imagecolorallocate($img,255,255,255);
     imagestring($img,rand(1,6),rand(3,70),rand(1,20),$rand,$color);
     header("Content-type:image/jpeg");//告诉浏览器输出的格式,目前浏览器的功能                                                                 //较强大,可省略
     imagejpeg($img);
?>

<?php
/*check.php
 * Created on 2009-2-9
 * Created by yunqing
 * yunqing's personal page:remindme.blogbus.com
 * yunqing's QQ:493568395
 * yunqing's Email:syq1357@qq.com
 */
 session_start();
if($_POST['check']){
    if($_POST['check']==$_SESSION['check_img']){
        echo "验证码正确".$_SESSION['check_img']."
";
        echo "<a href='check.php'>重新填写</a>";
    }else{
        echo "验证码错误".$_SESSION['check_img']."
";
        echo "<a href='check.php'>重新填写</a>";
    }
}else{
?>
  <form action="" method="post">
  <img src='img.php'>

  <input type="text" name="check">

  <input type="submit" name="sub" value="提交"/>
  </form>
<?php }?>
----------------------------------------------------------------
下面是修改过的可以显示中文验证码跟噪点的验证图片生成文件:
<?php
/*img.php
 * Created on 2009-2-9
 * Created by yunqing
 * yunqing's personal page:remindme.blogbus.com
 * yunqing's QQ:493568395
 * yunqing's Email:syq1357@qq.com
 */
    //打开SESSION
    session_start();
    //随机生成4个数字字母
    for($i=1;$i<=4;$i++){
         $rand.=dechex(rand(1,15));
    }
    //将生成的数字存入SESSION
    $_SESSION['check_img']=$rand;
    //生成一个图片画板
     $img=imagecreatetruecolor(100,30);
     //首次颜色调制作为画板背景色
     imagecolorallocate($img,0,0,0);
     //调制白色
     $white=imagecolorallocate($img,255,255,255);
     //调制随机色
     $color=imagecolorallocate($img,rand(1,255),rand(1,255),rand(1,255));
     //随机生成4条直线
     for($i=0;$i<4;$i++){
     imageline($img,0,0,rand(0,100),30,$color);
     imageline($img,100,30,rand(0,100),0,$color);
     }
     //随机生成200个点
     for($i=0;$i<200;$i++){
     imagesetpixel($img,rand(0,100),rand(0,30),$color);
     }
//     imagestring($img,rand(1,6),rand(3,70),rand(1,20),$rand,$white);//随机数字
                                                                    //字母显示
     //中文显示
     $icon=iconv('gbk','utf-8',"元宵快乐");
     imagettftext($img,12,0,rand(0,45),rand(10,30),$white,"simhei.ttf",$icon);
     //告诉浏览器什么格式
     header("Content-type:image/jpeg");
     //将PHP生成的二进制编码转换成图像编码
     imagejpeg($img);
PHP | 阅读 947 次
文章评论,共0条
游客请输入验证码