PHP implements Chinese character verification code

墨辰丷
Release: 2023-03-30 19:50:02
Original
3032 people have browsed it

This article mainly introduces the implementation of Chinese character verification code in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

Now more and more websites are beginning 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('栀', '子', '花', '开'); 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.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Basic knowledge and application of php design patterns

php image file operation encapsulation class

Those knowledge points that are often confused in PHP

The above is the detailed content of PHP implements Chinese character verification code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!