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脚本的10个技巧(1)
Oct 09 PHP
php Memcache 中实现消息队列
Nov 24 PHP
php cout&amp;lt;&amp;lt;的一点看法
Jan 24 PHP
Could not load type System.ServiceModel.Activation.HttpModule解决办法
Dec 29 PHP
解析php中const与define的应用区别
Jun 18 PHP
php利用反射实现插件机制的方法
Mar 14 PHP
Composer设置忽略版本匹配的方法
Apr 27 PHP
php使用CURL模拟GET与POST向微信接口提交及获取数据的方法
Sep 23 PHP
浅谈php中的循环while、do...while、for、foreach四种循环
Nov 05 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
Dec 25 PHP
php输出形式实例整理
May 05 PHP
PHP实现页面静态化深入讲解
Mar 04 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+MYSQL会员系统的开发实例教程
2014/08/23 PHP
php使用正则表达式获取图片url的方法
2015/01/16 PHP
php简单生成随机数的方法
2015/07/30 PHP
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
2013/07/08 Javascript
基于jQuery的JavaScript模版引擎JsRender使用指南
2014/12/29 Javascript
使用javascript实现简单的选项卡切换
2015/01/09 Javascript
jQuery简单自定义图片轮播插件及用法示例
2016/11/21 Javascript
利用Js的console对象,在控制台打印调式信息测试Js的实现
2016/11/26 Javascript
Express与NodeJs创建服务器的两种方法
2017/02/06 NodeJs
Mui使用jquery并且使用点击跳转新窗口的实例
2017/08/19 jQuery
AngularJS路由删除#符号解决的办法
2017/09/28 Javascript
vue里面父组件修改子组件样式的方法
2018/02/03 Javascript
vue-cli构建vue项目的步骤详解
2019/01/27 Javascript
[49:17]DOTA2-DPC中国联赛 正赛 Phoenix vs Dynasty BO3 第三场 1月26日
2021/03/11 DOTA
python批量同步web服务器代码核心程序
2014/09/01 Python
Python读取一个目录下所有目录和文件的方法
2016/07/15 Python
Python编程修改MP3文件名称的方法
2017/04/19 Python
Python中模块与包有相同名字的处理方法
2017/05/05 Python
python中kmeans聚类实现代码
2018/02/23 Python
python3实现二叉树的遍历与递归算法解析(小结)
2019/07/03 Python
python搜索包的路径的实现方法
2019/07/19 Python
python随机生成库faker库api实例详解
2019/11/28 Python
Python DataFrame使用drop_duplicates()函数去重(保留重复值,取重复值)
2020/07/20 Python
Python并发爬虫常用实现方法解析
2020/11/19 Python
一文读懂python Scrapy爬虫框架
2021/02/24 Python
CSS3 优势以及网页设计师如何使用CSS3技术
2009/07/29 HTML / CSS
海蓝之谜英国官网:La Mer英国
2020/01/15 全球购物
Cecil Mode法国在线商店:女性时尚
2021/01/08 全球购物
大学校园毕业自我鉴定
2014/01/15 职场文书
医院检讨书范文
2014/02/01 职场文书
军训自我鉴定范文
2014/02/13 职场文书
装饰施工员岗位职责
2015/04/11 职场文书
2016国庆促销广告语
2016/01/28 职场文书
创业计划书之个人工作室
2019/08/22 职场文书
python中的被动信息搜集
2021/04/29 Python
centos8安装nginx1.9.1的详细过程
2021/08/02 Servers