复制代码 代码如下: /*
php生成文字png图片,可以使用如下方式调用函数:

*/ Header(“Content-type: image/png”); class textPNG { var $font =
‘fonts/TIMES.TTF’; //默认字体. 相对于脚本存放目录的相对路径. var $msg =
“undefined”; // 默认文字. var $size = 24; var $rot = 0; // 旋转角度. var
$pad = 0; // 填充. var $transparent = 1; // 文字透明度. var $red = 0; //
在黑色背景中… var $grn = 0; var $blu = 0; var $bg_red = 255; //
将文字设置为白色. var $bg_grn = 255; var $bg_blu = 255; function
draw() { $width = 0; $height = 0; $offset_x = 0; $offset_y = 0;
$bounds = array(); $image = “”; // 确定文字高度. $bounds =
ImageTTFBBox($this->size, $this->rot, $this->font, “W”); if {
$font_height = abs($bounds[7]-$bounds[1]); } else if {
$font_height = abs($bounds[1]-$bounds[7]); } else { $font_height =
abs($bounds[7]-$bounds[1]); } // 确定边框高度. $bounds =
ImageTTFBBox($this->size, $this->rot, $this->font,
$this->msg); if { $width = abs($bounds[4]-$bounds[0]); $height =
abs($bounds[3]-$bounds[7]); $offset_y = $font_height; $offset_x =
0; } else if { $width = abs($bounds[2]-$bounds[6]); $height =
abs($bounds[1]-$bounds[5]); $offset_y =
abs($bounds[7]-$bounds[5])+$font_height; $offset_x =
abs($bounds[0]-$bounds[6]); } else { $width =
abs($bounds[4]-$bounds[6]); $height =
abs($bounds[7]-$bounds[1]); $offset_y = $font_height;; $offset_x
= 0; } $image = imagecreate+1,$height+; $background =
ImageColorAllocate($image, $this->bg_red, $this->bg_grn,
$this->bg_blu); $foreground = ImageColorAllocate($image,
$this->red, $this->grn, $this->blu); if ImageColorTransparent;
ImageInterlace; // 画图. ImageTTFText($image, $this->size,
$this->rot, $offset_x+$this->pad, $offset_y+$this->pad,
$foreground, $this->font, $this->msg); // 输出为png格式. imagePNG;
} } $text = new textPNG; if $text->msg = $msg; // 需要显示的文字 if
$text->font = $font; // 字体 if $text->size = $size; // 文字大小
if $text->rot = $rot; // 旋转角度 if $text->pad = $pad; // padding
if $text->red = $red; // 文字颜色 if $text->grn = $grn; // .. if
$text->blu = $blu; // .. if $text->bg_red = $bg_red; //
背景颜色. if $text->bg_grn = $bg_grn; // .. if $text->bg_blu =
$bg_blu; // .. if $text->transparent = $tr; // 透明度 .
$text->draw(); ?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注