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简单封装了一些常用JS操作
Feb 25 PHP
基于PHP开发中的安全防范知识详解
Jun 06 PHP
单台服务器的PHP进程之间实现共享内存的方法
Jun 13 PHP
php在线解压ZIP文件的方法
Dec 30 PHP
作为程序员必知的16个最佳PHP库
Dec 09 PHP
最新版本PHP 7 vs HHVM 多角度比较
Feb 14 PHP
php metaphone()函数的定义和用法
May 15 PHP
php5.3后静态绑定用法详解
Nov 11 PHP
PHP简单实现模拟登陆功能示例
Sep 15 PHP
PHP实现简单计算器小程序
Aug 28 PHP
php成功操作redis cluster集群的实例教程
Jan 13 PHP
PHP判断是否微信访问的方法示例
Mar 27 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下实现农历日历的代码
2007/03/07 PHP
php的hash算法介绍
2014/02/13 PHP
PHP+Ajax验证码验证用户登录
2016/07/20 PHP
PHP实现对数组分页处理实例详解
2017/02/07 PHP
PHP+swoole+linux实现系统监控和性能优化操作示例
2019/04/15 PHP
return false;和e.preventDefault();的区别
2010/07/11 Javascript
js给dropdownlist添加选项的小例子
2013/03/04 Javascript
js实现同一页面多个运动效果的方法
2015/04/10 Javascript
[原创]javascript typeof id==='string'?document.getElementById(id):id解释
2016/11/02 Javascript
JS中substring与substr的用法
2016/11/16 Javascript
AngularJS执行流程详解
2017/02/17 Javascript
基于JavaScript实现的顺序查找算法示例
2017/04/14 Javascript
JavaScript html5 canvas实现图片上画超链接
2017/10/20 Javascript
js中获取URL参数的共用方法getRequest()方法实例详解
2018/10/24 Javascript
怎样使你的 JavaScript 代码简单易读(推荐)
2019/04/16 Javascript
小程序Request的另类用法详解
2019/08/09 Javascript
Ant Design的可编辑Tree的实现操作
2020/10/31 Javascript
Python读取ini文件、操作mysql、发送邮件实例
2015/01/01 Python
python中迭代器(iterator)用法实例分析
2015/04/29 Python
Python单链表简单实现代码
2016/04/27 Python
Python 错误和异常代码详解
2018/01/29 Python
Python基于socket模块实现UDP通信功能示例
2018/04/10 Python
把JSON数据格式转换为Python的类对象方法详解(两种方法)
2019/06/04 Python
python3.7 使用pymssql往sqlserver插入数据的方法
2019/07/08 Python
python实现京东订单推送到测试环境,提供便利操作示例
2019/08/09 Python
Python 使用 Pillow 模块给图片添加文字水印的方法
2019/08/30 Python
Python 2种方法求某个范围内的所有素数(质数)
2020/01/31 Python
Django 设置admin后台表和App(应用)为中文名的操作方法
2020/05/10 Python
Pyecharts地图显示不完成问题解决方案
2020/05/11 Python
Liu Jo西班牙官网:意大利服装品牌
2019/09/11 全球购物
编辑个人求职信范文
2013/09/21 职场文书
学生意外伤害赔偿协议书
2014/09/17 职场文书
英语辞职信范文
2015/02/28 职场文书
这样写python注释让代码更加的优雅
2021/06/02 Python
Python+Appium自动化测试的实战
2021/06/30 Python
mysqldump进行数据备份详解
2022/07/15 MySQL