PHP's idea of implementing a simple Chinese character verification code

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.