PHP realizes simple Chinese character verification code _php skill

Source: Internet
Author: User

Now more and more websites are beginning to use Chinese character verification code, both increase the intimacy of our countrymen, but also increase the difficulty of the machine to crack, here I will simply rough say ...

Create a background canvas

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

Draw the jamming point

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

Draw Interference Line

4 Horizontal line for
($i =0 $i < 5 $i + +) { 
  $lineColor = imagecolorallocate ($image, Rand, Rand), Rand (5 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), RA nd (m));
  $lineX 1 = ($i + 1) *;
  $lineX 2 = ($i + 1) *;
  $lineY 1 = 0;
  $lineY 2 =;
  Imageline ($image, $lineX 1, $lineY 1, $lineX 2, $lineY 2, $lineColor);


Draw Chinese Characters

$text = Array (' zhi ', ' zi ', ' flower ', ' open ');
For ($i =0 $i < 4 $i + +) {
  $textColor = imagecolorallocate ($image, Rand, Rand), Rand (a)); 
    $textX = $i * +;
  $textY = rand (a);
  Imagettftext ($image, Rand (a), $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 Chinese remember to turn? "Iconv function can help you."

Output image

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

Destroying resources

Imagedestroy ($image);

After a rough do it, the Chinese verification code is also displayed, of course, the general Web site when the use of a Chinese character library seed, from the inside randomly out a specific number of Chinese characters display, and finally is recorded to the session to verify.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.