PHP code example of sandpear / validate-code

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

    

sandpear / validate-code example snippets




    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);

    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;

    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 '<img src="'.$base64Png.'">';

    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);