承接 olakunlevpn/phpcapcha 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

olakunlevpn/phpcapcha

Composer 安装命令:

composer require olakunlevpn/phpcapcha

包简介

PHP Captcha is an elegant, highly customizable, and secure captcha generator for PHP 8.0+. It supports advanced features like wave distortion, mathematical challenges, granular noise control, and custom font management.

README 文档

README

PHP Captcha is an elegant, highly customizable, and secure captcha generator for PHP 8.0+. It supports advanced features like wave distortion, mathematical challenges, granular noise control, and custom font management.

Designed to be simple to use but powerful enough for any security requirement.

PHP Captcha Preview

Requirements

  • PHP 8.0 or higher
  • GD Extension (ext-gd)

Features

  • Highly Customizable: Control colors, fonts, noise, lines, and distortion.
  • Math Mode: Present users with simple arithmetic verification (e.g., 5 + 3).
  • Wave Distortion: Advanced warp algorithms to defeat OCR bots.
  • Font Management: Use a specific font or randomize from a directory.
  • Multiple Outputs: Stream directly to browser, get Base64, or save to disk.
  • Zero Dependencies: Requires only standard PHP GD extension.

Installation

You can install the package via composer:

composer require olakunlevpn/phpcapcha

Basic Usage

Here is the quickest way to get a captcha up and running:

use Olakunlevpn\Captcha\Captcha;

session_start();

// Initialize
$captcha = new Captcha();

// Generate and Output an Image (200x70)
$captcha->create()->output();

// Store the code in session for validation later
$_SESSION['captcha_code'] = $captcha->getCode();

Advanced Usage

Customizing Appearance

You can chain methods to configure the captcha exactly how you want it.

use Olakunlevpn\Captcha\Captcha;

$captcha = new Captcha();

$captcha->setFont(__DIR__ . '/fonts/Monaco.ttf') // Or pass a directory for random font
        ->setTextColor(40, 40, 40)               // Dark Grey Text
        ->setBackgroundColor(240, 240, 240)      // Light Grey Background
        ->setLength(5)                           // 5 Characters
        ->setType('alpha')                       // Alphabetic only (no numbers)
        ->create(250, 80);                       // Create with specific dimensions

$captcha->output();

Math Challenge Mode

Instead of asking users to read distorted text, ask them to solve a simple math problem.

$captcha = new Captcha();

// Set type to 'math'
$captcha->setType('math');

$image = $captcha->create();

// The code will be the result (e.g., if image shows "5 + 3", code is "8")
$result = $captcha->getCode();

$captcha->output();

Granular Noise & Distortion Control

You have full control over the security elements.

$captcha = new Captcha();

// Enable/Disable specific elements
$captcha->setNoise(true, 100)      // Enabled, 100 dots
        ->setLines(true, 5)        // Enabled, 5 random lines
        ->setDistortion(true, 5, 40) // Enabled, Amplitude 5, Period 40
        ->create();

To create a Clean captcha (easier for humans, less secure against bots):

$captcha->setNoise(false)
        ->setLines(false)
        ->setDistortion(false)
        ->create();

Saving to File

Useful for caching or generating static assets.

$captcha->create();
$captcha->save(__DIR__ . '/captchas/image_123.png');

Inline Base64

Useful for passing the image directly to a view or API response without a separate endpoint.

$captcha->create();
$base64 = $captcha->getBase64();

echo '<img src="' . $base64 . '" />';

Validation

Validation is straightforward. Simply compare the user's input against the stored code.

session_start();

$input = $_POST['captcha']; // User input
$stored = $_SESSION['captcha_code']; // Stored from generation

// Use case-insensitive comparison for text
if (strtolower($input) === strtolower($stored)) {
    echo "Verification Successful!";
} else {
    echo "Incorrect Code.";
}

API Reference

Method Description
setFont(string $path) Path to a .ttf file OR a directory containing fonts (random selection).
setTextColor(int $r, int $g, int $b) Sets the RGB color of the text.
setBackgroundColor(int $r, int $g, int $b) Sets the RGB color of the background.
setLength(int $length) Number of characters to generate (ignored in Math mode).
setType(string $type) mixed (default), alpha, numeric, or math.
setNoise(bool $enable, int $count) Toggle dots noise and set quantity.
setLines(bool $enable, int $count) Toggle random lines and set quantity.
setDistortion(bool $enable, int $amp, int $period) Toggle wave distortion. Amplitude/Period control wave shape.
create(int $width, int $height) Generates the image resource. Must be called before output.
output() Headers + Image binary stream (PNG).
getBase64() Returns Base64 encoded string of the image.
save(string $path) Saves the image to the specified file path.
getCode() Returns the correct answer/code for validation.

Examples

Check the examples/ directory for full implementations:

  • Vanilla CSS (examples/vanilla-css.php)
  • Tailwind CSS (examples/tailwind.php)
  • Bootstrap 5 (examples/bootstrap.php)
  • jQuery/AJAX (examples/jquery.php)

AI assistant integration

This repository includes an AGENTS.md file specifically designed for AI assistants (like GitHub Copilot, Cursor, or Claude). If you're using an AI tool to help with your Laravel project:

  • Point your AI assistant to the AGENTS.md file when asking about phpcapcha optimization.
  • The file contains detailed guidance for analyzing components and determining eligibility.
  • Use it for automated analysis - AI assistants can help audit your entire component library.

Example prompts for AI assistants:

  • "Using the AGENTS.md file, analyze my components and tell me which can use phpcapcha"
  • "Help me add phpcapcha to all eligible components following the guidelines"
  • "Check if this component is safe for captcha protection based on AGENTS.md"

License

The MIT License (MIT). Please see License File for more information.

olakunlevpn/phpcapcha 适用场景与选型建议

olakunlevpn/phpcapcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 106 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 olakunlevpn/phpcapcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 olakunlevpn/phpcapcha 我们能提供哪些服务?
定制开发 / 二次开发

基于 olakunlevpn/phpcapcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 106
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-08