+ -
当前位置:首页 → 问答吧 → php 生成文字png图片

php 生成文字png图片

时间:2011-04-18

来源:互联网

转:About  

php 生成文字png图片




使用GD生成文字图片是php一项比较常用的功能,笔者今天介绍的是生成文字png图片的函数。喜欢的朋友收藏吧^_^


view sourceprint?
  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.    

  11. class textPNG {  

  12.     var $font = 'fonts/TIMES.TTF'; //默认字体. 相对于脚本存放目录的相对路径.  

  13.     var $msg = "undefined"; // 默认文字.  

  14.     var $size = 24;  

  15.     var $rot = 0; // 旋转角度.  

  16.     var $pad = 0; // 填充.  

  17.     var $transparent = 1; // 文字透明度.  

  18.     var $red = 0; // 在黑色背景中...  

  19.     var $grn = 0;  

  20.     var $blu = 0;  

  21.     var $bg_red = 255; // 将文字设置为白色.  

  22.     var $bg_grn = 255;  

  23.     var $bg_blu = 255;  

  24.    

  25. function draw() {  

  26.     $width = 0;  

  27.     $height = 0;  

  28.     $offset_x = 0;  

  29.     $offset_y = 0;  

  30.     $bounds = array();  

  31.     $image = "";  

  32.    

  33.     // 确定文字高度.  

  34.     $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");  

  35.     if ($this->rot < 0) {  

  36.         $font_height = abs($bounds[7]-$bounds[1]);  

  37.     } else if ($this->rot > 0) {  

  38.         $font_height = abs($bounds[1]-$bounds[7]);  

  39.     } else {  

  40.         $font_height = abs($bounds[7]-$bounds[1]);  

  41.     }  

  42.    

  43.     // 确定边框高度.  

  44.     $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);  

  45.     if ($this->rot < 0) {  

  46.         $width = abs($bounds[4]-$bounds[0]);  

  47.         $height = abs($bounds[3]-$bounds[7]);  

  48.         $offset_y = $font_height;  

  49.         $offset_x = 0;  

  50.    

  51.     } else if ($this->rot > 0) {  

  52.         $width = abs($bounds[2]-$bounds[6]);  

  53.         $height = abs($bounds[1]-$bounds[5]);  

  54.         $offset_y = abs($bounds[7]-$bounds[5])+$font_height;  

  55.         $offset_x = abs($bounds[0]-$bounds[6]);  

  56.    

  57.     } else {  

  58.         $width = abs($bounds[4]-$bounds[6]);  

  59.         $height = abs($bounds[7]-$bounds[1]);  

  60.         $offset_y = $font_height;;  

  61.         $offset_x = 0;  

  62.     }  

  63.    

  64.     $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);  

  65.    

  66.     $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);  

  67.     $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);  

  68.    

  69.     if ($this->transparent) ImageColorTransparent($image, $background);  

  70.     ImageInterlace($image, false);  

  71.    

  72.     // 画图.  

  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. ?>
复制代码

作者: so_brave   发布时间: 2011-04-18

了解了,顶。。。

作者: surpass_li   发布时间: 2011-04-19

热门下载

更多