php 生成文字png图片的代码

 更新时间:2011年04月17日 21:54:25   作者:  
使用GD生成文字图片是php一项比较常用的功能,笔者今天介绍的是生成文字png图片的函数。需要的朋友可以参考下。
复制代码 代码如下:

<?
/*
php生成文字png图片,可以使用如下方式调用函数:
http://www.yourdomian.com/text_png.php3?msg=helloworld+class&rot=15&size=48&font=fonts/ARIAL.TTF
*/
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 ($this->rot < 0) {
$font_height = abs($bounds[7]-$bounds[1]);
} else if ($this->rot > 0) {
$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 ($this->rot < 0) {
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;
} else if ($this->rot > 0) {
$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($width+($this->pad*2)+1,$height+($this->pad*2)+1);
$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
if ($this->transparent) ImageColorTransparent($image, $background);
ImageInterlace($image, false);
// 画图.
ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
// 输出为png格式.
imagePNG($image);
}
}
$text = new textPNG;
if (isset($msg)) $text->msg = $msg; // 需要显示的文字
if (isset($font)) $text->font = $font; // 字体
if (isset($size)) $text->size = $size; // 文字大小
if (isset($rot)) $text->rot = $rot; // 旋转角度
if (isset($pad)) $text->pad = $pad; // padding
if (isset($red)) $text->red = $red; // 文字颜色
if (isset($grn)) $text->grn = $grn; // ..
if (isset($blu)) $text->blu = $blu; // ..
if (isset($bg_red)) $text->bg_red = $bg_red; // 背景颜色.
if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
if (isset($tr)) $text->transparent = $tr; // 透明度 (boolean).
$text->draw();
?>

相关文章

  • PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍

    PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍

    PHP 为执行外部命令提供大量函数,其中包括 shell_exec()、exec()、passthru() 和 system()。这些命令是相似的,但为您运行的外部程序提供不同的界面。
    2011-09-09
  • PHP实现二维数组去重功能示例

    PHP实现二维数组去重功能示例

    这篇文章主要介绍了PHP实现二维数组去重功能,涉及php针对数组的遍历、判断、设置等相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2017-01-01
  • 使用ltrace工具跟踪PHP库函数调用的方法

    使用ltrace工具跟踪PHP库函数调用的方法

    这篇文章主要介绍了使用ltrace工具跟踪PHP库函数调用的方法,结合实例形式分析了ltrace工具用来跟踪PHP库函数运行时间的相关技巧,需要的朋友可以参考下
    2016-04-04
  • PHP框架Laravel插件Pagination实现自定义分页

    PHP框架Laravel插件Pagination实现自定义分页

    这篇文章主要为大家详细介绍了PHP框架Laravel5.1插件Pagination实现自定义分页的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • php 字符转义 注意事项

    php 字符转义 注意事项

    今天碰到一个处理文件特殊字符的事情,再次注意到这个问题
    2009-05-05
  • php中的三元运算符使用说明

    php中的三元运算符使用说明

    今天一个网友在群里发了个题目不难,但是可能会错,大家可以看看啊。
    2011-07-07
  • PHP回调函数及匿名函数概念与用法详解

    PHP回调函数及匿名函数概念与用法详解

    这篇文章主要介绍了PHP回调函数及匿名函数概念与用法,结合实例形式详细分析了PHP回调函数及匿名函数的概念、功能、使用方法及相关操作注意事项,需要的朋友可以参考下
    2018-03-03
  • php简单构造json多维数组的方法示例

    php简单构造json多维数组的方法示例

    这篇文章主要介绍了php简单构造json多维数组的方法,结合实例形式分析了php数据库查询结果的json格式转换操作技巧,需要的朋友可以参考下
    2017-06-06
  • PHP对象Object的概念 介绍

    PHP对象Object的概念 介绍

    类提供了一个基础,可以在此基础上创建实体(即这个类所建模的实体)的特定实例,这些特定实例称为对象(object)
    2012-06-06
  • PHP unset函数原理及使用方法解析

    PHP unset函数原理及使用方法解析

    这篇文章主要介绍了PHP unset函数原理及使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08

最新评论