PHP graphics Image processing generation verification code

Source: Internet
Author: User

\ (^o^)/~

Now online more and more inseparable from the verification code, do not know the small partners know that the use of PHP GD Library can generate verification code, Σ (⊙▽⊙ "a ...

Let's start by introducing several functions that need to be used.

1.imagesetpixel ()

This function can be used to draw pixel points, in the verification code, we call "noise", is simply an artifact. I don't know if my little buddy thinks about it. The point on the verification code is generated using this function.

2.str_shuffle ()

Using this scrambled string, and then using substr () to intercept the given number of bits, you can generate a random string.

Instance:

1<?PHP2 3$img = Imagecreatetruecolor ( -, -);4$black = Imagecolorallocate ($img,0x00,0x00,0x00);5$green = Imagecolorallocate ($img,0x00,0xFF,0x00);6$white = Imagecolorallocate ($img,0xFF,0xFF,0xFF);7Imagefill ($img,0,0, $white);8 //generate a random verification code9$code = Make (5);TenImagestring ($img,5,Ten,Ten, $code, $black); One //add noise interference. A  for($i =0; $i < -; $i + +) { -Imagesetpixel ($img, Rand (0, -), Rand (0, -), $black); -Imagesetpixel ($img, Rand (0, -), Rand (0, -), $green); the } - //Join line Interference -  for($n =0; $n <=1; $n + +) { -Imageline ($img,0, Rand (0, +), -, Rand (0, +), $black); +Imageline ($img,0, Rand (0, +), -, Rand (0, +), $white); - } + //Output Verification Code AHeader"Content-type:image/png"); at imagepng ($img); - //Destroying pictures - Imagedestroy ($img); - //the function of producing random verification code - function make ($length) - { in$code ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; -     returnsubstr (Str_shuffle ($code),0, $length); to}

Effect:

PHP graphics Image processing generation verification code

Related Article

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.