PHP+验证码制作

验证码<captcha>

提示

1.在制作验证码之前请确认PHP的GD库(图像处理库)是否打开。(phpinfo()查看)
2.在输出图像之前,一定要在先设置header('content-type: image/png')
3.在给验证码字符颜色取值的时候一般R、G、B的取值在0-120之间(深色区间)(值越大颜色越浅)
4.再出现图片显示错误无法显示的情况下,请先屏蔽header和输出图片的语句,然后运行检查自己是否有用echo函数输出其他内容,或者程序报错。

GD库文档

http://php.net/manual/zh/book.image.php

GD库函数

imagecreatetruecolor( width, height);

说明:创建一张图片
$width:宽
$height:高
返回值:返回图片资源句柄

imagecolorallocate( image, red, green, blue);

说明:根据图像来构建颜色,对于用 imagecreate() 建立的图像,第一次调用 imagecolorallocate() 会自动给图像填充背景色
$image:图片资源句柄
$red:红色比例(0-255)
$green:绿色比例(0-255)
$blue:蓝色比例(0-255)
返回值:返回颜色值

imagefill( image, x, y, color)

说明:为图像填充颜色(x,y 分别为填充的起始 x 坐标和 y 坐标,与 x, y 点颜色相同且相邻的点都会被填充。 )
$image:图片资源句柄
$x:x坐标
$y:y坐标
$color:指定的颜色

imagepng($image)

说明:输出png图片
$image:图片资源句柄

imagedestroy($image)

说明:销毁图片资源
$image:图片资源句柄

imagestring( image, font, x, y, string, color)

说明:在图像上绘出文字,不支持中文
$image:图片资源句柄
$font:字体类型(如果是数字就使用内置字体,数字代表大小)
$x:x坐标
$y:y坐标
$string:要绘出的文字
$color:文字的颜色

imagesetpixel( image, x, y, color)

说明:在图像指定点上绘制指定颜色
$image:图片资源句柄
$x:x坐标
$y:y坐标
$color:指定的颜色

imageline( image, x1, y1, x2, y2, color)

说明:在图像上绘制线段
$image:图片资源句柄
$x1:起点x1坐标
$y1:起点y1坐标
$x2:终点x2坐标
$y2:终点y2坐标
$color:指定的颜色

SESSION存储验证信息

提示:
1.必须在脚本最顶部调用session_start()函数
2.多服务器情况,需要考虑集中管理session信息
3.用$_SESSION全局变量来存储验证信息,下次直接读就行了。

汉字验证码实现

array imagettftext ( resource image,float size , float angle,int x , int y,int color , string fontfile,string text )
说明:使用 TrueType 字体将 指定的 text 写入图像,可以调整角度。 
$fontfile:这个是中文的TTF文件所在路径(就是文字文件,百度有,系统也有自带的)

实现代码

index.php

这里用中文验证码做示例,可以自己更改生成验证码图片的地址
所有生成验证码图片的函数都会自动输出验证码图片并且根据你提供的session_key存储到$_SESSION[session_key]中,并在最后返回他绘制在验证码上的文字。

<?php
    session_start();
    if(isset($_GET['captcha'])){
        echo $_GET['captcha'];
        echo '<br\>';
        echo  $_SESSION['captcha'];
        if($_GET['captcha'] == $_SESSION['captcha']){
            echo "<script>alert('ok')</script>";
        }else{
            echo "<script>alert('no')</script>";
        }
    }
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <p>
            <image src="ChinaCaptcha.php"/>
        </p>
        <form action="" method="get">
            <input name="captcha" type="text"/>
            <input type="submit" />
        </form>
    </body>
</html>

获取中文验证码

<?php
session_start();
define("PIC_WIDTH", 200);
define("PIC_HEIGHT", 100);
define("PIC_NUM_WORD",4);
define("PIC_NUM_POINT",200);//干扰点数量
define("PIC_NUM_LINE",6);//干扰线数量
getChinaCaptcha("captcha");
//-获取中文验证码
 function getChinaCaptcha($session_key){
     $image = imagecreate(PIC_WIDTH, PIC_HEIGHT);
     $color = imagecolorallocate($image, 255, 255, 255);//生成颜色
     imagefill($image, 0, 0, $color);//填充背景
     setBackgroundPoint($image);
     setBackgroundLine($image);
     $captcha = "";
     $str = "今晚低女村民诶哦啊脾胃去啊杀了你发洼大王";
     $strArr = str_split($str,3);
     for($i=0;$i<PIC_NUM_WORD;$i++){
         $rd = $strArr[rand(0, count($strArr)-1)];
         $captcha = $captcha.$rd;
         $fontColor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0, 120));
         $fontSize = rand(20,24);
         $angle = rand(-60,60);
         $x =($i * PIC_WIDTH / PIC_NUM_WORD) + rand(0,10);
         $y = rand($fontSize,PIC_HEIGHT - $fontSize);
         $filePath = "simsun.ttc";
         imagettftext($image, $fontSize, $angle, $x,$y, $fontColor, $filePath, $rd);     
     }
     header("content-type: image/png");
     imagepng($image);
     $_SESSION[$session_key] = $captcha;
     return $captcha;
 }

 function setBackgroundPoint($image){
    for ($i = 0;$i < PIC_NUM_POINT;$i++){
        $color = imagecolorallocate($image, rand(100,220), rand(100,220), rand(100,220));
        imagesetpixel($image, rand(0,PIC_WIDTH), rand(0,PIC_HEIGHT), $color);
    }
}

