当前位置:首页 > PHP教程 > php函数 > 列表

在GD中输出汉字的函数

发布:smiling 来源: PHP粉丝网  添加日期:2013-12-09 14:12:29 浏览: 评论:0 

感谢sadly为我们写出了在GD中输出汉字的函数,我在使用中发现此版本输出的字符串必须为纯中文,不能夹杂英文,随修改了此bug,与大家分享。

  1. <? 
  2. //Program writen by sadly www.phpfensi.com 
  3. //modified by agun 2013/6/20 
  4. function gb2utf8($gb
  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,0,6))]=substr($value,7,6); 
  12. $ret=""
  13. $utf8=""
  14. while($gb
  15. if (ord(substr($gb,0,1))>127) 
  16. $this=substr($gb,0,2); 
  17. $gb=substr($gb,2,strlen($gb)); 
  18. $utf8=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080])); 
  19. for($i=0;$i<strlen($utf8);$i+=3) 
  20. $ret.=chr(substr($utf8,$i,3)); 
  21. else 
  22. $ret.=substr($gb,0,1); 
  23. $gb=substr($gb,1,strlen($gb)); 
  24. return $ret
  25. function u2utf8($c
  26. for($i=0;$i<count($c);$i++) 
  27. $str=""
  28. if ($c < 0x80) { 
  29. $str.=$c
  30. else if ($c < 0x800) { 
  31. $str.=(0xC0 | $c>>6); 
  32. $str.=(0x80 | $c & 0x3F); 
  33. else if ($c < 0x10000) { 
  34. $str.=(0xE0 | $c>>12); 
  35. $str.=(0x80 | $c>>6 & 0x3F); 
  36. $str.=(0x80 | $c & 0x3F); 
  37. else if ($c < 0x200000) { 
  38. $str.=(0xF0 | $c>>18); 
  39. $str.=(0x80 | $c>>12 & 0x3F); 
  40. $str.=(0x80 | $c>>6 & 0x3F); 
  41. $str.=(0x80 | $c & 0x3F); 
  42. return $str
  43. Header("Content-type: image/gif"); 
  44. $im = imagecreate(300,150); 
  45. $bkg = ImageColorAllocate($im, 0,0,0); 
  46. $clr = ImageColorAllocate($im, 255,255,255); 
  47. $fnt = "c:windowsfontssimsun.ttf"
  48. //include("gb2utf8.php"); 
  49. $str = gb2utf8("中国agun阿棍"); 
  50. ImageTTFText($im, 30, 0, 50,50, $clr$fnt$str); 
  51. ImageGif($im); 
  52. ImageDestroy($im); 
  53. ?> 

Tags: GD 汉字 函数

分享到: