PHP implementation of Chinese character verification code

Source: Internet
Author: User
This article mainly introduces PHP implementation of Chinese character verification code, interested in the reference of friends, I hope to help you.

Now more and more sites are beginning to use Chinese character verification code, not only to increase the intimacy of our people, but also to increase the difficulty of the machine crack, here I will simply rough said ...

Create a background canvas

$image = imagecreatetruecolor, $background = Imagecolorallocate ($image, 255, 255, 255); Imagefill ($image, 0, 0, $b Ackground);

Draw interference points

for ($i =0; $i < $i + +) {   $pixColor = imagecolorallocate ($image, rand (+), rand (150, 240)) ;  $pixX = rand (ten);  $pixY = rand (5, +);  Imagesetpixel ($image, $pixX, $pixY, $pixColor);}

Draw interference Lines

4 horizontal lines for ($i =0; $i < 5; $i + +) {   $lineColor = imagecolorallocate ($image, Rand), Rand (50, 15 0));  $lineX 1 = 0;  $lineX 2 =;  $lineY 1 = ($i + 1) *;  $lineY 2 = ($i + 1) *;  Imageline ($image, $lineX 1, $lineY 1, $lineX 2, $lineY 2, $lineColor);} 10 vertical lines for ($i =0; $i < $i + +) {   $lineColor = imagecolorallocate ($image, Rand), Rand (50, ));  $lineX 1 = ($i + 1) * TEN;  $lineX 2 = ($i + 1) * TEN;  $lineY 1 = 0;  $lineY 2 =;  Imageline ($image, $lineX 1, $lineY 1, $lineX 2, $lineY 2, $lineColor);}

Painting Chinese characters

$text = Array (' Gardenia ', ' child ', ' flower ', ' open '); for ($i =0; $i < 4; $i + +) {  $textColor = imagecolorallocate ($image, Rand (20, 100) , rand (+), rand (+,));  $textX = $i * + ten;  $textY = rand (at +);  Imagettftext ($image, Rand, $textX, $textY, $textColor, "/library/fonts/Chinese Imitation ttf", $text [$i]);}

Note here that the font file must support Chinese

Encoding to use UTF-8,GBK's Chinese remember to turn it "iconv function can help you"

Output image

Header ("Content-type:image/png"); Imagepng ($image);

Destroying resources

Imagedestroy ($image);

After a rough-and-go, Chinese verification code is also displayed, of course, the general site will be used when the use of a Chinese character library seed, from the inside randomly take out a specific number of Chinese characters display, and finally recorded to the session to verify.

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

Basic knowledge and application of PHP design pattern

PHP Picture file Operation Encapsulation Class

The often confusing points of knowledge in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.