public function generate($width = 100, $height = 30, $ttl = 300)
{
$code = '';
$charsetCount = strlen($this->_charset);
for ($i = 0; $i < $this->_length; $i++) {
$code .= $this->_charset[mt_rand() % $charsetCount];
}
if (class_exists('Imagick')) {
$response = $this->_generateByImagic($code, $width, $height);
} elseif (function_exists('gd_info')) {
$response = $this->_generateByGd($code, $width, $height);
} else {
throw new CaptchaException('`captcha` service is not support, please install `gd` or `imagic` extension first');
}
$captchaData = ['code' => $code, 'created_time' => time(), 'ttl' => $ttl];
$this->session->set($this->_sessionVar, $captchaData);
return $response;
}