zoukankan      html  css  js  c++  java
  • 原生PHP生成验证码

    <?php
    //生成随机的字符串
        function get_str($len = 4)
        {
            $char = "1234567890abcdefghijklmnoqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ";
            $str = str_shuffle($char); //将字符串打乱
            $str = substr($str, 0, $len); //截取四位字符串
            return $str;
        }
    
    //验证码函数
        function img_code($width = 76, $height = 30)//1.定义验证码宽高默认值
        {
            
    //2.创建
            $img = imagecreatetruecolor($width, $height);
            
    //3.验证码添加背景颜色和文字颜色---imagecolorallcoate()
            $bgcolor = imagecolorallocate($img, 200, 200, 200);
            $textcolor = imagecolorallocate($img, 255, 0, 0);
            
    //4.指定图片上画矩形---imagefilledrectangle()
            imagefilledrectangle($img, 0, 0, $width, $height, $bgcolor);
            
    //5.将文字放入图片
            $code = get_str();
            imagestring($img, 5, 20, 5, $code, $textcolor);
            
    //6.图片上面加一些点
            for ($i = 0; $i < 30; $i++) {
                $dotcolor = imagecolorallocate($img, mt_rand(0, 155), mt_rand(0, 155), mt_rand(0, 155));
                imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), $dotcolor);
            }
            
    //7.画线
            for ($i = 0; $i < 2; $i++) {
                $linecolor = imagecolorallocate($img, mt_rand(0, 155), mt_rand(0, 155), mt_rand(0, 155));
                imageline($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $linecolor);
            }
            
    //将验证码存入session
            session_start();
            $_SESSION['imgcode'] = strtolower($code);
            
    //输出图像
            header('Content-Type:image/jpeg');
            imagejpeg($img);
            
    //销毁图像,释放内存
            imagedestroy($img);
        }
    //调用验证码函数查看验证码
        img_code();
  • 相关阅读:
    记一次诡异的调优
    java动态代理学习笔记
    c#反射机制学习和利用反射获取类型信息
    php开启ssl的方法
    关于java中split的使用
    c#使用反射调用类型成员示例
    C#关于反射加载的问题
    Twitter:使用Netty 4来减少GC开销
    Java中如何修改Jar中的内容
    Android中自定义视图View之---前奏篇
  • 原文地址:https://www.cnblogs.com/wxdindex/p/11265641.html
Copyright © 2011-2022 走看看