使用面向对象编程,直接上代码:


代码

本代码参考自其它文章,下面给出在原代码基础上做出部分修改以适合实际情况的代码:

参考原文在文章末尾给出

<?php
header("content-type:text/html;charset=utf-8"); 
/**
 * Captcha class
 */
class Captcha
{
    private $codeNum;
    private $width;
    private $height;
    private $img;
    private $lineFlag;
    private $piexFlag;
    private $fontSize;
    private $code;
    private $string;
    private $font;
    function __construct($codeNum = 4,$height = 30,$width = 70,$fontSize = 16,$lineFlag = true,$piexFlag = true)
    {
        $this->string = 'qwertyupmkjnhbgvfcdsxa123456789';//去掉相似符号
        $this->codeNum = $codeNum;
        $this->height = $height;
        $this->width = $width;
        $this->code='';
        $this->lineFlag = $lineFlag;
        $this->piexFlag = $piexFlag;
        $this->font = dirname(__FILE__).'/../fonts/consola.ttf';
        //定义字体,字体文件下载链接下文给出
        $this->fontSize = $fontSize;
    }
    //创建背景图片
    public function createImage(){
        $this->img = imagecreate($this->width, $this->height);
        imagecolorallocate($this->img,255,255,255);//图片背景颜色
    }
    //创建验证码
    public function createCode(){
        $strlen = strlen($this->string)-1;
        for ($i=0; $i < $this->codeNum; $i++) {
            $this->code .= $this->string[mt_rand(0,$strlen)];
        }
        $_SESSION['code'] = $this->code;
        $diff = $this->width/$this->codeNum;
        for ($i=0; $i < $this->codeNum; $i++) {
            $txtColor = imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));//定义字体颜色
            imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
        }
    }
    //创建干扰线条
    public function createLines(){
        for ($i=0; $i < 4; $i++) {
            $color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));
            imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
        }
    }
    //创建干扰点
    public function createPiexs(){
        for ($i=0; $i < 100; $i++) {
            $color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
        }
    }
    public function show()
    {
        $this->createImage();
        $this->createCode();
        if ($this->lineFlag) {
            $this->createLines();
        }
        if ($this->piexFlag) {
            $this->createPiexs();
        }
        ob_clean();//如果图片无法加载请加上这句话
        header('Content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }
  
    public function getCode(){
        return $this->code;
    }
}
    session_start(); //开启session
    $captcha = new Captcha();  //实例化验证码类(可自定义参数)
    $captcha->show();  //调用输出
?>  

下面这个放入html中引入验证码,可以实现点击图片刷新验证码

<img src="../php/captcha.php" onclick="javascript:this.src = '../php/Captcha.php?'+Math.random();">

效果

验证码.png

注意事项

1. 字体文件目录要修改成自己对应的目录

dirname(__FILE__)函数是给出当前php文件所在目录,由于我存放字体文件目录和存放该php文件目录同级,所以在后面要加上 ..,代表返回上级目录

所以我的加载字体路径为 dirname(__FILE__).'/../fonts/consola.ttf'

之前我忘记改了,出来的图片都是有图无字,在php日志文件中发现无法读取字体错误

2. 图片无法加载(该位置能显示图片标识)

如果你无法加载出图片就加上 ob_clean(),具体位置见上面源码

3. 点我下载字体 链接:http://labfile.oss.aliyuncs.com/courses/587/consola.ttf

4. 参考文章

Last modification:September 11th, 2020 at 03:05 pm
如果觉得我的文章对你有用,请我喝杯奶茶吧