php 生成文字png图片的代码
2015-01-24信息快讯网
使用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服务器页面间跳转实现方法
php中3des加密代码(完全与.net中的兼容)
浏览器关闭后,能继续执行的php函数(ignore_user_abort)
php读取文件内容至字符串中,同时去除换行、空行、行首行尾空格(Zjmainstay原创)
单一index.php实现PHP任意层级文件夹遍历(Zjmainstay原创)
Erlang的运算符(比较运算符,数值运算符,移位运算符,逻辑运算符)
PHP Warning: PHP Startup: Unable to load dynamic library \ D:/php5/ext/php_mysqli.dll\
php提示无法加载或mcrypt没有找到 PHP 扩展 mbstring解决办法
Notice: Trying to get property of non-object problem(PHP)解决办法
支持中文字母数字、自定义字体php验证码代码
PHP的博客ping服务代码
PHP中实现中文字符进制转换原理分析
PHP mb_convert_encoding文字编码的转换函数介绍
PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
php smarty截取中文字符乱码问题?gb2312/utf-8
PHP支持多种格式图片上传(支持jpg、png、gif)
PHP基础学习小结
适用于php-5.2 的 php.ini 中文版[金步国翻译]
php编写一个简单的路由类
php 求质素(素数) 的实现代码
php 5.3.5安装memcache注意事项小结
php处理json时中文问题的解决方法
The specified CGI application misbehaved by not returning a complete set of HTTP headers
在MongoDB中模拟Auto Increment的php代码
PHP中simplexml_load_string函数使用说明
window+nginx+php环境配置 附配置搭配说明
php iconv() : Detected an illegal character in input string
检测png图片是否完整的php代码
在Windows系统上安装PHP运行环境文字教程
php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
php 中文字符入库或显示乱码问题的解决方法
php截取utf-8中文字符串乱码的解决方法
php下图片文字混合水印与缩略图实现代码