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递归实现无限分类生成下拉列表的函数
Aug 08 PHP
php数据结构与算法(PHP描述) 查找与二分法查找
Jun 21 PHP
基于ubuntu下nginx+php+mysql安装配置的具体操作步骤
Apr 28 PHP
PHP生成数组再传给js的方法
Aug 07 PHP
linux下实现定时执行php脚本
Feb 13 PHP
PHP框架Laravel插件Pagination实现自定义分页
Apr 22 PHP
php版微信自动登录并获取昵称的方法
Sep 23 PHP
CI框架常用函数封装实例
Nov 21 PHP
PHP编程实现计算抽奖概率算法完整实例
Aug 09 PHP
PHP实现的字符串匹配算法示例【sunday算法】
Dec 19 PHP
python进程与线程小结实例分析
Nov 11 PHP
php+mysql开发中的经验与常识小结
Mar 25 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
ThinkPHP验证码使用简明教程
2014/03/05 PHP
Laravel 4.2 中队列服务(queue)使用感受
2014/10/30 PHP
PHP观察者模式定义与用法实例分析
2019/03/22 PHP
使用PHP开发留言板功能
2019/11/19 PHP
javascript Array对象基础知识小结
2010/11/16 Javascript
如何确保JavaScript的执行顺序 之jQuery.html并非万能钥匙
2011/03/03 Javascript
jQuery实现回车键(Enter)切换文本框焦点的代码实例
2014/05/05 Javascript
jquery ui bootstrap 实现自定义风格
2014/11/14 Javascript
XML文件转化成NSData对象的方法
2015/08/12 Javascript
JavaScript中的对象继承关系
2016/08/01 Javascript
js控制div层的叠加简单方法
2016/10/15 Javascript
使用vue构建一个上传图片表单
2017/07/04 Javascript
JS+WCF实现进度条实时监测数据加载量的方法详解
2017/12/19 Javascript
jQuery 获取除某指定对象外的其他对象 ( :not() 与.not())
2018/10/10 jQuery
微信小程序ibeacon三点定位详解
2018/10/31 Javascript
vue实现跨域的方法分析
2019/05/21 Javascript
解决vue使用vant下拉框van-dropdown-item 绑定title值不变问题
2020/08/05 Javascript
基于Ionic3实现选项卡切换并重新加载echarts
2020/09/24 Javascript
Python爬虫框架Scrapy实战之批量抓取招聘信息
2015/08/07 Python
浅谈python socket函数中,send与sendall的区别与使用方法
2017/05/09 Python
Python程序运行原理图文解析
2018/02/10 Python
python文件拆分与重组实例
2018/12/10 Python
使用jupyter Nodebook查看函数或方法的参数以及使用情况
2020/04/14 Python
pytorch 把图片数据转化成tensor的操作
2021/03/04 Python
英国在线女鞋目的地:SIMMI
2018/12/27 全球购物
校园十佳歌手策划书
2014/01/22 职场文书
秋季运动会稿件
2014/01/30 职场文书
大学生素质拓展活动方案
2014/02/11 职场文书
俄语专业职业生涯规划
2014/02/26 职场文书
面试必备的求职信
2014/05/25 职场文书
我的中国梦演讲稿1000字
2014/08/19 职场文书
乡党委干部党的群众路线教育实践活动个人对照检查材料思想汇报
2014/10/01 职场文书
民间借贷借条如何写
2015/05/26 职场文书
WINDOWS 64位 下安装配置mysql8.0.25最详细的教程
2022/03/22 MySQL
win10更新失败无限重启解决方法
2022/04/19 数码科技
java版 简单三子棋游戏
2022/05/04 Java/Android