总结
豆包 AI 助手文章总结

php如何实现随机数字和字母验证码的代码

不言
发布: 2018-07-13 09:03:58
原创
2495人浏览过

 php实现随机数字、字母的验证码

   可自定义生成验证码文字的大小、数量、干扰项等等,也可以自定义验证文字的字体。。。

  废话不多说,直接上代码:

<?php
Class Captcha{
        private $_fontfile='';
        private $_size=36;
        private $_width=200;
        private $_height=100;
        private $_length=4;
        private $_image=null;
        private $_snow=0;
        private $_pixel=0;
        private $_line=0;
    public function __construct($config=array()){
        if(is_array($config)&&count($config)>0){
            if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
                $this->_fontfile=$config['fontfile'];
            }else{
                return false;
            }
            if(isset($config['size'])&&$config['size']>0){
                $this->_size=(int)$config['size'];
            }
            if(isset($config['width'])&&$config['width']>0){
                $this->_width=(int)$config['width'];
            }
            if(isset($config['height'])&&$config['height']>0){
                $this->_height=(int)$config['height'];
            }
            if(isset($config['length'])&&$config['length']>0){
                $this->_length=(int)$config['length'];
            }
            if(isset($config['snow'])&&$config['snow']>0){
                $this->_snow=(int)$config['snow'];
            }
            if(isset($config['pixel'])&&$config['pixel']>0){
                $this->_pixel=(int)$config['pixel'];
            }
            if(isset($config['line'])&&$config['line']>0){
                $this->_line=(int)$config['line'];
            }
            $this->_image=imagecreatetruecolor($this->_width,$this->_height);
            return $this->_image;

         }
         else{
            return false;
        }
    }
    public function getCaptcha(){
        $white=imagecolorallocate($this->_image,255,255,255);
        imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white);
        $str=$this->_generateStr($this->_length);
        if(false===$str){
            return false;
        }
        $fontfile=$this->_fontfile;
        for($i=0;$i<$this->_length;$i++){
            $size=$this->_size;
            $angle=mt_rand(-30,30);
            $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
            $y=ceil($this->_height/1.5);
            $color=$this->_getRandColor();
            //针对中文字符截取
            //$text=mb_substr($str,$i,1,'utf-8');
            $text=$str{$i};
            imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);
        }
        if($this->_snow){
            $this->_getSnow();

        }else{
            if($this->_pixel){
                $this->_getPixel();

            }
            if($this->_line){
                $this->_getLine();
            }
        }
        header('content-type:image/png');
        imagepng($this->_image);
        imagedestroy($this->_image);
        return strtolower($str);
    }
    private function _getSnow(){
        for($i=1;$i<=$this->_snow;$i++){
            imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor());
        }
    }
    private function _getPixel(){
        for($i=1;$i<=$this->_pixel;$i++){
            imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
        }
    }
    private function _getLine(){
        for($i=1;$i<=$this->_line;$i++){
            imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());

        }
    }
    private function _generateStr($length=4){
        if($length<1 || $length>30){
            return false;
        }
        $chars=array(
            'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z',
            'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z',
            1,2,3,4,5,6,7,8,9
            );
        $str=join('',array_rand(array_flip($chars),$length));
        return $str;
    }
    private function _getRandColor(){
        return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    }
}
?>
登录后复制

  2、testCaptcha.php

<?php
require_once 'classgd.class.php';
$config=array(
'fontfile'=>'fonts/simfang.ttf',   //引入字体文件
//'snow'=>50,
'pixel'=>100,
'line'=>10
    );
$captcha=new Captcha($config);
$captcha->getCaptcha();
?>
登录后复制

立即学习PHP免费学习笔记(深入)”;

 以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

php如何重写session的存储机制

如何通过php来遍历文件夹获取图片目录名称及文件名

以上就是php如何实现随机数字和字母验证码的代码的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号