php 图片验证码

php验证码程序 验证码 代码 php php 图片验证码

网络有众多的php随机数与验证码的代码与篇章,真正适用的从未有过多少个。

?php/*@date 20080514@author hluan

干脆本身搞三个吗。

//session_start();class icode{function __construct(){
header(‘Content-Type:image/png’);}function __destruct(){
imagedestroy($png);}function getcode($len,$type){ switch ($type){ case
1; $str = 1234567890; break; case 2; $str = abcdefghijklmnopqrstuvwxyz;
break; case 0; $str = 1234567890abcdefghijklmnopqrstuvwxyz; break; }
$result = ; $length = strlen($str) – 1; $num = 0; for
($i=0;$i$len;$i++){ $num = rand(0,$length); $a = $str[$num]; $result =
$result.$a; } /*Before using it,you should use session_start()*/
$_SESSION[‘icode’] = $result;//Store in session. //echo
session:.$_SESSION[‘icode’]; //die(); $png = imagecreate(60,30);
$white = imagecolorallocate($png,255,255,255);//background $red =
imagecolorallocate($png,255,0,0); $blue =
imagecolorallocate($png,0,0,255); $brown =
imagecolorallocate($png,100,0,0); $black =
imagecolorallocate($png,0,0,0);//Identifying Code
imagefill($png,0,0,$white); $k = rand(0,3); if (0 == $k){ for
($n=0;$n60;$n++){ $y = 15*sin($n/30*pi());
imagesetpixel($png,$n,15+$y,$red); } }else if (1 == $k){ for
($n=0;$n60;$n++){ $y = 15*cos($n/60*pi());
imagesetpixel($png,$n,15+$y,$red); } }else if (2 == $k){ for
($n=0;$n60;$n++){ $y = 15*sin($n/45*pi());
imagesetpixel($png,$n,15+$y,$brown); } }else if (3 == $k){ for
($n=0;$n60;$n++){ $y = 15*cos($n/30*pi());
imagesetpixel($png,$n,15+$y,$brown); } } for ($k=0;$k10;$k++){ $i =
rand(3,60);//width $j = rand(3,15);//height
imageline($png,$i-3,$j-3,$i,$j,$black); }
imagestring($png,5,3,5,$result,$blue); $icode = imagepng($png); return
$icode;}}/*测量试验代码$icode = new icode(卡塔尔(قطر‎;$icp = $icode-getcode(6,0卡塔尔;echo
$icp;*/

早先本节的php教程
吧,以下代码的贯彻,首要达成能够很好界别二个get_code(),另一个create_check_image(卡塔尔(قطر‎,输出图像直接调用前面包车型客车,session(卡塔尔取验证码时间接get_code(卡塔尔就ok,顺带提下使用session时必须定会将session_star(卡塔尔(قطر‎放在最前面。

代码如下:

复制代码 代码如下:
class RandCheckCode
{
        /*函数名称:get_code()
        *功能:得到自由字符串
        * 参数:
        1、(int)$length = 32 #自由字符长度
        2、(int)$mode = 0    #轻易字符类型,
        0为大小写Republika Hrvatska语和数字,1为数字,2为小写字母,3为大写字母,
        4为大小写字母,5为大写字母和数字,6为小写字母和数字
        *回到:拿到的字符串
        */
        function get_code($length=32,$mode=0卡塔尔国//获取随机验证码函数
        {
                switch ($mode)
                {
                        case ‘1’:
                                $str=’123456789′;
                                break;
                        case ‘2’:
                                $str=’abcdefghijklmnopqrstuvwxyz’;
                                break;
                        case ‘3’:
                                $str=’ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
                                break;
                        case ‘4’:
                               
$str=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz’;
                                break;
                        case ‘5’:
                               
$str=’ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890′;
                                break;
                        case ‘6’:
                               
$str=’abcdefghijklmnopqrstuvwxyz1234567890′;
                                break;
                        default:
                               
$str=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890′;
                                break;
                }
                $checkstr=”;
                $len=strlen($str)-1;
                for ($i=0;$i                {
                       
//$num=rand(0,$len卡塔尔;//发生一个0到$len之间的妄动数
                       
$num=mt_rand(0,$len卡塔尔;//发生三个0到$len之间的任性数
                        $checkstr.=$str[$num];

                      
                }
                return $checkstr;
        }

/**     函数名称:create_check_image()
        函数效用:产生三个校验码的图形
        参    数:$checkcode:校验码字符串
        返 回 值:重临该图片
*/
        function create_check_image($checkcodeState of Qatar//发生二个
        {
                $im=imagecreate(65,22卡塔尔(قطر‎;//爆发三个图纸
                $black=imagecolorallocate($im,0,0,0卡塔尔国;//背景颜色
                $white=imagecolorallocate($im,255,255,255卡塔尔(قطر‎;//前程颜色
                $gray=imagecolorallocate($im,200,200,200);
               
imagefill($im,30,30,$gray卡塔尔;//在$im图像的坐标30,30(图像左上角为0,0卡塔尔国处用$gray
颜色实行区域填充(即与30,30点颜色雷同且左近的点都会被填充State of Qatar

               
imagestring($im,5,8,3,$checkcode,$white卡塔尔国;//用$white颜色将字符串$checkcode画到$im
所代表的图像的8,3坐标处(那是字符串左上角坐标,整幅图像的左上角为0,0卡塔尔国,5是字体大小,
字体只可以是1,2,3,4或5,使用内置字体
                for ($i=0;$i<120;$i++)
                {
                       
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
                       
imagesetpixel($im,rand(卡塔尔国%70,rand(卡塔尔%30,$randcolorState of Qatar;//在$im图象上用$randcolor颜色在(rand(State of Qatar%70,rand(卡塔尔(قطر‎%30卡塔尔坐标(图像左上角为0,0State of Qatar上画一个点
                }
                header(“Content-type:image/png”);
                imagepng($imState of Qatar;//以PNG格式将图像输出到浏览器或文件
                imagedestroy($im卡塔尔国;//销毁图像$im
        }
}
/*
$randcode=new RandCheckCode();
$checkstring=$randcode->get_code(5,7);
$image=$randcode->create_check_image($checkstring);
echo $image;
*/

?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注