{"id":18333734,"url":"https://github.com/easy-swoole/verify-code","last_synced_at":"2025-04-06T03:34:38.430Z","repository":{"id":49253876,"uuid":"110237122","full_name":"easy-swoole/verify-code","owner":"easy-swoole","description":"验证码生成类库","archived":false,"fork":false,"pushed_at":"2024-01-31T13:54:54.000Z","size":210,"stargazers_count":22,"open_issues_count":1,"forks_count":9,"subscribers_count":0,"default_branch":"3.x","last_synced_at":"2025-03-30T13:03:30.779Z","etag":null,"topics":["verifycode"],"latest_commit_sha":null,"homepage":"http://www.easyswoole.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/easy-swoole.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-10T10:54:17.000Z","updated_at":"2024-12-16T08:32:40.000Z","dependencies_parsed_at":"2024-01-31T14:50:23.957Z","dependency_job_id":"1de2bac2-a23d-4a42-9e9d-7ce5cb752345","html_url":"https://github.com/easy-swoole/verify-code","commit_stats":{"total_commits":34,"total_committers":6,"mean_commits":5.666666666666667,"dds":0.5294117647058824,"last_synced_commit":"804d3eb0a96f2c859a25e2a7451f5c38c613eef3"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easy-swoole%2Fverify-code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easy-swoole%2Fverify-code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easy-swoole%2Fverify-code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easy-swoole%2Fverify-code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/easy-swoole","download_url":"https://codeload.github.com/easy-swoole/verify-code/tar.gz/refs/heads/3.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247430838,"owners_count":20937873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["verifycode"],"created_at":"2024-11-05T19:44:07.865Z","updated_at":"2025-04-06T03:34:33.402Z","avatar_url":"https://github.com/easy-swoole.png","language":"PHP","readme":"验证码组件\n------\n\n用于生产验证码，支持自定义验证码字体，使用Composer安装\n\n```\ncomposer require easyswoole/verifycode=3.x\n```\n\n配置定义\n------\n\n组件本身提供了默认配置，即使不做任何设置也可以直接生成验证码，需要对验证码进行自定义配置可以使用组件提供的`Config`类进行动态配置\n\n```\n// v1.x\nuse easySwoole\\VerifyCode\\Config;\n$config = new Config();\n// v2.x\nuse EasySwoole\\VerifyCode\\Config;\n$config = new Config();\n```\n\n#### 设置字符集合\n可以自定义验证码生成使用的字符集合设置后从集合中随机选取，不设置则从`[0-9A-Za-z]`中随机选取\n\n```\n$config-\u003esetCharset('123456ABCD');\n```\n\n#### 设置背景色\n设置验证码的背景颜色，不设置默认使用白色，支持使用完整HEX，缩写HEX和RGB值设置\n\n```\n$config-\u003esetBackColor('#3A5FCD');\n$config-\u003esetBackColor('CCC');\n$config-\u003esetBackColor([30, 144, 255]);\n```\n\n#### 设置文字颜色\n设置验证码的背景颜色，不设置则随机生成一个颜色，支持使用完整HEX，缩写HEX和RGB值设置\n\n```\n$config-\u003esetFontColor('#3A5FCD');\n$config-\u003esetFontColor('CCC');\n$config-\u003esetFontColor([30, 144, 255]);\n```\n\n#### 设置混淆\n支持两种混淆方式，默认两种混淆都是关闭的，需要手动开启\n\n```\n// 开启或关闭混淆曲线\n$config-\u003esetUseCurve();\n// 开启或关闭混淆噪点\n$config-\u003esetUseNoise();\n```\n\n#### 设置字体\n默认验证码类已经带有6种字体，如果需要增加自己的字体库来提高识别难度，或者指定使用的字体，可以如下设置，注意字体路径需要使用绝对路径，即文件的完整路径\n\n```\n// 增加单个字体传入路径字符串\n$config-\u003esetFonts('path/to/file.ttf');\n// 增加多个字体传入路径的数组\n$config-\u003esetFonts(['path/to/file1.ttf', 'path/to/file2.ttf']);\n```\n\n```\n// 指定生成使用的字体文件\n$config-\u003esetUseFont('path/to/file.ttf');\n```\n\n#### 其他设置\n可以指定图片宽高，字体大小，随机生成的验证码位数等\n\n```\n// 设置图片的宽度\n$config-\u003esetImageWidth(400);\n// 设置图片的高度\n$config-\u003esetImageHeight(200);\n// 设置生成字体大小\n$config-\u003esetFontSize(30);\n// 设置生成验证码位数\n$config-\u003esetLength(4);\n```\n\n#### 链式调用\n为了更流畅的进行设置，所有的配置项均支持链式调用\n\n```\n$config = new Config();\n$config-\u003esetUseNoise()-\u003esetUseCurve()-\u003esetFontSize(30);\n```\n\n------\n\n可以使用上方的动态配置，将设置好的配置类传入给验证码类，\n```\n$config = new Config();\n$config-\u003esetFontSize(30);\n$VCode = new VerifyCode($config);\n```\n\n如果配置比较多，也需要全站统一验证码配置，可以将验证码的配置放入配置文件，在生成时读取配置，验证码的设置类继承自`SplBean`，可以在设置好后使用配置类的`toArray`方法直接获得配置数组，实例化验证码时，读取数组重新生成Config类即可\n\n生成\n------\n\n初始化配置后即可生成验证码，可以随机生成，也可以指定需要生成的验证码\n\n```\n$VCode = new VerifyCode($config);\n\n// 随机生成验证码\n$Code = $VCode-\u003eDrawCode(); // v2.x -\u003edrawCode\n\n// 生成指定验证码\n$Code = $VCode-\u003eDrawCode('MyCode'); // v3.x, v2.x -\u003edrawCode\n```\n\n生成好验证码后结果是一个`Result`类，可以使用`getImageBody`获取验证码的图片内容，使用`getImageStr`获得验证码字符，`getMineType`获得验证码的Mine信息\n\n### EasySwoole\n\n在控制器中输出\n\n```\n$this-\u003eresponse()-\u003ewithHeader(\"Content-Type\",$code-\u003egetImageMime());\n$this-\u003eresponse()-\u003ewrite($code-\u003egetImageByte());\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasy-swoole%2Fverify-code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feasy-swoole%2Fverify-code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasy-swoole%2Fverify-code/lists"}