PHP code example of fastknife / ajcaptcha
1. Go to this page and download the library: Download fastknife/ajcaptcha library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fastknife / ajcaptcha example snippets
Fastknife\Service\BlockPuzzleCaptchaService;
use Fastknife\Service\ClickWordCaptchaService;
// 加载配置
$config = � (前端滑动/点选后调用) ---
$token = $_REQUEST['token'];
$pointJson = $_REQUEST['pointJson'];
try {
// 验证成功会返回加密的 captchaVerification
$captchaVerification = $service->check($token, $pointJson);
// 将 captchaVerification 返回给前端
// echo json_encode(['success' => true, 'repData' => ['captchaVerification' => $captchaVerification]]);
} catch (\Exception $e) {
// 验证失败
}
// --- 二次验证 (业务接口登录/注册时调用) ---
// 前端将上一步获取的 captchaVerification 传给业务接口
$captchaVerification = $_REQUEST['captchaVerification'];
try {
$service->verificationByEncryptCode($captchaVerification);
// 验证通过,执行业务逻辑 (登录/注册...)
} catch (\Exception $e) {
// 二次验证失败,拦截业务请求
}
return [
// --------------------------------------------------------------------
// 基础配置
// --------------------------------------------------------------------
// 自定义字体包路径,不填使用默认值 (resources/fonts/WenQuanZhengHei.ttf)
'font_file' => '',
// 水印配置
'watermark' => [
'fontsize' => 12,
'color' => '#ffffff',
'text' => '我的水印'
],
// --------------------------------------------------------------------
// 滑动验证码配置 (Block Puzzle)
// --------------------------------------------------------------------
'block_puzzle' => [
// 模式: 'drawing' (推荐, 原生绘图), 'resource' (旧版图片模板)
'mode' => 'drawing',
// 形状类型 (仅在 drawing 模式下生效)
// 可选: 'jigsaw' (拼图), 'red_heart' (红桃), 'spade' (黑桃), 'diamond' (方片), 'club' (草花)
'shape_type' => 'jigsaw',
// 开启干扰图 (在 drawing 模式下生成干扰凹槽,在 resource 模式下生成干扰拼图)
'is_interfere' => true,
// 容错偏移量 (px)
'offset' => 10,
// 背景图路径
// 支持 string (目录路径) 或 array (文件路径列表)
// 'backgrounds' => '/path/to/images/',
'backgrounds' => [],
// 模板图路径 (仅在 resource 模式下生效)
'templates' => [],
// 是否开启像素缓存 (仅在 resource 模式下生效,提升性能)
'is_cache_pixel' => true,
],
// --------------------------------------------------------------------
// 点击验证码配置 (Click Word)
// --------------------------------------------------------------------
'click_word' => [
// 干扰字数量 (混淆视觉,增加破解难度)
'distract_num' => 2,
// 目标字数量 (需要点击的文字数量)
'word_num' => 4,
// Unicode 图标配置:图标字符 => 文字说明
'icons' => [
'☎' => '电话',
'★' => '星星',
'☀' => '太阳',
'☂' => '雨伞',
'☺' => '笑脸',
'♪' => '音符'
],
// 图标模式: 'random'=随机出现, 'always'=每次都有, 'never'=从不出现
'icon_mode' => 'random',
// 最多图标数量
'max_icons' => 1,
// 图标字体放大比例(相对于汉字)
'icon_font_size_scale' => 1.3,
// 背景图路径
'backgrounds' => [],
],
// --------------------------------------------------------------------
// 缓存配置
// --------------------------------------------------------------------
// 默认使用内置文件缓存 (\Fastknife\Utils\CacheUtils)
'cache' => [
'constructor' => \Fastknife\Utils\CacheUtils::class,
'method' => [
// 如果您的缓存驱动符合 PSR-16 规范 (如 Laravel, TP6, Hyperf),则无需配置此项
// 如果是不兼容的旧框架 (如 TP5),需在此处做方法映射
'get' => 'get', // 获取缓存方法名
'set' => 'set', // 设置缓存方法名
'delete' => 'delete',// 删除缓存方法名 (TP5 为 'rm')
'has' => 'has' // 检查存在方法名
],
'options' => [
'expire' => 300, // 300秒有效期
'prefix' => '',
'path' => '', // 缓存目录 (仅内置缓存有效)
]
]
];
// ThinkPHP 5.0 示例
'cache' => [
'constructor' => [think\Cache::class, 'store'],
'method' => [
'get' => 'get',
'set' => 'set',
'delete' => 'rm', // TP5 删除缓存的方法是 rm
'has' => 'has'
],
// ...
]
'cache' => [
'constructor' => [Illuminate\Support\Facades\Cache::class, 'store'],
// Laravel 符合 PSR-16,无需配置 method
]
'cache' => [
'constructor' => [think\Facade\Cache::class, 'instance'],
// TP6 符合 PSR-16,无需配置 method
]
'cache' => [
'constructor' => function () {
return \Hyperf\Utils\ApplicationContext::getContainer()->get(\Psr\SimpleCache\CacheInterface::class);
},
]
src/
├── Domain/
│ ├── Template/ # 策略层
│ │ ├── TemplateProviderInterface.php # 策略接口
│ │ ├── DrawingTemplateProvider.php # 策略A: 原生绘图 (抗锯齿核心)
│ │ ├── ResourceTemplateProvider.php # 策略B: 图片资源 (兼容旧版)
│ │ │
│ │ └── Shape/ # 形状绘制子策略
│ │ ├── ShapeDrawerInterface.php
│ │ ├── ShapeFactory.php # 工厂模式
│ │ ├── JigsawShapeDrawer.php # 拼图形状
│ │ ├── RedHeartShapeDrawer.php # 红桃形状
│ │ ├── SpadeShapeDrawer.php # 黑桃形状
│ │ ├── DiamondShapeDrawer.php # 方片形状
│ │ └── ClubShapeDrawer.php # 草花形状
│ │
│ └── Logic/ # 业务逻辑层
│ ├── BlockImage.php # 滑动验证码合成逻辑 (Alpha混合, 挖槽)
│ ├── WordImage.php # 点击验证码图像处理 (新增drawWordList支持Unicode图标)
│ └── ...
│
├── Utils/
│ └── ImageUtils.php # 基础设施层: GD 库底层封装 (屏蔽 PHP 版本差异)
└── ...