拐杖 / 验证码生成器
PHP GD 验证码生成器
v1.1.0
2023-02-24 00:09 UTC
Requires
- php: ^7.4 || ^8.0
- ext-gd: *
- ext-mbstring: *
This package is not auto-updated.
Last update: 2024-09-09 18:14:45 UTC
README
验证码生成器。
此代码基于KCAPTCHA v2.0
安装
composer require crutch/captcha-generator:^1.0
使用
<?php use Crutch\CaptchaGenerator\CaptchaGenerator; require __DIR__ . '/vendor/autoload.php'; $generator = (new CaptchaGenerator()) ->withSize(250, 100) // Image size. By default, 160x80 ->withBackgroundColor(20, 20, 20) // Set background color (r, g, b). By default, random light color ->withForegroundColor(200, 200, 200) // Set foreground color (r, g, b). By default, random dark color ->withCredits('crutch captcha') // Added text to image bottom. By default, NULL ->withSpaces(true) // Adds spaces between characters. By default, FALSE ->withFluctuationAmplitude(0) // Vertical fluctuation amplitude. By default, 8 ->withWhiteNoiseDensity(.1) // White noise density. By default, 1 / 6 ->withBlackNoiseDensity(.05) // White noise density. By default, 1 / 30 ->asJpeg(90) // Use JPEG format with quality from 1 to 100 ->asWebp(80) // Use WEBP format with quality from 1 to 100 ->asGif() // Use GIF format ->asPng(8) // Use PNG format with quality from 1 to 9 ; // $symbols = '23456789abcdegkpqsvxyz'; // alphabet without similar symbols (o=0, 1=l, i=j, t=f) $text = ''; for ($i = 0; $i < 6; $i++) { $text .= substr($symbols, mt_rand(0, strlen($symbols) - 1), 1); } $image = $generator->generate($text); // generate PNG image file_put_contents('/tmp/captcha.png', $image); // save to file echo sprintf('<img src="data:image/png;base64,%s" alt="captcha"/>', base64_encode($image)); // out inline
字体
您可以从 ttl 文件生成字体
./vendor/bin/crutch-captcha-generator font:convert /path/to/input-font.ttf /path/to/output-font-simple.png
或轮廓字体
./vendor/bin/crutch-captcha-generator font:convert /path/to/input-font.ttf /path/to/output-font-outline.png --outline
并将其设置为验证码
<?php use Crutch\CaptchaGenerator\CaptchaGenerator; require __DIR__ . '/vendor/autoload.php'; $generator = (new CaptchaGenerator()) ->withFont('/path/to/output-font-simple.png', true) // add custom font and unset others ->withFont('/path/to/output-font-outline.png') // add other custom font ; $image = $generator->generate('abc'); // generate PNG image