function setBackgroundLine($image){
    for ($i = 0;$i < PIC_NUM_LINE;$i++){
        $color = imagecolorallocate($image, rand(100,220), rand(100,220), rand(100,220));
        imageline($image,rand(0,PIC_WIDTH) ,rand(0,PIC_HEIGHT) , rand(0,PIC_WIDTH), rand(0,PIC_HEIGHT), $color);
    } 
}  
?>

获取纯数字验证码

<?php
session_start();
define("PIC_WIDTH", 100);
define("PIC_HEIGHT", 50);
define("PIC_NUM_WORD",4);
define("PIC_NUM_POINT",200);//干扰点数量
define("PIC_NUM_LINE",6);//干扰线数量
getNumberCaptcha("captcha");
//-获取数字验证码
function getNumberCaptcha($session_key){
    $image = imagecreate(PIC_WIDTH, PIC_HEIGHT);
    $color = imagecolorallocate($image, 255, 255, 255);//生成颜色
    imagefill($image, 0, 0, $color);//填充背景
    setBackgroundPoint($image);
    setBackgroundLine($image);
    $captcha = "";
    for($i=0;$i<PIC_NUM_WORD;$i++){
        $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0, 120));
        $font = 5;
        $x =($i * PIC_WIDTH / PIC_NUM_WORD) + rand(0,10);
        $y = rand(0,PIC_HEIGHT / 2);
        $rd = rand(0,9);
        $captcha = $captcha.$rd;
        imagestring($image, $font, $x, $y,$rd , $fontcolor);
    }
    header("content-type: image/png");
    imagepng($image);
    $_SESSION[$session_key] = $captcha;
    return $captcha;
}


function setBackgroundPoint($image){
    for ($i = 0;$i < PIC_NUM_POINT;$i++){
        $color = imagecolorallocate($image, rand(100,220), rand(100,220), rand(100,220));
        imagesetpixel($image, rand(0,PIC_WIDTH), rand(0,PIC_HEIGHT), $color);
    }
}

function setBackgroundLine($image){
    for ($i = 0;$i < PIC_NUM_LINE;$i++){
        $color = imagecolorallocate($image, rand(100,220), rand(100,220), rand(100,220));
        imageline($image,rand(0,PIC_WIDTH) ,rand(0,PIC_HEIGHT) , rand(0,PIC_WIDTH), rand(0,PIC_HEIGHT), $color);
    } 
}

获取字母数字混合验证码

<?php
session_start();
define("PIC_WIDTH", 100);
define("PIC_HEIGHT", 50);
define("PIC_NUM_WORD",4);
define("PIC_NUM_POINT",200);//干扰点数量
define("PIC_NUM_LINE",6);//干扰线数量

getNumberWordCaptcha("captcha");
//-获取数字与字母混合验证码
function getNumberWordCaptcha($session_key){
    $image = imagecreate(PIC_WIDTH, PIC_HEIGHT);
    $color = imagecolorallocate($image, 255, 255, 255);//生成颜色
    imagefill($image, 0, 0, $color);//填充背景
    setBackgroundPoint($image);
    setBackgroundLine($image);
    $captcha = "";
    for($i=0;$i<PIC_NUM_WORD;$i++){
        $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0, 120));
        $font = 5;
        $x =($i * PIC_WIDTH / PIC_NUM_WORD) + rand(0,10);
        $y = rand(0,PIC_HEIGHT / 2);
        $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $rd =substr($str, rand(0, strlen($str)),1);
        $captcha = $captcha.$rd;
        imagestring($image, $font, $x, $y,$rd , $fontcolor);
    }
    header("content-type: image/png");
    imagepng($image);
    $_SESSION[$session_key] = $captcha;
    return $captcha;
}

function setBackgroundPoint($image){
    for ($i = 0;$i < PIC_NUM_POINT;$i++){
        $color = imagecolorallocate($image, rand(100,220), rand(100,220), rand(100,220));
        imagesetpixel($image, rand(0,PIC_WIDTH), rand(0,PIC_HEIGHT), $color);
    }
}

function setBackgroundLine($image){
    for ($i = 0;$i < PIC_NUM_LINE;$i++){
        $color = imagecolorallocate($image, rand(100,220), rand(100,220), rand(100,220));
        imageline($image,rand(0,PIC_WIDTH) ,rand(0,PIC_HEIGHT) , rand(0,PIC_WIDTH), rand(0,PIC_HEIGHT), $color);
    } 
}    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值