dkh/captcha-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dkh/captcha-generator

Composer 安装命令:

composer require dkh/captcha-generator

包简介

A PHP library for generating Captcha challenges using libgd.

README 文档

README

Description

A PHP library for generating Captcha challenges using libgd.

Captcha examples

  • Easy expression captcha:
    PHP Captcha Generator - Expression - Easy
  • Hard expression captcha:
    PHP Captcha Generator - Expression - Hard
  • Easy string captcha:
    PHP Catpcha Generator - String - Easy
  • Hard string captcha (colored):
    PHP Captcha Generator - String - Hard

Repositories

Required dependencies

  • GD Graphics Library (ext-gd).

User Guide

Installation

You can easily get the library installed on your project by running this command.

composer require dkh/captcha-generator

Implementation

Captcha types

  • ExpressionCaptcha expression captcha requires users to do basic arithmetic operations (addition, subtraction, multiplication and division) to solve.
  • StringCaptcha string captcha only requires users to recognize the characters in the string.

Create a captcha

To create a captcha, use new *Captcha($size?, $level?).

// Default size: 3 and default difficulty level: 1
$expression_captcha = new ExpressionCaptcha();
$string_captcha = new StringCaptcha();

// Specific size and difficulty level
$size = 10;
$level = 2;
$another_expression_captcha = new ExpressionCaptcha($size, $level);

Get captcha's solved value

To get captcha's solved value, call $captcha->solve() or *Captcha::solveString($string).
Store the solved value somewhere, e.g in a session variable, to later verify user's captcha input.

$_SESSION['captcha'] = $captcha->solve();

// Or in a way that is infrequent,
// use static method *Captcha::solveString()
$my_expression = '1+6:3-2*4';
$_SESSION['my_captcha'] = ExpressionCaptcha::solveString($my_expression);

Verify user's captcha input

To verify user's captcha input, compare it with the captcha's previously solved value stored somewhere.

$user_captcha_input = $_POST['captcha'] ?? '';
$is_matched = $_SESSION['captcha'] === $user_captcha_input;

Display the captcha image

To render captcha into image, call $captcha->render($options?), Captcha::renderString($string, $options?). Return value is a PNG image data string encoded with base64.
To dislay the captcha image, use data URL data:image/png;base64, or save the rendered image somewhere and return the image's path.

$base64_image = $captcha->render();
echo sprintf('<img src="data:image/png;base64,%s">', $base64_image);

// Or in a way like this:
$my_string = 'any will do?';
$image_path = 'captcha.png';
$base64_image_to_be_saved = Captcha::renderString($my_string);
file_put_contents(
    $image_path,
    base64_decode($base64_image_to_be_saved)
);
echo sprintf('<img src="/%s">', $image_path);

// Image rendered with some options
$another_base64_image = $captcha->render([
    'height' => 50,
    'fill' => [0, 0, 0, 30],
    // The alpha channel is optional
    'color' => [255, 255, 255]
]);
echo sprintf(
    '<img src="data:image/png;base64,%s">',
    $another_base64_image
);

Example implementation of the library

<?php

require_once 'vendor/autoload.php';

use Dkh\ExpressionCaptcha;

session_start();

// Verify user's captcha input
if (isset($_POST['captcha']) && isset($_SESSION['captcha'])) {
    $is_matched = $_POST['captcha'] === $_SESSION['captcha'];
} else {
    $is_matched = null;
}
$message = $is_matched !== null ?
    ($is_matched ? 'Captcha matched' : 'Captcha not matched') :
    'Try solving the captcha';

// Create a captcha
$captcha = new ExpressionCaptcha();
// Store captcha's solved value
$_SESSION['captcha'] = $captcha->solve();
// Render the captcha into image
// The return value is a PNG image string encoded with base64
$base64_image = $captcha->render();

echo sprintf(
    '<!DOCTYPE html>' .
    '<html>' .
    '<body>' .
    '<form method="POST">' .
    '<img src="data:image/png;base64,%s"><br>' .
    '<input name="captcha" placeholder="Input captcha">' .
    '<input type="submit" value="Submit"><br>' .
    '</form>' .
    '<div>Message: %s</div>' .
    '</body>' .
    '</html>',
    $base64_image,
    $message
);

dkh/captcha-generator 适用场景与选型建议

dkh/captcha-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.73k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「php」 「captcha」 「dkh」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 dkh/captcha-generator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-10