Do-it-yourself captcha creation

Just the other day, I decided that I urgently need to put captcha on the registration page, the bots are so insolent that they feel good and call even more bots without it.
Upon request in the search engine "CAPTCHA script" , so many ready-made options appear - it’s a sin to complain, but they all do not suit me - some are too scary, others are too complicated.
Solved! We make captcha with our own hands. The topic is certainly beaten, but I will share the process of work.

First of all, I expect ergonomics from captcha, the site is colorful and captcha should be appropriate.

On a white background, drawing numbers is not for me, so make a rectangle and fill it with different squiggles.
The background size is 3 times larger than the size of the future captcha. in the process of generating a picture, this background will randomly change its position - making each captcha unique.


Now we need numbers. In the script options that were offered to me - which generate captcha, the numbers were drawn with text - their color and size changed.
I went the other way and drew 30 different numbers - 3 different fonts and painted them with a gradient in the colors of the site.


By storing each digit in a separate file and setting it to size height: 22px; I begin to code: CAPTCHA

will consist of 6 digits therefore. Numbers are assigned randomly.

$pic1 = rand(1, 30);
$pic2 = rand(1, 30);
$pic3 = rand(1, 30);
$pic4 = rand(1, 30);
$pic5 = rand(1, 30);
$pic6 = rand(1, 30);

For each variable, a digit from 1 to 30 is taken randomly, the maximum threshold is the number of files with numbers (each file is signed from 0.png to 30.png). Since the numbers are saved in the correct order, you can find out which one is drawn in the file by determining the last digit from the file name:

$pic1_value = substr($pic1, strlen($pic1)-1, 1);
$pic2_value = substr($pic2, strlen($pic2)-1, 1);
$pic3_value = substr($pic3, strlen($pic3)-1, 1);
$pic4_value = substr($pic4, strlen($pic4)-1, 1);
$pic5_value = substr($pic5, strlen($pic5)-1, 1);
$pic6_value = substr($pic6, strlen($pic6)-1, 1);

That is, the number 2 can be stored in the file 2.png, 12.png, 22.png, etc.

Be sure to remember the captcha code and write it into the session.

$_SESSION['captha'] = "{$pic1_value}{$pic2_value}{$pic3_value}{$pic4_value}{$pic5_value}{$pic6_value}";

Now that we know the numbers - we need to draw them in a picture 112x38px in size;

$dest = imagecreatetruecolor(112,38); # Сюда мы и будем рисовать
$back = imagecreatefromjpeg("../../pic/captha/back.jpg"); # Задний фон

We also open the numbers we need:

$var1 = imagecreatefrompng("../../pic/captha/{$pic1}.png");
$var2 = imagecreatefrompng("../../pic/captha/{$pic2}.png");
$var3 = imagecreatefrompng("../../pic/captha/{$pic3}.png");
$var4 = imagecreatefrompng("../../pic/captha/{$pic4}.png");
$var5 = imagecreatefrompng("../../pic/captha/{$pic5}.png");
$var6 = imagecreatefrompng("../../pic/captha/{$pic6}.png");

The background is randomly moved horizontally and vertically so that the background does not go beyond the captcha.

imagecopyresampled($dest, $back, 0, 0, rand(0, 224), rand(0, 106), 112, 38, 112, 38); 

The numbers themselves we also copy into captcha - randomly changing the position vertically and horizontally, as well as the size of the digits by a maximum of 4px.

imagecopyresampled ($dest, $var1, rand(0, 6), rand(2, 10), 0, 0, imagesx($var1) - rand(0, 4), imagesy($var1) - rand(0, 4), imagesx($var1), imagesy($var1)); 
imagecopyresampled ($dest, $var2, rand(18, 24), rand(2, 10), 0, 0, imagesx($var2) - rand(0, 4), imagesy($var2) - rand(0, 4), imagesx($var2), imagesy($var2)); 
imagecopyresampled ($dest, $var3, rand(36, 42), rand(2, 10), 0, 0, imagesx($var3) - rand(0, 4), imagesy($var3) - rand(0, 4), imagesx($var3), imagesy($var3)); 
imagecopyresampled ($dest, $var4, rand(54, 60), rand(2, 10), 0, 0, imagesx($var4) - rand(0, 4), imagesy($var4) - rand(0, 4), imagesx($var4), imagesy($var4)); 
imagecopyresampled ($dest, $var5, rand(72, 78), rand(2, 10), 0, 0, imagesx($var5) - rand(0, 4), imagesy($var5) - rand(0, 4), imagesx($var5), imagesy($var5)); 
imagecopyresampled ($dest, $var6, rand(90, 96), rand(2, 10), 0, 0, imagesx($var6) - rand(0, 4), imagesy($var6) - rand(0, 4), imagesx($var6), imagesy($var6)); 

In principle, everything is ready, it remains only to set the desired header at the beginning of the php file:

header('Content-type: image/jpeg');

And output the drawn captcha to the browser:

Well and of course do not forget to clear the buffer:


Save, check.


We put in the right place on the site.


If the entered code
!= $_SESSION['captha']
- then we display the corresponding error.


As necessary, additional numbers can be copied to the captcha folder, and the maximum threshold in the lines:

$pic1 = rand(1, 30);- increase.

On this, our self-made improved and simple captcha is ready. Thank you for your attention and do not judge strictly. If someone likes the example, use your health.

Also popular now: