百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

Captcha

> 安全
开源

PHP Captcha 库

1.8K stars0 点赞2 次浏览
访问官网GitHub

工具介绍

PHP Captcha 库

验证码

安装

使用 composer: composer require gregwar/captcha 用法

您可以使用 CaptchaBuilder 创建验证码:

php
<?php

use Gregwar\Captcha\CaptchaBuilder;

$builder = new CaptchaBuilder;
$builder->build();

然后可以将其保存到文件中:

php
<?php

$builder->save('out.jpg');

或者直接输出它:

php
<?php

header('Content-type: image/jpeg');
$builder->output();

或者直接在 HTML 页面中嵌入:

php
inline(); ?>" />

您将能够获取代码并与用户输入进行比较:

php
<?php

// Example: storing the phrase in the session to test for the user 
// input later
$_SESSION['phrase'] = $builder->getPhrase();

您可以将该短语与用户输入的内容进行比较:

php
if($builder->testPhrase($userInput)) {
    // instructions if user phrase is good
}
else {
    // user phrase is wrong
}

API

您可以使用以下函数:

  • __construct($phrase = null), 使用给定的短语构造构建器, 如果短语为空, 将生成一个随机短语
  • getPhrase(), 允许您获取短语内容
  • setDistortion($distortion), 启用或禁用扭曲, 在调用 build() 之前调用它
  • isOCRReadable(), 如果 OCR 可以使用 ocrad 软件读取, 则返回 true, 您需要启用 shell_exec, 并安装 imagemagick 和 ocrad
  • buildAgainstOCR($width = 150, $height = 40, $font = null), 直到不能使用 ocrad 读取, 才生成验证码
  • build($width = 150, $height = 40, $font = null), 使用给定的 $width, $height 和 $font 生成验证码。默认情况下, 将从库中使用随机字体
  • save($filename, $quality = 80), 以给定的质量将验证码保存到 $filename 中
  • get($quality = 80), 返回图像数据
  • output($quality = 80), 直接输出图像
  • inline($quality = 80), 直接在 HTML 页面中返回 jpeg 代码
  • setBackgroundColor($r, $g, $b), 设置背景颜色以强制使用它(这将禁用许多效果, 不建议使用)
  • setBackgroundAlpha($alpha), 设置背景透明度 0 表示完全不透明, 127 表示完全透明

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPanti-botanti-spambotscaptcha

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类安全
定价开源

> 相关工具

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.