Verification code appears on canvas

Source: Internet
Author: User

<?php
/*
* Verification Code Class
*/
Class Captcha
{
Member properties
Private $_width = 100; Canvas default Width
Private $_height = 25; Default height of canvas


Member Methods
Generate an image and output it to the browser
Public Function Makeimage ()
{
1. Create a canvas first (create an image resource in memory)
$image = Imagecreatetruecolor ($this->_width, $this->_height);

2. Fill the canvas with color, otherwise the default is black very scary allocate assignment
$color = Imagecolorallocate ($image, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagefill ($image, 0, 0, $color);

Create a random text



4. Output this canvas directly in the browser
Header ("Content-type:image/png");
Generates an image if the second parameter is added to save to a local
Imagepng ($image);
5. Destroying in-memory image resources
Imagedestroy ($image);
}
Functions that produce random text
Public Function Makecode ()
{
Random text may be numbers, letters
Range () produces a collection of characters from A through Z (array)
$upper _str = range (' A ', ' Z ');
$lower _str = Range (' A ', ' Z ');
$num = range (1,9);

Combine the above three numbers and
$data = Array_merge ($upper _str, $lower _str, $num);
To make the resulting numbers more random, first scramble the order
Shuffle ($data);

Randomly remove 4 from the above array
$randoms = Array_rand ($data, 4);

Get the corresponding character by subscript
$str = ";
foreach ($randoms as $v) {
$str. = $data [$v];
}
Echo ' <pre> ';
Var_dump ($STR);
}

}

$captcha = new Captcha ();
$captcha-Makeimage ();
$captcha-Makecode ();


Verification code appears on canvas

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.