Donald Prompt
Donald Prompt

Reputation: 25

How to create an audio captcha in CodeIgniter?

How to create an audio captcha using the captcha function in CodeIgniter?

I just want it to be a numeric 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)
    )
);

Upvotes: 1

Views: 493

Answers (1)

POPI
POPI

Reputation: 871

First, make a folder. For example "Audio".

Then create an mp3 file (or other formats) with your own voice. You can record your voice (speak 0-9 slowly) and trim your mp3 file with an online software. Then rename new trimmed files based on their contents.

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

Also you can use an online speech to text software like this : https://ttsmp3.com and create 0-9 mp3 files.

Copy your mp3 files (10 files) to "Audio" folder.

Then, create a folder for voice captcha files. For example "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);

And view file for example "My-Page" is :

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

Remember, if you want to delete old voice captcha files, in the "captcha_helper", find this line :

@unlink($img_path.$filename);

And add below code after it :

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

Note : When you record your own voice, you can add audio effects on your voice or use the voices of several people to create different files. These ways enhance security and makes it harder for robots to detect sound.

Upvotes: 1

Related Questions