Generate Verification Code in PHP

Source: Internet
Author: User

Create a picture
$im = Imagecreatetruecolor (100,30);
Generate color, when the first call to generate a color method, is to generate a background color, default is black
If you want to customize the background color, use the Imagefill function
$BG = Imagecolorallocate ($im, 200,200,200);
Imagefill ($im, 0,0, $BG);
The second call to this method is the ability to generate the text above the picture or the color of other styles
$te = Imagecolorallocate ($im, 0,0,0);

$te 2 = imagecolorallocate ($im, 255,255,255);

Create an interference line, just draw a line
$posLineX 1 = rand (6,30);
$posLineX 2 = rand (30,60);
for ($i =0; $i <5; $i + +) {
$posLinY 1=rand (2,10);
$posLinY 2=rand (11,28);
Imageline ($im, $posLineX 1, $posLinY 1, $posLineX 2, $posLinY 2, $te 2);
$posLineX 1 + = rand (30,60);
$posLineX 2 + = rand (61,98);
}

for ($i =0; $i <90; $i + +) {
$te 3 = imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($im, rand (2,100), Rand (2,30), $te 3);
}


Create text on top of a picture
Imagestring ($im, 5,rand (3,70), Rand (3,15), $rand, $te);

$posX = rand (6,45);
for ($i =0; $i <4; $i + +) {
$posY = rand (2,15);
Imagestring ($im, rand (2,5), $posX, $posY, substr ($rand, $i, 1), $te);
$posX + = rand (8,20);
}

To take PHP as a picture output, you must give the file a header declaration

Ob_clean ();
Header ("Content-type:image/jpeg");
Final image generation
Imagejpeg ($im);

The resulting picture can be in the form of JPEG and GIF and PNG

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.