<?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