PHPackages                             sandpear/validate-code - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sandpear/validate-code

ActiveExtension[Utility &amp; Helpers](/categories/utility)

sandpear/validate-code
======================

validate code 是生成验证码图片的扩展。

v1.0(3y ago)02MITPHPPHP &gt;=7.4.0

Since Oct 28Pushed 3y ago1 watchersCompare

[ Source](https://github.com/sandpear/validate-code)[ Packagist](https://packagist.org/packages/sandpear/validate-code)[ RSS](/packages/sandpear-validate-code/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

validate-code 生成验证码图片
=====================

[](#validate-code-生成验证码图片)

- 根据验证码生成图片。

生成效果：
-----

[](#生成效果)

[![image](https://github.com/sandpear/validate-code/raw/master/tests/1.png)](https://github.com/sandpear/validate-code/blob/master/tests/1.png)

[![image](https://github.com/sandpear/validate-code/raw/master/tests/2.png)](https://github.com/sandpear/validate-code/blob/master/tests/2.png)

[![image](https://github.com/sandpear/validate-code/raw/master/tests/3.png)](https://github.com/sandpear/validate-code/blob/master/tests/3.png)

安装：
---

[](#安装)

```
composer require sandpear/validate-code

```

- 单纯的生成验证码图片扩展。
- 使用方法见`tests/test.php`文件示例:

```
require '../vendor/autoload.php';
require 'helpers.php';

```

示例一：创建固定验证码图片
-------------

[](#示例一创建固定验证码图片)

```
    use Sandpear\ValidateCode\GraphValidateCode;
    $ValidateCode = new GraphValidateCode();
    $code = $ValidateCode->createGraphValidateCode('1234')->getCode();
    fwrite_log('固定验证码:' . $code);
    $ValidateCode->outputPng(true);

```

\##示例二：创建随机验证码图片

```
    use Sandpear\ValidateCode\GraphValidateCode;
    $ValidateCode = new GraphValidateCode();
    $code = $ValidateCode->createGraphValidateCode()->getCode();
    fwrite_log('随机验证码:' . $code);
    $ValidateCode->outputPng(true);

```

示例三：创建自定义随机验证码图片
----------------

[](#示例三创建自定义随机验证码图片)

```
    use Sandpear\ValidateCode\GraphValidateCode;
    $config = [
                 'codeLength' => 4,   #验证码长度
                 'imgWidth'   => 130, #图片宽度
                 'imgHeight'  => 50,  #图片高度
                 'fontSize'   => 20,  #字体大小
                 # 字体文件
                 'fontFile' => [
                     __DIR__.'/../src/resources/font/century-gothic.ttf',
                 ],
              ];
    $ValidateCode = new GraphValidateCode($config);
    $code = $ValidateCode->createGraphValidateCode()->getCode();
    fwrite_log('自定义随机验证码:' . $code);
    $ValidateCode->outputPng(true);

```

示例四：创建随机验证码并返回图片Blob
--------------------

[](#示例四创建随机验证码并返回图片blob)

```
    use Sandpear\ValidateCode\GraphValidateCode;
    $ValidateCode = new GraphValidateCode();
    $code = $ValidateCode->createGraphValidateCode()->getCode();
    fwrite_log('随机验证码:' . $code);
    $outputImageBlob = $ValidateCode->outputImageBlob();
    fwrite_log('随机验证码Blob:' . $outputImageBlob);
    header('Content-type:image/png');
    echo $outputImageBlob;

```

示例五：创建随机验证码并返回图片Blob的另一个用法
--------------------------

[](#示例五创建随机验证码并返回图片blob的另一个用法)

```
    use Sandpear\ValidateCode\GraphValidateCode;
    $ValidateCode = new GraphValidateCode();
    $code = $ValidateCode->createGraphValidateCode()->getCode();
    fwrite_log('随机验证码:' . $code);
    $outputImageBlob = $ValidateCode->outputImageBlob();
    $base64Png = 'data:image/png;base64,'.base64_encode($outputImageBlob);
    fwrite_log('随机验证码Base64:' . $base64Png);
    echo '';

```

示例六：创建自定义中文验证码（中文验证码不支持随机码）
---------------------------

[](#示例六创建自定义中文验证码中文验证码不支持随机码)

```
    use Sandpear\ValidateCode\GraphValidateCode;
    $config = [
        'codeLength' => 4,   #验证码长度
        'imgWidth'   => 130, #图片宽度
        'imgHeight'  => 50,  #图片高度
        'fontSize'   => 20,  #字体大小
        # 字体文件
        'fontChineseFile' => [
         __DIR__.'/../src/resources/font/microsoft-Ya-hei.ttf',
        ],
    ];
    $ValidateCode = new GraphValidateCode($config);
    $code = $ValidateCode->createGraphValidateCode('吾验证码')->getCode();
    fwrite_log('自定义中文验证码:' . $code);
    $ValidateCode->outputPng(true);

```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1298d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/97410c7a78f121df7eeee285d0b01ab086c635bb1f4cfa508da5f70d36b14319?d=identicon)[sandpear](/maintainers/sandpear)

---

Top Contributors

[![sandpear](https://avatars.githubusercontent.com/u/71304992?v=4)](https://github.com/sandpear "sandpear (1 commits)")[![superman32432432](https://avatars.githubusercontent.com/u/7228420?v=4)](https://github.com/superman32432432 "superman32432432 (1 commits)")

---

Tags

libraryextensionvalidate code

### Embed Badge

![Health badge](/badges/sandpear-validate-code/health.svg)

```
[![Health](https://phpackages.com/badges/sandpear-validate-code/health.svg)](https://phpackages.com/packages/sandpear-validate-code)
```

###  Alternatives

[fedemotta/yii2-widget-datatables

DataTables widget for Yii2

34179.4k1](/packages/fedemotta-yii2-widget-datatables)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
