PHP Production Verification Code

Source: Internet
Author: User

We know that the verification code is a picture, but PHP itself does not support the processing of images, need to rely on the GD library.

Let's take a look at the use of the GD Library to implement verification code production:

Look directly at the code ~

<?php
Make Verification Code

$im =imagecreatetruecolor (200,50);

Create a background color

$color =imagecolorallocate ($im, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));

Fill Canvas

Imagefill ($im, 0,0, $color);

Get Authenticode Data
$str = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789 ';
$captcha = ";
for ($i =0; $i <5; $i + +)
{
$captcha. = $str [Mt_rand (0,strlen ($STR)-1)];
}

Set Text color
$str _color=imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100));

Write text to a picture

Imagestring ($im, 5,80,20, $captcha, $str _color);

Increase the interference line (to the top of the word)
for ($j =0; $j <5; $j + +)
{
$line _color=imagecolorallocate ($im, Mt_rand (100,200), Mt_rand (100,200), Mt_rand (100,200));

Crossed

Imageline ($im, Mt_rand (0,200), Mt_rand (0,50), Mt_rand (0,200), Mt_rand (0,50), $line _color);
}
Draw points
for ($i =0; $i <300; $i + +)
{
$line _pixel=imagecolorallocate ($im, Mt_rand (100,200), Mt_rand (100,200), Mt_rand (100,200));

Imagesetpixel ($im, Mt_rand (0,200), Mt_rand (0,50), $line _pixel);
}
Header (' content-type:image/png ');

Imagepng ($im);

PHP Production Verification Code

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.