Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pluveto/swoole-captcha
Captcha for swoole (or swoft / hyperf ...) modified from php-captcha.
https://github.com/pluveto/swoole-captcha
Last synced: 4 days ago
JSON representation
Captcha for swoole (or swoft / hyperf ...) modified from php-captcha.
- Host: GitHub
- URL: https://github.com/pluveto/swoole-captcha
- Owner: pluveto
- License: mit
- Created: 2020-07-28T14:46:23.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-30T05:04:50.000Z (over 4 years ago)
- Last Synced: 2024-10-07T05:21:07.575Z (about 1 month ago)
- Language: PHP
- Size: 331 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swoole-captcha
基于 php-captcha 的验证码插件。对 PSR7 适配。增加 Base64 输出和字节输出。
## 安装
```bash
$ composer require pluveto/swoole-captcha dev-master
```## 使用
```php
/**
* @api {get} /auth/captcha 获取验证码
* @apiName GetCaptcha
* @apiGroup auth
* @apiVersion 1.0.0
* @apiPermission none
*
* @apiParam {number{32-320}} [width=180] 宽度
* @apiParam {number{32-320}} [height=64] 高度
* @apiParam {boolean} [raw=true]
* @apiParam {string} [uuid=""] UUID
*/
public function getCaptcha()
{
$req = $this->params();if (!$req->uuid) {
$uuid = uuid();
} else {
$uuid = $this->authValidation->validateUUID($req->uuid);
}$captha = new CaptchaBuilder();
$captha->initialize([
'width' => $req->width, // 宽度
'height' => $req->height, // 高度
'line' => false, // 直线
'curve' => true, // 曲线
'noise' => 1, // 噪点背景
'fonts' => [] // 字体
]);
$captha->create();
$text = $captha->getText(); // 获取验证码文本// 将验证码放到缓存中
$redis = ApplicationContext::getContainer()->get(\Hyperf\Redis\Redis::class);
$redis->set('captcha_' . str_replace("-", "", $uuid), $text, 60);
if ($req->raw) {
// 直接输出验证码
return $captha->output($this->response->raw(""), 1);
} else {
// 通过 base64 输出验证码
return $this->success(
[
"uuid" => $uuid,
"expiredAt" => time() + 60,
"content" => $captha->getBase64(1)
]
);
}
}