zoukankan      html  css  js  c++  java
  • php 验证码

     验证码1 需要的背景图片 根据具体情况换代码中路径 第一个可能会出现问题 是找找背景图片的路径问题

    <?php
    
    /**
     * Created by PhpStorm.
     * User: DY040
     * Date: 2018/4/24
     * Time: 10:42
     * <?php 置于最左上
     * ?> 不要有结束标记
     * 不要包含其他输出
     * 不要有html
     *
     */
    class Captchatool
    {
        function __construct()
        {
            $this->imageCode1();
    //        var_dump(111);
        }
    
        public function imageCode1($len = 4, $sess = 'code')
        {
    
            @session_start();
            $n = $len;
            $mes = '';
            /*创建随机验证码 保存session中*/
            for ($i = 0; $i < $n; $i++) {
                $mes .= dechex(mt_rand(0, 15));
    
            }
    
            $_SESSION[$sess] = $mes;
    
    
            $bg_file = ROOT_PATH . 'Framework/tool/img/captcha_bg' . mt_rand(1, 5) . '.jpg';//背景图片
            //1创建画布·······························································
            $_img = imagecreatefromjpeg($bg_file);
    
            $img_w = imagesx($_img); //图宽
            $img_h = imagesy($_img);
            $font_size = 5;
            $font_w = imagefontwidth($font_size);
            $font_h = imagefontheight($font_size);
    
    
            //2操作画布·······························································
            //将字符串写入函数
    //    imagestring(画布,大小,x,y,内容,颜色)
            for ($i = 0; $i < strlen($mes); $i++) {
                $_rand_color = imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
    
                imagestring(
                    $_img,
                    $font_size,
                    ($img_w - $n * $font_w) / 2 + $font_w * $i,//x偏移量
                    mt_rand(0, 7),
                    $mes[$i],
                    $_rand_color);
    
            }
    
    //    3 导出画布
    
            header("Content-Type:image/jpeg");
            imagejpeg($_img);
    //    4销毁画布
            imagedestroy($_img);
        }
    
        public function imageCode2($n = 4, $sess = 'code', $_width = 75, $_height = 25)
    
        {
            @session_start();
            $mes = '';
            /*创建随机验证码 保存session中*/
    
            for ($i = 0; $i < $n; $i++) {
                $mes .= dechex(mt_rand(0, 15));
    
            }
    
            $_SESSION[$sess] = $mes;
    
            /*长和高*/
    
            /*创建图像*/
            $_img = imagecreatetruecolor($_width, $_height);
            /*画背景颜色*/
            $_white = imagecolorallocate($_img, 255, 255, 255);
            /*填充*/
            imagefill($_img, 0, 0, $_white);
            $_flag = false;
            if ($_flag) {
                /*画边框 黑色*/
                $_black = imagecolorallocate($_img, 0, 0, 0);
                imagerectangle($_img, 0, 0, $_width - 1, $_height - 1, $_black);
            }
    //随机画出线条
            for ($i = 0; $i < 8; $i++) {
                $_rand_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),
                    $_rand_color
                );
    
            }
    
            /*随机雪花*/
            for ($i = 0; $i < 80; $i++) {
                $_rand_color = imagecolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
                /*参数2 字体大小 1-5*/
                imagestring(
                    $_img,
                    1,
                    mt_rand(1, $_width),
                    mt_rand(1, $_height),
                    '#',
                    $_rand_color
                );
    
            }
            /*输出验证码数字*/
            for ($i = 0; $i < strlen($_SESSION['code']); $i++) {
                /*获取随机颜色*/
                $_rand_color = imagecolorallocate(
                    $_img,
                    mt_rand(0, 100),
                    mt_rand(0, 150),
                    mt_rand(0, 200)
                );
                imagestring(
                    $_img,
                    5,
                    $i * $_width / strlen($_SESSION['code']), //每个验证码左偏移量
                    $_height / mt_rand(2, 10),//上偏移量
                    $_SESSION['code'][$i],
                    $_rand_color
                );
    
    
            }
            /*输出图像*/
            header("Content-Type:image/png");
            imagepng($_img);
    //销毁图像
            imagedestroy($_img);
        }
    
        public function check_code($code)
        {
            @session_start();
            if ($code === $_SESSION['code']) {
                unset($_SESSION['code']);
                return true;
            } else {
                unset($_SESSION['code']);
                return false;
            }
    
    
        }
    }
    
    //$a=new Captchatool();
    //$a->imageCode1();
    //var_dump('我是验证码工具类');
  • 相关阅读:
    Scala并发编程react、loop代码实战具体解释
    Linux内核通知链模块
    STL栈的应用之表达式求值
    ansi 控制码表及颜色代码
    关于substring的char[]共享
    jQuery几个经典表单应用整理回想
    ShareSDK for Android 2.3.10已经公布
    cocos2dx坐标系
    一种Android数据请求框架
    css 小问题解决方法整理
  • 原文地址:https://www.cnblogs.com/aqigogogo/p/8953230.html
Copyright © 2011-2022 走看看