Through the previous section, we have well grasped the essence of image processing.
Through the knowledge we have. With some simple extensions, we can write: one of the most commonly used little things on the Internet - verification code.
Verification code is one of the most commonly used means to prevent machines from automatically registering and machines automatically swiping tickets.
Let’s take a look at the effect of the verification code:
We will reason about the implementation process based on the above effect.
Implementation process:
1. Generate a canvas with specified width and height
2. Prepare the string that needs to be generated
3. Each time Execute and let the background be filled with a random color (light color)
4. Draw random interference elements on the canvas (random points, random lines, random arcs, etc. are acceptable, but they must not overly affect the user Visual)
5. Write 4 words
6. Output the header and tell the browser to display it according to a certain type
7. Output the image
8. Destroy image resources
The above steps explain clearly every step of implementing the verification code. Let’s reason and implement it based on this process.
First step, create the canvas
$img = imagecreate($width, $height);
We can define a custom width and height . Make a function and pass in width and height through the function. In this way, the size of the captcha image can be modified.
function check_code($width = 100, $height = 50) { $img = imagecreate($width, $height); }
The second step is to generate the text displayed by the verification code
The text of the verification code usually contains numbers and letters. In order to distinguish the verification code characters. We can use 0-9A-Za-Z. But sometimes the distinction between 0 and o, l and I is not clear. If we don’t need to process it, we can find a way to remove it:
Option 1:
Loop through 4 ascii codes, and ascii codes, and chr or sprintf('%c', the second parameter is passed ascii code) to convert the corresponding characters into the specified characters.
for ($i = 0; $i < $num; $i++) { $rand = mt_rand(0, 2); switch ($rand) { case 0: $ascii = mt_rand(48, 57); //0-9 break; case 1: $ascii = mt_rand(65, 90); //A-Z break; case 2: $ascii = mt_rand(97, 122); //a-z break; } //chr() $string .= sprintf('%c', $ascii); }
Option 2:
The solution is a bit complicated for many people, and many people cannot understand the ascci code. Is there a simpler solution? Alone there is. I can prepare the characters, then use str_shuffle to shuffle the characters and then use substr to intercept them.
//没有0,i,l,o $str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789'; $str = str_shuffle($str); $string = substr($str,0 ,3);
3. Each time it is executed, let the background be filled with a random color (light color)
The text is dark and can be seen clearly , while the background is latent.
The RGB color representation of the background is usually like this. RGB color is three color values, and these three color values are from 0-255.
And:
0-120 A low value is a dark color.
130 - 255 Usually light colors.
There are many places where picture colors are used. Therefore, I can define the function of assigning colors:
//浅色的背景函数 function randBg($img) { return imagecolorallocate($img, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255)); } //深色函数,深色的字或者点这些干 扰元素 function randPix($img) { return imagecolorallocate($img, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); }
4. Draw random interference elements on the canvas
We can randomly draw 50 pixels in the picture. The smallest position is 0,0. The largest position is the largest width or largest height.
Then use mt_rand(0, maximum width), mt_rand(0, maximum height). Then use randPix to assign colors to the canvas we created.
//画干扰元素 for ($i = 0; $i < 50; $i++) { imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), randPix($img)); }
5. Write 4 characters
$string is a string, and string$string[0] is the first character of the character one character, and so on.
So, I can use the imagechar function to write text in the image.
We can use a formula to derive the x and y coordinates of written text.
X position = image width/number of characters (4) * number of loops. The result obtained is rounded and multiplied by the number of times each loop is performed. Assume that the image is 100 widths, then: the first time is written at 0, the second time is 25, the third time is 50, and the fourth time is 75.
Y position = mt_rand(0,image height - 15).
We can deduce the following code and write the corresponding text:
for ($i = 0; $i < $num; $i++) { $x = floor($width / $num) * $i; $y = mt_rand(0, $height - 15); imagechar($img, 5, $x, $y, $string[$i], randPix($img)); }
6. Output the header and tell the browser to display it according to a certain type
We know that the output functions of image types have the following characteristics: imagejpeg, imagepng, imagegif and other characteristics.
The mime type of the image is image/jpeg, image/png, image/gif, etc.
Therefore, we can declare a variable:
$imagetype = 'jpeg';
$imagetype = 'png';
$imagetype = 'gif';
Execute when outputting the header type:
$header = 'Content-type:image/' . $imagetype;
The output of the execution function can be spliced into a variable function:
$func = 'image' . $type;
If our system supports a type, use function_exists to detect whether the function exists. If it exists, the system supports this type; if it does not exist, it does not support this type.
So the code can be written like this:
$func = 'image' . $type; $header = 'Content-type:image/' . $type; if (function_exists($func)) { header($header); //变为了imagejpeg等 $func($img); } else { echo '图片类型不支持'; }
8. Destroy the resource and return the characters
during verification later. Everyone needs to display the verification code. Moreover, the verification characters input by the user also need to match the verification characters in the image.
So the verification characters must be returned for later saving and use.
imagedestroy($img); return $string;
Let’s take a look at all the prepared PHP function files. We will encapsulate the above code into a unified function for everyone to learn and use:
<?php check_code(); function check_code($width = 100, $height = 50, $num = 4, $type = 'jpeg') { $img = imagecreate($width, $height); $string = ''; for ($i = 0; $i < $num; $i++) { $rand = mt_rand(0, 2); switch ($rand) { case 0: $ascii = mt_rand(48, 57); //0-9 break; case 1: $ascii = mt_rand(65, 90); //A-Z break; case 2: $ascii = mt_rand(97, 122); //a-z break; } //chr() $string .= sprintf('%c', $ascii); } //背景颜色 imagefilledrectangle($img, 0, 0, $width, $height, randBg($img)); //画干扰元素 for ($i = 0; $i < 50; $i++) { imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), randPix($img)); } //写字 for ($i = 0; $i < $num; $i++) { $x = floor($width / $num) * $i + 2; $y = mt_rand(0, $height - 15); imagechar($img, 5, $x, $y, $string[$i], randPix($img)); } //imagejpeg $func = 'image' . $type; $header = 'Content-type:image/' . $type; if (function_exists($func)) { header($header); $func($img); } else { echo '图片类型不支持'; } imagedestroy($img); return $string; } //浅色的背景 function randBg($img) { return imagecolorallocate($img, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255)); } //深色的字或者点这些干 扰元素 function randPix($img) { return imagecolorallocate($img, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); } ?>