php 生成文字png图片的代码


Posted in PHP onApril 17, 2011
<? 
/* 
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获取某个目录大小的代码
Sep 10 PHP
Erlang的运算符(比较运算符,数值运算符,移位运算符,逻辑运算符)
Jul 23 PHP
基于PHP异步执行的常用方式详解
Jun 03 PHP
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
Jan 22 PHP
PHP使用json_encode函数时不转义中文的解决方法
Nov 12 PHP
PHP图片处理之使用imagecopyresampled函数裁剪图片例子
Nov 19 PHP
使用PHP生成PDF方法详解
Jan 23 PHP
Android AsyncTack 异步任务实例详解
Nov 02 PHP
php实现的网页版剪刀石头布游戏示例
Nov 25 PHP
PHP验证码类ValidateCode解析
Jan 07 PHP
详解Laravel服务容器的绑定与解析
Nov 05 PHP
PHP的垃圾回收机制代码实例讲解
Feb 27 PHP
适用于php-5.2 的 php.ini 中文版[金步国翻译]
Apr 17 #PHP
php编写一个简单的路由类
Apr 13 #PHP
php 求质素(素数) 的实现代码
Apr 12 #PHP
php 5.3.5安装memcache注意事项小结
Apr 12 #PHP
php处理json时中文问题的解决方法
Apr 12 #PHP
php 面向对象的一个例子
Apr 12 #PHP
深入理解PHP原理之Session Gc的一个小概率Notice
Apr 12 #PHP
You might like
php中的观察者模式
2010/03/24 PHP
php简单获取目录列表的方法
2015/03/24 PHP
PHP简单实现数字分页功能示例
2016/08/24 PHP
PHP获取ttf格式文件字体名的方法示例
2019/03/06 PHP
PHP数组array类常见操作示例
2020/05/15 PHP
javascript dom 操作详解 js加强
2009/07/13 Javascript
JQuery jsonp 使用示例代码
2009/08/12 Javascript
js 屏蔽鼠标右键脚本附破解方法
2009/12/03 Javascript
JavaScript 题型问答有答案参考
2010/02/17 Javascript
深入Javascript函数、递归与闭包(执行环境、变量对象与作用域链)使用详解
2013/05/08 Javascript
javascript列表框操作函数集合汇总
2013/11/28 Javascript
JS获取随机数函数可自定义最小值最大值
2014/05/08 Javascript
Javascript的&amp;&amp;和||的另类用法
2014/07/23 Javascript
微信小程序 动画的简单实例
2017/10/12 Javascript
微信小程序使用setData修改数组中单个对象的方法分析
2018/12/30 Javascript
VUE使用 wx-open-launch-app 组件开发微信打开APP功能
2020/08/11 Javascript
[11:01]2014DOTA2西雅图邀请赛 冷冷带你探秘威斯汀
2014/07/08 DOTA
使用Python操作MySQL的一些基本方法
2015/08/16 Python
Python下rrdtool模块的基本使用方法
2015/11/13 Python
pandas获取groupby分组里最大值所在的行方法
2018/04/20 Python
Python实现的合并两个有序数组算法示例
2019/03/04 Python
python面向对象实现名片管理系统文件版
2019/04/26 Python
python 伯努利分布详解
2020/02/25 Python
python中matplotlib实现随鼠标滑动自动标注代码
2020/04/23 Python
Keras 切换后端方式(Theano和TensorFlow)
2020/06/19 Python
HTML5 拖放(Drag 和 Drop)详解与实例代码
2017/09/14 HTML / CSS
英国手工布艺沙发在线购买:Sofas & Stuff
2018/03/02 全球购物
运动鞋、足球鞋和慕尼黑球衣:Sport Münzinger
2019/08/26 全球购物
金鑫耀Java笔试题
2014/09/06 面试题
大学生职业生涯规划书模板
2014/01/18 职场文书
幼儿园秋游活动方案
2014/01/21 职场文书
安全生产汇报材料
2014/02/17 职场文书
预备党员的自我评价
2014/03/12 职场文书
Python使用openpyxl批量处理数据
2021/06/23 Python
电脑开机弹出documents文件夹怎么回事?弹出documents文件夹解决方法
2022/04/08 数码科技
Python使用socket去实现TCP客户端和TCP服务端
2022/04/12 Python