如何在CodeIgniter中创建音频验证码?



如何使用CodeIgniter中的captcha函数创建音频验证码?

我只希望它是一个数字验证码。

$config = array(
'img_path' => './captchas/',
'img_url' => './captchas/',
'font_path' => './fonts/tahoma.ttf',
'img_width' => 96,
'img_height' => 22,
'expiration' => 1,
'word_length' => 5,
'font_size' => 11,
'img_id' => 'captcha_image',
'img_alt' => 'captcha',
'img_class' => '',
'img_role' => '',
'pool' => '0123456789', /* only numeric captcha */
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(140, 140, 140),
'grid' => array(170, 170, 170)
)
);

首先,创建一个文件夹。例如"Audio">.

然后用你自己的声音创建一个mp3文件(或其他格式)。你可以录下你的声音(慢慢地说0-9),用在线软件修改你的mp3文件。然后根据文件的内容重命名新的裁剪文件。

0.mp3, 1.mp3, 2.mp3,…,8.mp3, 9.mp3

你也可以使用一个在线演讲文本软件,如:https://ttsmp3.com和创建0-9 mp3文件。

复制mp3文件到"Audio">文件夹。

然后,为语音验证码文件创建一个文件夹。例如"Voice-Captcha">

/* your config array */
$config = array(
'img_path' => './captchas/',
'img_url' => './captchas/',
'font_path' => './fonts/tahoma.ttf',
'img_width' => 96,
'img_height' => 22,
'expiration' => 1,
'word_length' => 5,
'font_size' => 11,
'img_id' => 'captcha_image',
'img_alt' => 'captcha',
'img_class' => '',
'img_role' => '',
'pool' => '0123456789', /* only numeric captcha */
'colors' => array(
'background' => array(255, 255, 255),
'border' => array(255, 255, 255),
'text' => array(140, 140, 140),
'grid' => array(170, 170, 170)
)
);
/* My added code begins here */
/* create captcha */
$cap = create_captcha($config);
/* new file name */
$file = './Voice-Captcha/' . $cap['filename'] . '.mp3';
/* open a voice file as writable */
$voice = fopen($file, 'a+');
/* split captcha word */
for ($i = 0; $i < $config['word_length']; $i++) {
/* get audio file name base on splitted captcha word */
$audio_file = './Audio/' . mb_substr($cap['word'], $i, 1) . '.mp3';
/* open audio file as readonly */
$audio = fopen($audio_file, 'r');
/* read audio data */
$data = fread($audio, filesize($audio_file));
/* write audio data to the end of the voice file (append data to prev. data) */
fwrite($voice, $data);
/* close audio file */
fclose($audio);
}
/* close voice file */
fclose($voice);
/* data */
$data['image'] = $cap['image'];
$data['voice'] = '<audio src="' . $file . '" controls></audio>';
/* load view page */
$this->load->view('My-Page', $data);

查看文件,例如"My-Page"是:

<?php echo $image; ?>
<?php echo $voice; ?>

请记住,如果您想删除旧的语音验证码文件,请在"captcha_helper">,找到这一行:

@unlink($img_path.$filename);

并在后面添加下面的代码:

@unlink('./Voice-Captcha/'.$filename.'.mp3');

注意:当你录制自己的声音时,你可以在你的声音上添加音频效果,或者使用几个人的声音来创建不同的文件。这些方法提高了安全性,使机器人更难探测到声音。

最新更新