<?php
/**
 * @copyright   2008-2020 伯乐发卡 <http://www.lailiyun.com>
 * @creatdate   2020-9-10 Mr wang <634273662@qq.com>
 * @version   伯乐发卡V3（个人自动发卡系统）
 */

namespace BL\app\controller;

/**验证码生成
 * Class chkcode
 * @package BL\app\controller
 */
class chkcode
{
    function index()
    {
        $randCode = '';
        $chars = 'asQW23456789';
        for ($i = 0; $i < 5; $i++) {
            $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        $_SESSION['chkcode'] = strtolower($randCode);
        $img = imagecreate(70, 22);
        $bgColor = isset($_GET['mode']) && $_GET['mode'] == 't' ? imagecolorallocate($img, 245, 245, 245) : imagecolorallocate($img, 255, 255, 255);
        $pixColor = imagecolorallocate($img, mt_rand(30, 180), mt_rand(10, 100), mt_rand(40, 250));
        for ($i = 0; $i < 5; $i++) {
            $x = $i * 13 + mt_rand(0, 4) - 2;
            $y = mt_rand(0, 3);
            $text_color = imagecolorallocate($img, mt_rand(30, 180), mt_rand(10, 100), mt_rand(40, 250));
            imagechar($img, 5, $x + 5, $y + 3, $randCode[$i], $text_color);
        }
        for ($j = 0; $j < 60; $j++) {
            $x = mt_rand(0, 70);
            $y = mt_rand(0, 22);
            imagesetpixel($img, $x, $y, $pixColor);
        }
        ob_clean();
        header('Content-Type: image/jpeg');
        imagejpeg($img);
        imagedestroy($img);
    }
}
?>