This article mainly introduces PHP implementation of Chinese character verification code, interested in the reference of friends, I hope to help you.
Now more and more sites are beginning to use Chinese character verification code, not only to increase the intimacy of our people, but also to increase the difficulty of the machine crack, here I will simply rough said ...
Create a background canvas
$image = imagecreatetruecolor, $background = Imagecolorallocate ($image, 255, 255, 255); Imagefill ($image, 0, 0, $b Ackground);
Draw interference points
for ($i =0; $i < $i + +) { $pixColor = imagecolorallocate ($image, rand (+), rand (150, 240)) ; $pixX = rand (ten); $pixY = rand (5, +); Imagesetpixel ($image, $pixX, $pixY, $pixColor);}
Draw interference Lines
4 horizontal lines for ($i =0; $i < 5; $i + +) { $lineColor = imagecolorallocate ($image, Rand), Rand (50, 15 0)); $lineX 1 = 0; $lineX 2 =; $lineY 1 = ($i + 1) *; $lineY 2 = ($i + 1) *; Imageline ($image, $lineX 1, $lineY 1, $lineX 2, $lineY 2, $lineColor);} 10 vertical lines for ($i =0; $i < $i + +) { $lineColor = imagecolorallocate ($image, Rand), Rand (50, )); $lineX 1 = ($i + 1) * TEN; $lineX 2 = ($i + 1) * TEN; $lineY 1 = 0; $lineY 2 =; Imageline ($image, $lineX 1, $lineY 1, $lineX 2, $lineY 2, $lineColor);}
Painting Chinese characters
$text = Array (' Gardenia ', ' child ', ' flower ', ' open '); for ($i =0; $i < 4; $i + +) { $textColor = imagecolorallocate ($image, Rand (20, 100) , rand (+), rand (+,)); $textX = $i * + ten; $textY = rand (at +); Imagettftext ($image, Rand, $textX, $textY, $textColor, "/library/fonts/Chinese Imitation ttf", $text [$i]);}
Note here that the font file must support Chinese
Encoding to use UTF-8,GBK's Chinese remember to turn it "iconv function can help you"
Output image
Header ("Content-type:image/png"); Imagepng ($image);
Destroying resources
Imagedestroy ($image);
After a rough-and-go, Chinese verification code is also displayed, of course, the general site will be used when the use of a Chinese character library seed, from the inside randomly take out a specific number of Chinese characters display, and finally recorded to the session to verify.
Summary : The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Basic knowledge and application of PHP design pattern
PHP Picture file Operation Encapsulation Class
The often confusing points of knowledge in PHP