zoukankan      html  css  js  c++  java
  • 验证码(上)——创建验证码

    一、创建验证码函数

        验证码函数输入通用函数,将函数放入global.func.php里

        //创建一个四位数的字母数字混合随机码

        for($i=0;$i<4:$i++){

          $_nmsg .= dechex(mt_rand(0,9));

        }

        //将验证码保存到session里

        $SESSION['code']=$_nmsg;

        //设定验证码图片的长度和宽度

        $_width=75;

        $_height=25;

        //创建图片

        $_img = imagecreatetruecolor($_width,$_height);

        //创建一个白色

        $_white = imagecolorallocate($_img,255,255,255);

        //填充背景

        $imagefill($_img,0,0,$_white);

        //创建一个黑色的边框

        $_blach = imagecolorallocate($_img,100,100,100);

        imagerectangle($_img,0,0,$_width-1,$height-1,$_black);

        //添加随机线条

        for($i=0;$i<100;$i++){

          //创建随机色

          $_rnd_color = imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

          imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);

        }

        //添加随机雪花........数值越大,颜色越淡

        for($i=0;$i<100;$i++){    

          imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),"*",imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)))

        }

        //输出验证码

        for($i=0;$i<strlen($_SESSION['code']),$i++){

          //各参数意义imagestring(句柄,字体,平分四等分x轴方向,随机y轴方向位置,打印的字符,设置颜色)

          imagestring($_img,mt_rand(3,5),$i*$_width/4+mt_rand(1,10),

          mt_rand(1,$_height/2),$_SESSION['code']['$i'],

         imagecolorallocate($_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));  

        }

         //输出验证码

        header("Content-Type:image/png");

        imagepng($_img);

         //输出销毁

        imagedestroy($_img);

     二、使用到的函数(图像处理函数)

        1.dechex():  十进制转换为二进制;

        2.rand(min,max)与mt_rand(min,max): 返回min到max之间的随机整数;

           区别:mt_rand() 产生随机数值的平均速度比rand() 快四倍;

        3.imagecreatetruecolor()和imagecreate():

           区别

              用imagecreatetruecolor(int x,int y)建立的是一幅大小为 x和 y的黑色图像(默认为黑色),

              如想改变背景颜色则需要用填充颜色函数imagefill($img,0,0,$color),

              imagecreate() 新建一个空白图像资源,用imagecolorallocate()添加背景色


           这两个函数只不过是一个功能的两种方法

             具体用法见以下两种方法:

    <?php
        $img = imagecreatetruecolor(100,100);    //创建真彩图像资源
        $color = imagecolorAllocate($img,200,200,200);   //分配一个灰色
        imagefill($img,0,0,$color);                 // 从左上角开始填充灰色
        header('content-type:image/jpeg');   //jpg格式
        imagejpeg($img);                              //显示灰色的方块
    ?>
    <?php
        $img = imagecreate(100,100);
        $color = imagecolorallocate($img,200,200,200);
        header('content-type:image/jpeg');   //jpg格式
        imagejpeg($img);                              //显示灰色的方块
    ?>

       4.imagecolorallocate(resource $image , int $red , int $green , int $blue )—— 为一幅图像分配颜色

          imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。

       5.imagerectangle( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) —— 画一个矩形

          imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。

       6.imageline(resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color) ——画一条线段

          imageline() 用 color 颜色在图像 image 中从坐标 x1y1 到 x2y2(图像左上角为 0, 0)画一条线段。

       7.imagestring(resource $image , int $font , int $x , int $y , string $s , int $color) —— 水平地画一行字符串

          imagestring() 用 color 颜色将字符串 s 画到 image 所代表的图像的 xy 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。

          




  • 相关阅读:
    Linux学习笔记:【004】Linux内核代码风格
    ARM寄存器介绍
    2013 HTML5中国峰会演讲:Android上的HTML5:过去,现在和将来
    Ubuntu下编译SHTOOLS
    反对网抄,没有规则可以创建目标"install" 靠谱解答
    使用MTL库求解最小二乘解
    反对抄袭 正解spring的@Autowired 不要相信网上的错误版本
    浅析GDAL库C#版本支持中文路径问题(续)
    浅析GDAL库C#版本支持中文路径问题
    GDAL库调试(包括跨语言调试)
  • 原文地址:https://www.cnblogs.com/jytblog/p/7419466.html
Copyright © 2011-2022 走看看