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 相关文章推荐
让你同时上传 1000 个文件 (二)
Oct 09 PHP
MySQL相关说明
Jan 15 PHP
MySql 按时间段查询数据方法(实例说明)
Nov 02 PHP
PHP 类型转换函数intval
Jun 20 PHP
浅析php数据类型转换
Jan 09 PHP
PHP之sprintf函数用法详解
Nov 12 PHP
php阿拉伯数字转中文人民币大写
Dec 21 PHP
yii2.0使用Plupload实现带缩放功能的多图上传
Dec 22 PHP
php利用ffmpeg提取视频中音频与视频画面的方法详解
Jun 07 PHP
浅谈Laravel中的三种中间件的作用
Oct 13 PHP
Laravel监听数据库访问,打印SQL的例子
Oct 24 PHP
ThinkPhP+Apache+PHPstorm整合框架流程图解
Nov 23 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
图书管理程序(一)
2006/10/09 PHP
php基本函数汇总
2015/07/09 PHP
php实现的简单数据库操作Model类
2016/11/16 PHP
PHP正则表达式匹配替换与分割功能实例浅析
2017/02/04 PHP
PHP模型Model类封装数据库操作示例
2019/03/14 PHP
用CSS+JS实现的进度条效果效果
2007/06/05 Javascript
Prototype ObjectRange对象学习
2009/07/19 Javascript
JSON 客户端和服务器端的格式转换
2009/08/27 Javascript
js+jquery常用知识点汇总
2015/03/03 Javascript
解决node-webkit 不支持html5播放mp4视频的方法
2015/03/11 Javascript
jQuery源码分析之Callbacks详解
2015/03/13 Javascript
基于HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
2016/01/05 Javascript
vuejs2.0子组件改变父组件的数据实例
2017/05/10 Javascript
浅谈ng-zorro使用心得
2018/12/03 Javascript
Vue入门学习笔记【基本概念、对象、过滤器、指令等】
2019/04/13 Javascript
vue父组件触发事件改变子组件的值的方法实例详解
2019/05/07 Javascript
jquery.pager.js分页实现详解
2019/07/29 jQuery
浅谈监听单选框radio改变事件(和layui中单选按钮改变事件)
2019/09/10 Javascript
微信内置开发 iOS修改键盘换行为搜索的解决方案
2019/11/06 Javascript
vue下axios拦截器token刷新机制的实例代码
2020/01/17 Javascript
JavaScript中遍历的十种方法总结
2020/12/15 Javascript
[01:51]开启你的城市传奇 完美世界城市挑战赛开始报名
2018/10/09 DOTA
python BeautifulSoup使用方法详解
2013/11/21 Python
用tensorflow实现弹性网络回归算法
2018/01/09 Python
python3判断IP地址的方法
2021/03/04 Python
HTML5 Web Workers之网站也能多线程的实现
2013/04/24 HTML / CSS
在Ajax应用中信息是如何在浏览器和服务器之间传递的
2016/05/31 面试题
精彩自我鉴定
2014/01/16 职场文书
关于毕业的中学校园广播稿
2014/01/26 职场文书
刊首寄语大全
2014/04/11 职场文书
法院干警四风问题个人对照检查材料思想汇报
2014/10/07 职场文书
环保建议书作文500字
2015/09/14 职场文书
小学秋季运动会加油口号及加油稿
2019/08/19 职场文书
html实现随机点名器的示例代码
2021/04/02 Javascript
mysql分组后合并显示一个字段的多条数据方式
2022/01/22 MySQL
Python requests用法和django后台处理详解
2022/03/19 Python