论坛风格切换切换到宽版
  • 738阅读
  • 3回复

[求助]用GD生成文字图片,中文乱码了! [复制链接]

上一主题 下一主题
离线xzy713
 

发帖
53
金钱
191
威望
2
只看楼主 倒序阅读 使用道具 楼主  发表于: 2011-12-31
  1. <?
  2. /*
  3. php生成文字png图片,可以使用如下方式调用函数:
  4.   
  5. http://www.yourdomian.com/text_png.php3?msg=helloworld+class&rot=15&size=48&font=fonts/ARIAL.TTF
  6.   
  7. */
  8.   
  9. Header("Content-type: image/png");
  10. class textPNG {
  11.     var $font = 'fonts/times.ttf'; //默认字体. 相对于脚本存放目录的相对路径.
  12.     var $msg = "asdd"; // 默认文字.
  13.     var $size = 10;//文字大小
  14.     var $rot = 0; // 旋转角度.
  15.     var $pad = 0; // 填充.
  16.     var $transparent = 1; // 文字透明度.
  17.     var $red = 0; // 在黑色背景中...
  18.     var $grn = 0;
  19.     var $blu = 0;
  20.     var $bg_red = 255; // 将文字设置为白色.
  21.     var $bg_grn = 255;
  22.     var $bg_blu = 255;
  23.   
  24. function draw() {
  25.     $width = 0;
  26.     $height = 0;
  27.     $offset_x = 0;
  28.     $offset_y = 0;
  29.     $bounds = array();
  30.     $image = "";
  31.   
  32.     // 确定文字高度.
  33.     $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
  34.     if ($this->rot < 0) {
  35.         $font_height = abs($bounds[7]-$bounds[1]);
  36.     } else if ($this->rot > 0) {
  37.         $font_height = abs($bounds[1]-$bounds[7]);
  38.     } else {
  39.         $font_height = abs($bounds[7]-$bounds[1]);
  40.     }
  41.   
  42.     // 确定边框高度.
  43.     $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
  44.     if ($this->rot < 0) {
  45.         $width = abs($bounds[4]-$bounds[0]);
  46.         $height = abs($bounds[3]-$bounds[7]);
  47.         $offset_y = $font_height;
  48.         $offset_x = 0;
  49.   
  50.     } else if ($this->rot > 0) {
  51.         $width = abs($bounds[2]-$bounds[6]);
  52.         $height = abs($bounds[1]-$bounds[5]);
  53.         $offset_y = abs($bounds[7]-$bounds[5])+$font_height;
  54.         $offset_x = abs($bounds[0]-$bounds[6]);
  55.   
  56.     } else {
  57.         $width = abs($bounds[4]-$bounds[6]);
  58.         $height = abs($bounds[7]-$bounds[1]);
  59.         $offset_y = $font_height;;
  60.         $offset_x = 0;
  61.     }
  62.   
  63.     $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
  64.   
  65.     $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
  66.     $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
  67.   
  68.     if ($this->transparent) ImageColorTransparent($image, $background);
  69.     ImageInterlace($image, false);
  70.   
  71.     // 画图.
  72.     $this->msg=iconv('gb2312','utf-8',$this->msg);
  73.     ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
  74.   
  75.     // 输出为png格式.
  76.     imagePNG($image);
  77. }
  78. }
  79.   
  80. $text = new textPNG;
  81.   
  82. if (isset($msg)) $text->msg = $msg; // 需要显示的文字
  83. if (isset($font)) $text->font = $font; // 字体
  84. if (isset($size)) $text->size = $size; // 文字大小
  85. if (isset($rot)) $text->rot = $rot; // 旋转角度
  86. if (isset($pad)) $text->pad = $pad; // padding
  87. if (isset($red)) $text->red = $red; // 文字颜色
  88. if (isset($grn)) $text->grn = $grn; // ..
  89. if (isset($blu)) $text->blu = $blu; // ..
  90. if (isset($bg_red)) $text->bg_red = $bg_red; // 背景颜色.
  91. if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
  92. if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
  93. if (isset($tr)) $text->transparent = $tr; // 透明度 (boolean).
  94.   
  95. $text->draw();
  96. ?>
中文乱码,为什么呢?
fonts/times.ttf字体文件是存在的...难道是这个字体不支持中文?但是我用了中文的字体,一样的是乱码!

发帖
3189
金钱
2859
威望
4323
只看该作者 沙发  发表于: 2011-12-31
离线www.bz82.com

发帖
283
金钱
101
威望
13
只看该作者 板凳  发表于: 01-07
要问一下高手才行了
离线1913727778

发帖
10
金钱
120
威望
21
只看该作者 3楼 发表于: 01-08
哦原来如此啊      只的学习
快速回复
限80 字节
温馨提示:欢迎交流讨论,请勿纯表情、纯引用!
 
上一个 下一个