It is relatively simple to generate a verification code and it is not difficult to draw a picture. However, everyone does not like to read the manual or do it by themselves. It is generally because I copied a piece of code online, in fact, drawing is quite interesting.
However, the verification code generated by a common Font does not have any "combat power". It is easy to identify the content of the verification code by the program, and then it is easily watered down, it is not a simple task to generate a good Verification Code image. Some large companies specialize in one department to do such a thing. However, I only know the demonstration here. The general purpose should be enough.
The following two functions can be used to draw Chinese content and English content.
/**
* Generate Chinese characters,
*
* The GD library, iconv, or mbstring library, and corresponding font files must exist.
*/
Function makechinesecharpic ($ STR = 'Chinese characters '){
// Transcoding
If (function_exists ('iconv ')){
$ STR = iconv ('gbk', 'utf-8', $ Str );
} Elseif (function_exists ('mb _ convert_encoding ')){
$ STR = mb_convert_encoding ($ STR, 'utf-8', 'gbk ');
} Else {
Exit ("not iconv charset ");
}
// Generate an image
$ Im = imagecreate (80, 30 );
// Background color
$ BKG = imagecolorallocate ($ im, 0,255,255 );
// Font color
$ CLR = imagecolorallocate ($ im, 0,128,255 );
// Font file path
$ Font = "C:/Windows/fonts/simhei. TTF ";
// Use a font to draw TTF content
Imagettftext ($ im, 13, 0, 5, 20, $ CLR, $ font, $ Str );
//--------------------------------------------------
// Simhei. TTF, simsun. TTC, simli. TTF, simkai. TTF, child circle: simyou. TTF
// 文: stxingka. TTF, 文: stxinwei. TTF, 文: stcaiyun. TTF, 文: stfangso. TTF
// 正: fzytk, 正: fzstk. TTF
//--------------------------------------------------
// Clear resources after output
Header ("Content-Type: image/PNG ");
Imagepng ($ IM );
Imagedestroy ($ IM );
}
/**
* Draw English characters (must have a GB Library)
*/
Function makeenglishcharpic ($ STR = 'abc12 '){
// Generate an image
$ Im = imagecreatetruecolor (65, 20 );
// Background color
$ BG = imagecolorallocate ($ im, 0, 0, 0 );
// Font color
$ Textcolor = imagecolorallocate ($ im, 0,255,255 );
// Draw
Imagestring ($ im, 5, 5, 2, $ STR, $ textcolor );
// Clear resources after output
Header ("Content-Type: image/JPEG ");
Imagejpeg ($ IM );
Imagedestroy ($ IM );
}
If you are interested, you are welcome to add interference elements or perform deformation on the images to make those image recognition programs unrecognizable. (I have been very busy in the last two months. I haven't written anything for a long time. I wrote an article. Hey, this table is strange)