📜 ⬆️ ⬇️

Creating a captcha with your own hands.

I recently decided that a captcha should be urgently put on the registration page, the bots became so impudent that they feel good there and even more bots are called.
Upon request in the search engine "Captcha Script" , there are so many ready-made options - that it is a sin to complain, but they all do not suit me - some are too scary, others are too sophisticated.
Resolved! Making a captcha with your own hands. The topic is certainly beaten, but I will share the process of work.

From the captcha I expect, first of all, ergonomics, my site is colorful and the captcha should be appropriate.

On a white background, drawing numbers is not for me, so we make a rectangle and fill it with different squiggles.
The size of the background 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.
')
image

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

image

Saving each digit to a separate file and setting it to the height: 22px; I'm starting 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 random number from 1 to 30 is taken, the maximum threshold is the number of files with numbers (each file is signed from 0.png to 30.png). Since the numbers are stored in the correct order, you can find out which of them 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_start(); $_SESSION['captha'] = "{$pic1_value}{$pic2_value}{$pic3_value}{$pic4_value}{$pic5_value}{$pic6_value}"; 


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

 $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); 


We also copy the numbers in the captcha - randomly changing the position vertically and horizontally, as well as the size of the digit 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 to be set only in the beginning of the php file the desired title:

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


And display the drawn captcha in the browser:
 imagejpeg($dest); 


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

 imagedestroy($back); imagedestroy($dest); imagedestroy($var1); imagedestroy($var2); imagedestroy($var3); imagedestroy($var4); imagedestroy($var5); imagedestroy($var6); 


Save, check.

image

We put in the right place on the site.

image

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

image

As needed, 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 an example, use on health.

Source: https://habr.com/ru/post/164733/


All Articles