zoukankan      html  css  js  c++  java
  • php简单实用的验证码生成类

     1 <?php
     2 class Captcha {
     3     public $width = 100;
     4     public $height = 20;
     5     public $code_len = 4;
     6 
     7     public $pixel_number = 100;
     8 
     9     private function _mkCode() {
    10         // 有4位,大写字母和数字组成
    11         // 思路,将所有的可能性,做一个集合。随机(依据某种条件)进行选择。
    12         // 所有的可能的字符集合
    13         $chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
    14         $chars_len = strlen($chars);// 集合长度
    15         // 随机选取
    16         $code = '';// 验证码值初始化
    17         for($i=0; $i<$this->code_len; ++$i) {
    18             // 随机取得一个字符下标
    19             $rand_index = mt_rand(0, $chars_len-1);
    20             // 利用字符串的下标操作,获得所选择的字符
    21             $code .= $chars[$rand_index];
    22         }
    23         // 存储于session中
    24         @session_start();
    25         $_SESSION['captcha_code'] = $code;
    26         return $code;
    27 
    28     }
    29     /**
    30      * 生成验证码图片
    31      * @param int $code_len 码值长度
    32      */
    33     public function makeImage() {
    34         // 一:处理码值,这里我们添加了一个函数 _mkCode()生成随机的验证码
    35         $code = $this->_mkCode();
    36 
    37         // 二,处理图像
    38         // 创建画布
    39         $image = imagecreatetruecolor($this->width, $this->height);
    40         // 设置背景颜色[背景就会是随机]
    41         $bg_color = imagecolorallocate($image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
    42         // 填充背景
    43         imagefill($image, 0, 0, $bg_color);
    44 
    45         // 分配字体颜色,随机分配,黑色或者白色
    46         if (mt_rand(0, 1) == 1) {
    47             $str_color = imagecolorallocate($image, 0, 0, 0);// 黑色
    48         } else {
    49             // 整数支持 不同进制表示
    50             $str_color = imagecolorallocate($image, 255, 0xff, 255);// 白色
    51         }
    52         // 内置5号字体
    53         $font = 5;
    54         // 位置
    55         // 画布大小
    56         $image_w = imagesx($image);
    57         $image_h = imagesy($image);
    58         // 字体宽高
    59         $font_w = imagefontwidth($font);
    60         $font_h = imagefontheight($font);
    61         // 字符串宽高
    62         $str_w = $font_w * $code_len;
    63         $str_h = $font_h;
    64         // 计算位置[把我们字符串在中间]
    65         $str_x = ($image_w - $str_w) / 2 - 20;
    66         $str_y = ($image_h-$str_h) / 2;
    67         // 字符串
    68         imagestring($image, $font, $str_x, $str_y, $code, $str_color);
    69 
    70         // 设置干扰
    71         for($i=1; $i<=$this->pixel_number; ++$i) {
    72             $color = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
    73             imagesetpixel($image, mt_rand(0, $this->width-1), mt_rand(0,$this->height-1), $color);
    74         }
    75 
    76         // 输出,销毁画布
    77         //ob_clean(),清空缓存,保证正确输出.
    78         ob_clean();
    79         header('Content-Type: image/jpeg');
    80         imagejpeg($image);
    81         imagedestroy($image);
    82     }
    83 
    84 }
  • 相关阅读:
    开发两年!JVM方法调用都玩不明白,你离被炒鱿鱼不远了!
    springboot基本框架搭建零基础教程,对新手极为友好!
    SpingBoot整合Mybatis,这些小技巧你得知道,对你工作有很大的帮助!
    今天我们基于jdk8聊聊JVM-常量池,希望对大家有帮助!
    剑指 Offer 12. 矩阵中的路径
    WUSTCTF2020 funnyre
    2020 DJBCTF RE wp
    黑马c++基础的一个通讯录系统
    elf文件结构解读以及plt节got节的理解
    ubuntu16.04上编译android的可执行文件并调用本地so库
  • 原文地址:https://www.cnblogs.com/ggkxg/p/5679052.html
Copyright © 2011-2022 走看看