Home > Backend Development > PHP Tutorial > The idea of ​​​​implementing a simple Chinese character verification code in PHP

The idea of ​​​​implementing a simple Chinese character verification code in PHP

藏色散人
Release: 2023-04-07 13:06:01
forward
2405 people have browsed it

Nowadays, more and more websites have begun to use Chinese character verification codes, which not only increases the intimacy of our people, but also increases the difficulty of machine cracking. Here I will briefly explain it.

Create background canvas

$image = imagecreatetruecolor(200, 60);
$background = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background);
Copy after login

Draw interference points

for ($i=0; $i < 300; $i++) { 
    $pixColor = imagecolorallocate($image, rand(150, 240), rand(150, 240), rand(150, 240));
    $pixX = rand(10, 190);
    $pixY = rand(5, 55);
    imagesetpixel($image, $pixX, $pixY, $pixColor);
}
Copy after login

Draw interference lines

//4条水平线
for ($i=0; $i < 5; $i++) { 
    $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineX1 = 0;
    $lineX2 = 300;
    $lineY1 = ($i + 1) * 12;
    $lineY2 = ($i + 1) * 12;
    imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}
//10条垂直线
for ($i=0; $i < 30; $i++) { 
    $lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineX1 = ($i + 1) * 10;
    $lineX2 = ($i + 1) * 10;
    $lineY1 = 0;
    $lineY2 = 60;
    imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}
Copy after login

Drawing Chinese characters

$text = array(&#39;栀&#39;, &#39;子&#39;, &#39;花&#39;, &#39;开&#39;);
for ($i=0; $i < 4; $i++) {
    $textColor = imagecolorallocate($image, rand(20, 100), rand(20, 100), rand(20, 100));
    $textX = $i * 50 + 10;
    $textY = rand(40, 60);
    imagettftext($image, 30, rand(20, 50), $textX, $textY, $textColor, "/Library/Fonts/华文仿宋.ttf", $text[$i]);
}
Copy after login

Note here, the font file must support Chinese

Encoding must use utf-8, do you remember to convert gbk Chinese? [iconv function can help you]

Output image

header("Content-Type:image/png");
imagepng($image);
Copy after login

Destroy resources

imagedestroy($image);
Copy after login

After a rough process, let’s do it , the Chinese verification code will be displayed. Of course, when a website is used, there will be a Chinese character library seed, from which a specific number of Chinese characters are randomly selected and displayed, and finally recorded in the session for verification.

The above is the detailed content of The idea of ​​​​implementing a simple Chinese character verification code in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template