PHP's idea of implementing a simple Chinese character verification code
Now more and more websites are using Chinese character verification codes, which increases the intimacy of Chinese people and the difficulty of machine cracking. here I will give you a rough description...
Create a background canvas
$image = imagecreatetruecolor(200, 60);$background = imagecolorallocate($image, 255, 255, 255);imagefill($image, 0, 0, $background);
Draw interference points
for ($i=0; $i < 300; $i++) { $pixColor = imagecolorallocate($image, rand(150, 240), rand(150, 240), rand(150, 240)); $pixX = rand(10, 190); $pixY = rand(5, 55); imagesetpixel($image, $pixX, $pixY, $pixColor);}
Draw interference lines
// Four horizontal lines for ($ I = 0; $ I <5; $ I ++) {$ lineColor = imagecolorallocate ($ image, rand (50,150 ), rand (50,150), rand (50,150); $ lineX1 = 0; $ lineX2 = 300; $ lineY1 = ($ I + 1) * 12; $ lineY2 = ($ I + 1) * 12; imageline ($ image, $ lineX1, $ lineY1, $ lineX2, $ lineY2, $ lineColor );} // 10 vertical lines for ($ I = 0; $ I <30; $ I ++) {$ lineColor = imagecolorallocate ($ image, rand (50,150 ), rand (50,150), rand (50,150); $ lineX1 = ($ I + 1) * 10; $ lineX2 = ($ I + 1) * 10; $ lineY1 = 0; $ lineY2 = 60; imageline ($ image, $ lineX1, $ lineY1, $ lineX2, $ lineY2, $ lineColor );}
Draw Chinese characters
$ Text = array ('hangzhou', 'sub', 'hua', 'Kai'); for ($ I = 0; $ I <4; $ I ++) {$ textColor = imagecolorallocate ($ image, rand (20,100), rand (20,100), rand (20,100); $ textX = $ I * 50 + 10; $ textY = rand (40, 60); imagettftext ($ image, 30, rand (20, 50), $ textX, $ textY, $ textColor, "/Library/Fonts/ 文. ttf ", $ text [$ I]);}
Note that font files must support Chinese characters.
UTF-8 is used for encoding. do you need to convert gbk to chinese? [iconv function can help you]
Output Image
header("Content-Type:image/png");imagepng($image);
Destroy resources
imagedestroy($image);
After a rough look, the Chinese verification code will be displayed. of course, when a website is used, there will be a Chinese character Library seed, and a specific number of Chinese characters will be randomly retrieved from it for display, finally, the session is recorded for verification.