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 相关文章推荐
第1次亲密接触PHP5(1)
Oct 09 PHP
其他功能
Oct 09 PHP
如何使用PHP往windows中添加用户
Dec 06 PHP
zend framework多模块多布局配置
Feb 26 PHP
PHP高级对象构建 多个构造函数的使用
Feb 05 PHP
Codeigniter操作数据库表的优化写法总结
Jun 12 PHP
ThinkPHP的L方法使用简介
Jun 18 PHP
Yii中CGridView关联表搜索排序方法实例详解
Dec 03 PHP
Laravel5中contracts详解
Mar 02 PHP
微信自定义分享php代码分析
Nov 24 PHP
Yii2 队列 shmilyzxt/yii2-queue 简单概述
Aug 02 PHP
php ZipArchive实现多文件打包下载实例
Oct 31 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
收音机怀古---春雷3P7图片欣赏
2021/03/02 无线电
php面向对象全攻略 (五) 封装性
2009/09/30 PHP
解析php做推送服务端实现ios消息推送
2013/07/01 PHP
jQuery的deferred对象使用详解
2011/08/20 Javascript
jquery解决图片路径不存在执行替换路径
2013/02/06 Javascript
jQuery实现密保互斥问题解决方案
2013/08/16 Javascript
javascript实现数字+字母验证码的简单实例
2014/02/10 Javascript
JavaScript前补零操作实例
2015/03/11 Javascript
jQuery实现防止提交按钮被双击的方法
2015/03/24 Javascript
JQuery中两个ul标签的li互相移动实现方法
2015/05/18 Javascript
基于jQuery仿淘宝产品图片放大镜代码分享
2020/06/23 Javascript
jQuery常用的一些技巧汇总
2016/03/26 Javascript
JavaScript+HTML5实现的日期比较功能示例
2017/07/12 Javascript
基于vue.js中事件修饰符.self的用法(详解)
2018/02/23 Javascript
Django+vue跨域问题解决的详细步骤
2019/01/20 Javascript
Vue.js实现大屏数字滚动翻转效果
2019/11/29 Javascript
详解JavaScript修改注册表的方法
2020/01/05 Javascript
Vue ElementUI实现:限制输入框只能输入正整数的问题
2020/07/31 Javascript
Python 列表list使用介绍
2014/11/30 Python
Python3实现的画图及加载图片动画效果示例
2018/01/19 Python
Python自然语言处理 NLTK 库用法入门教程【经典】
2018/06/26 Python
python爬取哈尔滨天气信息
2018/07/14 Python
Ubuntu下Anaconda和Pycharm配置方法详解
2019/06/14 Python
浅谈Python中range与Numpy中arange的比较
2020/03/11 Python
python线程池如何使用
2020/05/28 Python
python opencv pytesseract 验证码识别的实现
2020/08/28 Python
python speech模块的使用方法
2020/09/09 Python
matplotlib教程——强大的python作图工具库
2020/10/15 Python
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
锐步美国官方网站:Reebok美国
2018/01/10 全球购物
小孩百日宴答谢词
2014/01/15 职场文书
物流管理专业求职信
2014/05/29 职场文书
选秀节目策划方案
2014/06/06 职场文书
安全口号大全
2014/06/21 职场文书
教你用Python爬取英雄联盟皮肤原画
2021/06/13 Python
详解nginx location指令
2022/01/18 Servers