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中spl_autoload_register函数的用法总结

    PHP中spl_autoload_register函数的用法总结

    本文是对PHP中spl_autoload_register函数的用法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2013-11-11
  • php生成随机密码的几种方法

    php生成随机密码的几种方法

    使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码。
    2011-01-01
  • 2010年最新PHP类的精髓归纳

    2010年最新PHP类的精髓归纳

    2010年最新PHP类的精髓归纳,需要的朋友可以参考下。
    2010-03-03
  • PHP的伪随机数与真随机数详解

    PHP的伪随机数与真随机数详解

    这篇文章主要介绍了PHP的伪随机数与真随机数详解,本文首先讲解了真随机数和伪随机数的相关概念,并给出了比用mt_rand()函数产生更好的伪随机数的一段例子代码,需要的朋友可以参考下
    2015-05-05
  • PHP超全局变量实现原理及代码解析

    PHP超全局变量实现原理及代码解析

    这篇文章主要介绍了PHP超全局变量实现原理及代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • PHP与Web页面的交互示例详解一

    PHP与Web页面的交互示例详解一

    这篇文章主要介绍了PHP与Web页面的交互示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • php一行代码获取文件后缀名实例分析

    php一行代码获取文件后缀名实例分析

    这篇文章主要介绍了php一行代码获取文件后缀名的方法,以实例形式较为详细的分析了其中涉及的函数和原理,并补充了三种常用的获取文件后缀名的方法,需要的朋友可以参考下
    2014-11-11
  • phpexcel导出excel的颜色和网页中的颜色显示不一致

    phpexcel导出excel的颜色和网页中的颜色显示不一致

    关于phpexcel导出颜色的一些问题,用phpexcel做导出的excel的颜色怎么和网页中的颜色显示不一致呢,接下来将详细介绍解决方法
    2012-12-12
  • mongodb和php的用法详解

    mongodb和php的用法详解

    在本篇文章中小编给大家分享了关于mongodb和php的用法以及相关知识点,需要的朋友们学习下。
    2019-03-03
  • 在php中实现限流ip次数以及允许部分ip访问的代码示例

    在php中实现限流ip次数以及允许部分ip访问的代码示例

    这篇文章给大家介绍了如何在php中实现限流ip次数以及允许部分ip访问,文中通过代码示例给大家介绍的非常详细,对大家的学习具有一定的参考价值,需要的朋友可以参考下
    2023-12-12

最新评论