In the light of recent events, I dug such a thing, I decided to share it with the community.
The original article is in English, so here is only a free translation.
It means so. What to get hold of at the same time and graphic and sound captcha, you need to start the whole thing
download . Then copy it to a convenient place and read on. By the way, the license for this joy is BSD. Who understands, he chooses =).
In the directory with the downloaded script the most important is the file php-captcha.inc.php. We will connect it everywhere. In addition, you will need TrueType fonts. If they are not at hand, you can download
it from somewhere .
')
Actually, the process. Graphic captcha. We write this to the file visual-captcha.php.
<? php
require ('php-captcha.inc.php');
$ aFonts = array ('fonts / VeraBd.ttf', 'fonts / VeraIt.ttf', 'fonts / Vera.ttf');
$ oVisualCaptcha = new PhpCaptcha ($ aFonts, 200, 60);
$ oVisualCaptcha-> Create ();
?>
Sound captcha goes to audio-captcha.php.
<? php
require ('php-captcha.inc.php');
$ oAudioCaptcha = new AudioPhpCaptcha ('/ usr / bin / flite', '/ tmp /');
$ oAudioCaptcha-> Create ();
?>
/ tmp - temporary storage of the created file.
Here it is worth stopping and noticing that a certain flite binary was used. It is not difficult to guess, you must first
download and test.
flite -t ​​'Hello World' -o hello.wav
How now to display created in the document?
<p> <img src = "visual-captcha.php" width = "200" height = "60" alt = "Visual CAPTCHA" /> </ p>
<p> <a href="audio-captcha.php"> Can't see the image? Click for audible version </a> </ p>
Here the author separately notes that it is necessary to stick the sound captcha after the graphic one. This is for the correct creation of random code.
Everything, captcha is created and displayed. How to check incoming client now?
<? php
require ('php-captcha.inc.php');
if (PhpCaptcha :: Validate ($ _ POST ['user_code'])) {
echo 'Valid code entered';
} else {
echo 'Invalid code entered';
}
?>
The important thing is that the Validate method is called statically.
That's all. As a bonus - some possibilities for configuring the created captcha.
SetWidth (int iWidth) - set the width of the image. The default is 200px.
SetHeight (int iHeight) - set the height of the image. The default is 50px.
SetNumChars (int iNumChars) - the number of characters displayed. The default is 5.
SetNumLines (int iNumLines) is the number of noise lines. The default is 70.
DisplayShadow (bool bShadow) - display or not the shadow of elements.
SetOwnerText (sting sOwnerText) - the text “owner” displayed at the bottom of the picture. Designed to ensure that your captchas are not scrapped lemmings.
SetCharSet (variant vCharSet) is a set of characters used in captcha. Default is AZ. You can specify an array of characters, such as array ('1', 'G', '3') or a string of characters, or even character ranges, such as 'az, AZ, 0.3.7'.
CaseInsensitive (bool bCaseInsensitive) - to carry out checks with regard to the case of characters, or without. If set to “false”, you need to pass “false” as the second parameter to the “Validate” function when checking the entered code.
SetBackgroundImages (variant vBackgroundImages) - define one (string) or several (array) pictures that will be displayed instead of noise lines. When specifying multiple images, the library randomly selects one.
SetMinFontSize (int iMinFontSize) - the minimum displayed font size. The default is 16.
SetMaxFontSize (int iMaxFontSize) - the maximum displayed font size. The default is 25.
UseColour (bool bUseColour) - when set to “true”, displays noise lines and symbols in random colors.
SetFileType (string sFileType) - output format: jpeg, gif or png. Default jpeg.
All configuration parameters must be set before calling Create ().
Sample call and color picture.
<? php
require ('php-captcha.inc.php');
$ aFonts = array ('fonts / VeraBd.ttf', 'fonts / VeraIt.ttf', 'fonts / Vera.ttf');
$ oPhpCaptcha = new PhpCaptcha ($ aFonts, 200, 50);
$ oPhpCaptcha-> UseColour (true);
$ oPhpCaptcha-> Create ();
?>
The result was a picture:

Long live useful and convenient resources! =).