Function for outputting Chinese characters in GD

Source: Internet
Author: User
Thanks to sadly for writing the function for outputting Chinese characters in GD, I found that the strings output in this version must be pure Chinese and cannot be mixed with English. with this bug modified, share with you.

Thanks to sadly for writing the function for outputting Chinese characters in GD, I found that the strings output in this version must be pure Chinese and cannot be mixed with English. with this bug modified, share with you.

  1. // Program writen by sadly www.phpfensi.com
  2. // Modified by agun 2013/6/20
  3. Function gb2utf8 ($ gb)
  4. {
  5. If (! Trim ($ gb ))
  6. Return $ gb;
  7. $ Filename = "gb2312.txt ";
  8. $ Tmp = file ($ filename );
  9. $ Codetable = array ();
  10. While (list ($ key, $ value) = each ($ tmp ))
  11. $ Codetable [hexdec (substr ($ value,)] = substr ($ value );
  12. $ Ret = "";
  13. $ Utf8 = "";
  14. While ($ gb)
  15. {
  16. If (ord (substr ($ gb, 127)>)
  17. {
  18. $ This = substr ($ gb, 0, 2 );
  19. $ Gb = substr ($ gb, 2, strlen ($ gb ));
  20. $ Utf8 = u2utf8 (hexdec ($ codetable [hexdec (bin2hex ($ this)-0x8080]);
  21. For ($ I = 0; $ I
  22. $ Ret. = chr (substr ($ utf8, $ I, 3 ));
  23. }
  24. Else
  25. {
  26. $ Ret. = substr ($ gb, 0, 1 );
  27. $ Gb = substr ($ gb, 1, strlen ($ gb ));
  28. }
  29. }
  30. Return $ ret;
  31. }
  32. Function u2utf8 ($ c)
  33. {
  34. For ($ I = 0; $ I
  35. $ Str = "";
  36. If ($ c <0x80 ){
  37. $ Str. = $ c;
  38. }
  39. Else if ($ c <0x800 ){
  40. $ Str. = (0xC0 | $ c> 6 );
  41. $ Str. = (0x80 | $ c & 0x3F );
  42. }
  43. Else if ($ c <0x10000 ){
  44. $ Str. = (0xE0 | $ c> 12 );
  45. $ Str. = (0x80 | $ c> 6 & 0x3F );
  46. $ Str. = (0x80 | $ c & 0x3F );
  47. }
  48. Else if ($ c <0x200000 ){
  49. $ Str. = (0xF0 | $ c> 18 );
  50. $ Str. = (0x80 | $ c> 12 & 0x3F );
  51. $ Str. = (0x80 | $ c> 6 & 0x3F );
  52. $ Str. = (0x80 | $ c & 0x3F );
  53. }
  54. Return $ str;
  55. }
  56. Header ("Content-type: image/gif ");
  57. $ Im = imagecreate (300,150 );
  58. $ Bkg = ImageColorAllocate ($ im, 0, 0 );
  59. $ Clr = ImageColorAllocate ($ im, 255,255,255 );
  60. $ Fnt = "c: windowsfontssimsun. ttf ";
  61. // Include ("gb2utf8. php ");
  62. $ Str = gb2utf8 ("China agun sticks ");
  63. ImageTTFText ($ im, 30, 0, 50, 50, $ clr, $ fnt, $ str );
  64. ImageGif ($ im );
  65. ImageDestroy ($ im );
  66. ?>
Related Article

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.