Php utf8 page verification code Picture Chinese garbled characters

Source: Internet
Author: User

 

The code is as follows: Copy code

<?
Session_start ();


// Generate a random number
For ($ I = 0; $ I <4; $ I ++ ){
$ Rand. = dechex (rand (1, 15 ));
}
$ _ SESSION ['checkpic '] = $ rand;

$ Im = imagecreatetruecolor (, 30); // canvas to create a true color image

// Set the color
$ Bg = imagecolorallocate ($ im, 0, 0); // red, green, and blue background color
$ Te = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); // font color

// Draw lines
For ($ I = 0; $ I <3; $ I ++ ){
$ Te2 = imagecolorallocate ($ instant, rand (0,255), rand (0,255), rand (0,255 ));
Imageline ($ im, rand (0,100), 0, rand (0,100), 30, $ te2); // coordinates (x1, y1) to coordinates (x2, y2)
}

// Draw Point
For ($ I = 0; I I <200; $ I ++ ){
Imagesetpixel ($ im, rand () % 100, rand () % 30, $ te2 );
}


// Output Chinese characters
$ Str = iconv ("gbk", "UTF-8", "Happy New Year! "); // Determine the Chinese text to draw
Imagettftext ($ im, 12, 3, 20, 20, $ te, 'msyhbd. Ttf', 'Chinese en ');

// Write the string in the upper left corner of the image
// Imagestring ($ im, 5, rand (0, 50), rand (0, 15), $ rand, $ te );

// Output image
Header ("Content-type: image/jpeg"); // file type
Imagejpeg ($ im );

 

?>

Analyze causes and solutions

The code is as follows: Copy code

$ Str = iconv ("gbk", "UTF-8", "Happy New Year! "); // Determine the Chinese text to draw
Imagettftext ($ im, 12, 3, 20, 20, $ te, 'msyhbd. Ttf', 'Chinese en ');

-->
This should be the case

The code is as follows: Copy code
$ Str = iconv ("gbk", "UTF-8", "Happy New Year! "); // Determine the Chinese text to draw
Imagettftext ($ im, 12, 3, 20, 20, $ te, 'msyhbd. Ttf', $ str );

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.