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

    <?php
    //仿制ecshop验证码(四位大写字母和数字、背景)
    //处理码值(四位大写字母和数字组成)
    //所有的可能的字符集合
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $chars_len = strlen($chars); //集合长度
    //随机选取
    $code_len = 4;//验证码长度
    $code=''; //验证码值初始化
    for($i=0;$i<$code_len;++$i){
        //随机取得一个字符下标
        $rand_index = mt_rand(0,$chars_len-1);
        //利用字符串的下标操做,获得选择的字符
        $code .= $chars[$rand_index];
    }
    //echo $code;
    //存储于session中(用于校验)
    session_start();
    $_SESSION['code'] = $code;
    
    //验证码图像(已知的背景图片)
    //处理背景
    $bg_file= './captcha/captcha_bg' . mt_rand(1,5). '.jpg';
    //依据该图片,创建画布
    $image = imagecreatefromjpeg($bg_file);
    //简单的将字符串写在画布上的函数(imageString();)
    //imageString(画布,字体,位置X, 位置y,字符串内容,颜色); 
    //字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();
    
    //分配字体颜色(随机分配黑色或者白色)
    if(mt_rand(0,1)==1){
            $str_color = imagecolorallocate($image,0,0,0); //黑色
        }else{
            $str_color = imagecolorallocate($image,255,0xff,255);//白色
    }
    //内置5号字体
    $font = 5;
    //位置
    //画布大小
    $image_w = imagesx($image);
    $image_h = imagesy($image);
    //获得字体的宽和高
    $font_w = imagefontwidth($font);
    $font_h = imagefontheight($font);
    //获得字符串的宽高
    $str_w = $font_w * $code_len;
    $str_h = $font_h;
    //计算位置
    $str_x = ($image_w-$str_w) / 2;
    $str_y = ($image_h-$str_h) / 2;
    
    //字符串
    imagestring($image,$font,$str_x,$str_y,$code,$str_color);
    
    //输出和销毁画布
    header("content-type:image/jpeg");
    imagejpeg($image);
    imagedestroy($image);

    封装验证码工具类:

    <?php
    //验证码工具类(将所有和验证码操作相关的全部封装到该类中)
            class Captcha{
                /*生成验证码*/
            public function makeImage($code_len=4){
            //仿制ecshop验证码(四位大写字母和数字、背景)
            //处理码值(四位大写字母和数字组成)
            //所有的可能的字符集合
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            $chars_len = strlen($chars); //集合长度
            //随机选取
            $code=''; //验证码值初始化
            for($i=0;$i<$code_len;++$i){
                //随机取得一个字符下标
                $rand_index = mt_rand(0,$chars_len-1);
                //利用字符串的下标操做,获得选择的字符
                $code .= $chars[$rand_index];
            }
            //echo $code;
            //存储于session中(用于校验)
            @session_start();
            $_SESSION['code'] = $code;
    
            //验证码图像(已知的背景图片)
            //处理背景
            $bg_file=  TOOL . './captcha/captcha_bg' . mt_rand(1,5). '.jpg';
            //依据该图片,创建画布
            $image = imagecreatefromjpeg($bg_file);
            //简单的将字符串写在画布上的函数(imageString();)
            //imageString(画布,字体,位置X, 位置y,字符串内容,颜色); 
            //字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();
    
            //分配字体颜色(随机分配黑色或者白色)
            if(mt_rand(0,1)==1){
                    $str_color = imagecolorallocate($image,0,0,0); //黑色
                }else{
                    $str_color = imagecolorallocate($image,255,0xff,255);//白色
            }
            //内置5号字体
            $font = 5;
            //位置
            //画布大小
            $image_w = imagesx($image);
            $image_h = imagesy($image);
            //获得字体的宽和高
            $font_w = imagefontwidth($font);
            $font_h = imagefontheight($font);
            //获得字符串的宽高
            $str_w = $font_w * $code_len;
            $str_h = $font_h;
            //计算位置
            $str_x = ($image_w-$str_w) / 2;
            $str_y = ($image_h-$str_h) / 2;
    
            //字符串
            imagestring($image,$font,$str_x,$str_y,$code,$str_color);
            //输出和销毁画布
            header("content-type:image/jpeg");
            imagejpeg($image);
            imagedestroy($image);
        }
    }
  • 相关阅读:
    STM32 F4 DAC DMA Waveform Generator
    STM32 F4 General-purpose Timers for Periodic Interrupts
    Python第十四天 序列化 pickle模块 cPickle模块 JSON模块 API的两种格式
    Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块
    查看SQL Server服务运行帐户和SQL Server的所有注册表项
    Pycharm使用技巧(转载)
    SQL Server 2014内存优化表的使用场景
    Python第十天 print >> f,和fd.write()的区别 stdout的buffer 标准输入 标准输出 从控制台重定向到文件 标准错误 重定向 输出流和输入流 捕获sys.exit()调用 optparse argparse
    Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
    Python第六天 类型转换
  • 原文地址:https://www.cnblogs.com/yangzailu/p/6255581.html
Copyright © 2011-2022 走看看