Home  >  Article  >  PHP Framework  >  Simplify think-captcha verification code under ThinkPHP6 and improve text recognition

Simplify think-captcha verification code under ThinkPHP6 and improve text recognition

幸运切糕
幸运切糕Original
2020-05-01 20:59:513364browse

As we all know, the verification code can prevent malicious login registration. It is generated by the server, sent to the client, and finally displayed in image format. Complex verification codes can improve the security of the website, but overly complex verification codes will also make it extremely difficult to recognize text with the naked eye, greatly reducing the user experience.

This article uses the latest thinkphp6.0think-captcha extension as the modification object to explain in detail how to simplify the display of verification codes, improve text recognition, and make the elderly People can see clearly too!

think-captcha verification code

Controller test code:

<?php
namespace app\controller;

use app\BaseController;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        return View::fetch();
    }
}

View test code :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<img src="{:captcha_src()}" alt="">
</body>
</html>

The verification code rendered by default is as follows:

Simplify think-captcha verification code under ThinkPHP6 and improve text recognition

Some friends may think this is very clear, but for older people It is still difficult for older people. In addition, by modifying the verification code file, you can also improve your hands-on ability and increase your knowledge.

Optimize think-captcha verification code

Find the config\captcha.php configuration file and modify the following parts:

//验证码位数
&#39;length&#39;   => 4,                    //简洁的4字符验证码
// 验证码字符集合
&#39;codeSet&#39;  => &#39;0123456789&#39;,        //这里我去掉英文了,只留下数字部分
// 是否使用混淆曲线
&#39;useCurve&#39; => false,
//是否添加杂点
&#39;useNoise&#39; => false,
// 验证码字体 不设置则随机
&#39;fontttf&#39;  => &#39;4.ttf&#39;,            //经测试,该字体很顺眼

Refresh after modification to see if it is more concise?

Simplify think-captcha verification code under ThinkPHP6 and improve text recognition

This is actually quite good. Under normal circumstances, it is not recommended to modify the framework, but for the purpose of learning, we will further optimize it.

Openvendor\topthink\think-captcha\src\Captcha.php

Modify line 277 and remove the font tilt

imagettftext($this->im, $this->fontSize, 0, $x, $y, $this->color, $fontttf, $char);

Finally take a look at the effect, are you satisfied?

Simplify think-captcha verification code under ThinkPHP6 and improve text recognition

This article simplifies the display of the verification code and improves the performance by simply modifying the configuration file and removing the inclination. Regarding text recognition, friends, the more you practice, the more profound your impression will be!

The above is the detailed content of Simplify think-captcha verification code under ThinkPHP6 and improve text recognition. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn