PHP code example of namesfang / captcha

1. Go to this page and download the library: Download namesfang/captcha 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/ */

    

namesfang / captcha example snippets


/**
 * 当前验证码地址
 * 您的验证码链接:path/to/examples/output.php
 */
$option = new Option();

/**
 * 这里可以配置相关参数
 */

$bundle = new Bundle($option);

header('Content-Type: image/png');

$bundle->output();

$option = new Option();

/**
 * 这里可以配置相关参数
 */
 
$bundle = new Bundle($option);

/**
 * 后端验证码接口
 * 你的验证码接口地址:path/to/inline.php
 */
echo json_encode([
    'code'=> 0,
    'msg'=> 'ok',
    'data'=> $bundle->inline(), // data:image/png;base64,xxxxxx
]);

$option = new Option();

$bundle = new Bundle($option);

/**
 * 将验证码保存到本地
 */
file_put_contents('/path/to/captcha.png', $bundle->stream());
html
<!-- src引入地址 -->
<img src="path/to/examples/output.php">
javascript
/**
 * 前端js从后端获取验证码数据
 */
jQuery.get('path/to/inline.php', function (pl) {
   if(0 === pl.code) {
       $('#captcha_container').html('<img src="'+ pl.data +'"/>');
   }
});