博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php自定义验证码图片大小且可点击图片刷新验证码
阅读量:4323 次
发布时间:2019-06-06

本文共 2148 字,大约阅读时间需要 7 分钟。

<?php

class ValidateCode{
    public $charset="abcdefghjkmnpqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789";
    public $codelength=9;//验证码长度
    public $code;//验证码
    public $img;//画布
    public $width;
    public $height;
    public $fontttf='SIMSUN.TTC';//字体可从C:\WINDOWS\Fonts下复制
    public $fontSize=40;
    //生成随机验证码
    public function createCode(){
        $length=strlen($this->charset);
        for($i=0;$i<$this->codelength;$i++){
            $this->code.=$this->charset[mt_rand(0,$length-1)];
        }
        return $this->code;
    }
 
    //创建画布
    public function createBg(){
        header('Content-Type:image/png');
        $this->img=imagecreatetruecolor($this->width,$this->height);
        $white=imagecolorallocate($this->img, 255, 255, 255);        
        //填充背景(白色)
        imagefill($this->img, 0, 0, $white);
        //干扰线
        for($i=0;$i<6;$i++){
            $linecolor=imagecolorallocate($this->img, rand(50, 200), rand(100, 200), rand(150, 200));
            imagearc($this->img,mt_rand(-$this->width,$this->width),mt_rand(-$this->height,$this->height),mt_rand(30,$this->width*2),mt_rand(20,$this->height*2),mt_rand(0,360),mt_rand(0,360),$linecolor);
        }
        //干扰点
        for($i=0;$i<100;$i++){
            $charcolor=imagecolorallocate($this->img, rand(200, 250), rand(200, 250), rand(200, 250));
            imagechar($this->img, $this->fontSize, rand(0,$this->width), rand(0,$this->height), '*', $charcolor);
        }
        //验证码插入图片
        imagefttext($this->img, $this->fontSize, 0, 4, $this->fontSize+3, imagecolorallocate($this->img, rand(0, 100), rand(0, 150), rand(0, 200)), $this->fontttf,$this->code);
        imagepng($this->img);
        imagedestroy($this->img);
    }
    //返回验证码
    public function getCode(){
        return $this->code;
    }
    //验证函数
    public function checkCode(){
        !$this->width && $this->width= $this->codelength*$this->fontSize*4/5 +5;
        !$this->height && $this->height=$this->fontSize+10;
        $this->createCode();
        $this->createBg();
    }  
}
?>

将上面的代码复制保存为ValidateCode.php

新建一个code.php文件,代码如下

<?php

require './ValidateCode.php';
session_start();
$code=new ValidateCode();
$code->codelength=5;//验证码数量
$code->fontSize=20;//验证码大小
$code->checkCode();
$_SESSION['code']=strtolower($code->getCode());//将验证码保存到session中
?>

在html中引入验证码图片,可点击图片刷新验证码

<html>

    <img src="code.php" οnclick="this.src='code.php?'+Math.random()"/>
</html>

结果如下

 

转载于:https://www.cnblogs.com/lijiayong/p/5919642.html

你可能感兴趣的文章
CentOS系统将UTC时间修改为CST时间
查看>>
redis常见面试题
查看>>
导航控制器的出栈
查看>>
玩转CSS3,嗨翻WEB前端,CSS3伪类元素详解/深入浅出[原创][5+3时代]
查看>>
iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置
查看>>
Delphi消息小记
查看>>
JVM介绍
查看>>
将PHP数组输出为HTML表格
查看>>
经典排序算法回顾:选择排序,快速排序
查看>>
BZOJ2213 [Poi2011]Difference 【乱搞】
查看>>
一道关于员工与部门查询的SQL笔试题
查看>>
Canvas基础
查看>>
[Hive - LanguageManual] Alter Table/Partition/Column
查看>>
可持久化数组
查看>>
去除IDEA报黄色/灰色的重复代码的下划波浪线
查看>>
Linux发送qq、网易邮件服务配置
查看>>
几道面试题
查看>>
【转】使用 WebGL 进行 3D 开发,第 1 部分: WebGL 简介
查看>>
js用正则表达式控制价格输入
查看>>
chromium浏览器开发系列第三篇:chromium源码目录结构
查看>>