zoukankan      html  css  js  c++  java
  • laravel

    完成白底黑字验证码,随机英文字母

    //后台
     /**
         * 生成图片验证码
         *
         */
        public function setPicture()
        {
            // session有效
            session_start();
            $code = '';
            //画布大小
            $image = imagecreate(200, 60);
            //创建前景色是黑色
            $color = imagecolorallocate($image, 0, 0, 0);
            //设置背景颜色
            $bg = imagecolorallocate($image, 255, 255, 255);
            //填充背景颜色
            imagefill($image, 0, 0, $bg);
            //清除缓冲区内容,这样即便有BOM头,也不会影响图片
            ob_clean();
            //输出一个图片类型为image/png的HTTP 报头
            header("Content-Type: image/png");
            //在图片上显示4个数字,在x轴上等距间隔,在y轴上保持高度一致。
            $length = 5;
            $random = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $arr = str_split($random);
    
            for ($i = 0; $i < $length; $i++) {
                //从数组中随意取值
                $num = array_rand($arr, 1);
                $str = $random[$num];
                $code .= $str;
            }
            //calibrib.ttf 为字体文件,可在电脑库里找到合适的字体文件
            $fontfile = 'website/public' . '/calibrib.ttf';
            imagettftext($image, 38, 0, 0, 45, $color, $fontfile, $code);
            $_SESSION['code'] = $code;//存储在session里
            //最后在网页在打印出带数字的图片
            imagepng($image);
        }
    
    //html
              <div class="code-image">
                   <image src="{{route('setpicture')}}" class="codeimage"></image>
              </div>
              <div class="code-refresh">
                    <a href="javascript:void(0);" onclick="onRefresh()">刷新</a>
              </div>
    
    //js
    <script>
     var srcurl = '{{route('setpicture')}}';
    /**
     * 刷新二维码
     *
     */
    function onRefresh() {
        $('.codeimage').attr('src', srcurl);
    }
    </script>
    
    
    //后台获取验证码
    //验证码判断对错
            session_start();
            $code = Input::get('code');
            $codeOrigin = $_SESSION['code'];
            if ($code != $codeOrigin) {
                return Response::json(['status' => 10, 'error' => '验证码错误']);
            }
    
  • 相关阅读:
    Representation Data in OpenCascade BRep
    Render OpenCascade Geometry Surfaces in OpenSceneGraph
    Render OpenCascade Geometry Curves in OpenSceneGraph
    OpenCascade Shape Representation in OpenSceneGraph
    Geometry Surface of OpenCascade BRep
    Geometry Curve of OpenCascade BRep
    Tyvj2017清北冬令营入学测试
    Spfa算法模板
    洛谷1016 旅行家的预算
    洛谷1290 欧几里得的游戏
  • 原文地址:https://www.cnblogs.com/gggggggxin/p/12186460.html
Copyright © 2011-2022 走看看