当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP实现在图片中添加中文文字

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-20 10:24:51 浏览: 评论:0 
  1. <?php 
  2. /* 
  3. 注重:需要gd库支持,需要iconv支持(php5已经包含不用加载) 
  4. 在图片中添加中文文字 
  5. */ 
  6. /* 
  7. param $image 图象资源 
  8. param size 字体大小 
  9. param angle 字体输出角度 
  10. param showX 输出位置x坐标 
  11. param showY 输出位置y坐标 
  12. param font 字体文件位置 
  13. param content 要在图片里显示的内容 
  14. */ 
  15. class showChinaText 
  16. { 
  17. var $text='你好'; 
  18. var $font='fs.ttf'; 
  19. var $angle=0; 
  20. var $size=50; 
  21. var $showX=100; 
  22. var $showY=100; 
  23. function showChinaText($showText='') 
  24. { 
  25. $this->text=!isset($showText)?$showText:$this->text; 
  26. //exit(); 
  27. $this->show(); 
  28. } 
  29. function createText($instring) 
  30. { 
  31. $outstring=""; 
  32. $max=strlen($instring); 
  33. for($i=0;$i<$max;$i ) 
  34. { 
  35. $h=ord($instring[$i]); 
  36. if($h>=160 && $i<$max-1) 
  37. { 
  38. $outstring.="&#".base_convert(bin2hex(iconv("gb2312","ucs-2",substr ($instring,$i,2))),16,10).";"; 
  39. $i ; 
  40. } 
  41. else 
  42. { 
  43. $outstring.=$instring[$i]; 
  44. } 
  45. } 
  46. return $outstring; 
  47. } 
  48. function createJpeg() 
  49. {} 
  50. function show() 
  51. { 
  52. //输出头内容 
  53. Header( "Content-type: image/png"); 
  54. //建立图象 
  55. $image = imagecreate(400,300); 
  56. //定义颜色 
  57. $red = ImageColorAllocate($image,255,0,0); 
  58. $white = ImageColorAllocate($image,255,255,255); 
  59. $black=ImageColorAllocate($image,0,0,0); 
  60. //填充颜色 
  61. ImageFilledRectangle($image,0,0,200,200,$red); 
  62. //显示文字 
  63. $txt=$this->createText($this->text); 
  64. //写入文字 
  65. imagettftext($image,$this->size, $this->angle, $this->showX, $this->showY,$white,$this->font,$txt); 
  66. //ImageString($image,5,50,10,$txt,$white); 
  67. //显示图形 
  68. imagejpeg($image); 
  69. ImageDestroy($image); 
  70. }//开源代码phpfensi.com 
  71. } 
  72. //本类,并没有经过很好的考虑,只是简单的进行了封装,以后有机会,可能跟原来的图片类整合 
  73. ?> 
  74. <?php 
  75. //使用示例 
  76. $s = new showChinaText(); 
  77. ?> 

Tags: PHP添加文字 PHP图片中文文字

分享到: