Creating a canvas has two functions:
Imagecreate (width, height);
Function: Create a canvas with only 256 colors!
Imagecreatetruecolor (width, height);
Function: Create true color canvas.
What is true color? 24-bit true color
Three primary colors: rgb
Red: 0-255 can be stored in one byte (8 bits).
Green: 0-255 can be stored in one byte (8 bits).
Blue: Only one byte (8 bits) can store 0-255.
So a * * * can represent 28*28*28 = 224 colors!
Got a canvas resource!
Step 2: Create a color for the canvas.
note:
All colors that need to be presented on the canvas must be assigned to the canvas in advance!
Through a function to achieve:
Imagecolorallocate (image, red, green, blue);
Among them: img is a canvas resource, and the rgb behind it represents a color!
The function is used to get a handle to a color (it is an int value).
Step 3: Draw text (painting)
Text can be divided into two types:
One is simple text, ASCII code, and the other is complex text, such as Chinese!
Drawing text requires a function:
imagestring(img,size,x,y,string,color);
These include:
Img: canvas resource
Size: the size of the file, which can only be 1-5, and 5 is the largest.
X, y: starting coordinates
String: Text content
Color: It's a colored handle.
Step 4: Output or save the picture.
Output picture
At this point, you need to use the function: imagepng|imagejpeg|imagegif.
picture
At this point, img is the canvas resource!
At this point, we should go to the server and tell the browser when responding that the data fed back to the browser is pictures instead of html files!
At this point, we need to use the header function to set the response header information before outputting the picture:
Title ("Content Type: Image /png")
Save picture
Still use a series of functions such as imagepng, and only use the second parameter, which is a saved path.
There is one place that needs special attention:
When using the imagepng function to output a picture, there cannot be any output in front!
Therefore, in order to avoid similar problems in the project, you can empty the data buffer before outputting the picture:
At this time, you need to use a function ob_clean to complete it!
It is suggested to use ob_clean to clean the data buffer between output pictures at any time!
Generate a random verification code string
Step 1: Piece together an array (including uppercase and lowercase letters and numbers).
Basic strategy: use two functions to complete.
Range ('a',' z');
Array_merge: such as $ arr = array _ merge (range ('a',' z'), range ('a',' z'), range (0,9));
Step 2: Disrupt the array.
Use the shuffle function:
Step 3: Use array_rand to randomly extract the subscript values of several arrays.
Step 4: Traverse the obtained subscript to obtain the value of the original array.
Making verification code
Function to be used
Fill the background
Use the imagefill function
imagefill(img,x,y,color);
These include:
Img: canvas resource
X, y: coordinate point
Color: Handle to the color to fill.
Fill is to fill a continuous area with the same color!
Add interference lines
The essence of adding interference lines is to draw straight lines!
Image lines (img, x 1, y 1, x2, y2, color) are required.
These include:
Img: canvas resource
X 1, y 1, x2, y2: the coordinates of two points.
Color: color handle
Add noise (interference point)
The so-called noise is the interference point, which is the pixel!
Imagesetpixel(img, x, y, color) is required;
Code implementation
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
// 1. Create a canvas
$ img = imagecreatetruecolor( 170,40);
//2. Fill the background color
//2. 1 Create the color of the background.
$ back color = imagecolorlallocate($ img,mt_rand( 180,255),mt_rand( 180,255),mt_rand( 180,255));
//2.2 Fill the background
imagefill($img,0,0,$ back color);
//3. Generate random verification code string
//3. 1 Use the arrar_merge and range functions to piece together an array.
$arr=array_merge(range('a ',' Z '),range('a ',' Z '),range(0,9));
//3.2 Scramble the array
Shuffle ($ arr);
//3.3 Randomly extract the subscript values of several arrays.
$rand_key=array_rand($arr,4);
//3.4 Traverse the obtained subscripts to get the value of the original array.
$ str =“”;
foreach ($rand_key as $value) {
$str。 = $ arr[$ value]; //
}
//8. Save the verification code string to the session.
session_start()。
$ _ SESSION[' captcha ']= $ str;
//4. String the verification code on the picture.
//4. 1 Calculate character spacing
$ span = ceil( 170/(4+ 1));
for($ I = 1; $ i & lt=4; $i++){
$ string color = imagecolorlallocate($ img,mt_rand(0, 100),mt_rand(0, 100),mt_rand(0, 100));
imagestring($img,5,$i*$span, 10,$str[$i- 1],$ string color); //Draw a string horizontally.
}
//5. Add interference lines
for($ I = 1; $ i & lt=8; $i++){
//5. 1 Create interference line color
$ line color = imagecolorlocallocate($ img,mt_rand( 100, 180),mt_rand( 100, 180),mt_rand( 100, 180);
imageline($img,mt_rand(0, 169),mt_rand(0, 169),mt_rand(0,39),mt_rand(0,39),$ linecolor
}
//6. Add noise (interference point)
for($ I = 1; $ i & lt= 170*40*0.05; $i++){
$ pixel color = imagecolorlallocate($ img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($img,mt_rand(0, 169),mt_rand(0,39),$ pixel color);
}
//7. Output pictures
//Set the response header information
Header ("content-type: image/png");
//Clean up the data cache area
ob _ clean();
//output pictures
imagepng($ img);
//Save the picture