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在线打包程序源码
Jul 27 PHP
php 数组排序 array_multisort与uasort的区别
Mar 24 PHP
探讨PHP调用时间格式的参数详解
Jun 06 PHP
解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍
Jun 24 PHP
php使用curl发送json格式数据实例
Dec 17 PHP
php过滤HTML标签、属性等正则表达式汇总
Sep 22 PHP
php转换颜色为其反色的方法
Apr 27 PHP
详解HTTP Cookie状态管理机制
Jan 14 PHP
详解PHP中array_rand函数的使用方法
Sep 11 PHP
php无限极分类实现方法分析
Jul 04 PHP
php使用socket调用http和smtp协议实例小结
Jul 26 PHP
通过PHP实现获取访问用户IP
May 09 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
Snoopy类使用小例子
2008/04/15 PHP
关于IIS php调用com组件的权限问题
2012/01/11 PHP
PHP中调用C/C++制作的动态链接库的教程
2016/03/10 PHP
php 指定范围内多个随机数代码实例
2016/07/18 PHP
如何运行/调试你的PHP代码
2020/10/23 PHP
Firefox window.close()的使用注意事项
2009/04/11 Javascript
关于IE中getElementsByClassName不能用的问题解决方法
2013/08/26 Javascript
jquery()函数的三种语法介绍
2013/10/09 Javascript
JavaScript对象的property属性详解
2014/04/01 Javascript
JQuery判断checkbox是否选中及其它复选框操作方法合集
2015/06/01 Javascript
ECMAScript6中Set/WeakSet详解
2015/06/12 Javascript
JQuery 在文档中查找指定name的元素并移除的实现方法
2016/05/19 Javascript
JS实现的系统调色板完整实例
2016/12/21 Javascript
jquery+ajax实现省市区三级联动效果简单示例
2017/01/04 Javascript
nginx配置React静态页面的方法教程
2017/11/03 Javascript
完美解决手机网页中输入框被输入法遮挡的问题
2017/12/19 Javascript
MVVM 双向绑定的实现代码
2018/06/21 Javascript
node crawler如何添加promise支持
2020/02/01 Javascript
vue请求数据的三种方式
2020/03/04 Javascript
Postman内建变量常用方法实例解析
2020/07/28 Javascript
python3实现猜数字游戏
2020/12/07 Python
Django外键(ForeignKey)操作以及related_name的作用详解
2019/07/29 Python
解决Pytorch 训练与测试时爆显存(out of memory)的问题
2019/08/20 Python
python创建ArcGIS shape文件的实现
2019/12/06 Python
python支持多继承吗
2020/06/19 Python
食堂个人先进事迹
2014/01/22 职场文书
高二物理教学反思
2014/02/08 职场文书
《月光启蒙》教学反思
2014/03/01 职场文书
《莫泊桑拜师》教学反思
2014/04/23 职场文书
十一国庆节“向国旗敬礼”主题班会活动方案
2014/09/27 职场文书
国庆庆典邀请函
2015/02/02 职场文书
联村联户简报
2015/07/21 职场文书
python 统计代码耗时的几种方法分享
2021/04/02 Python
Python使用mitmproxy工具监控手机 下载手机小视频
2022/04/18 Python
Redis 异步机制
2022/05/15 Redis
Python使用pandas导入csv文件内容的示例代码
2022/12/24 Python