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类
Jul 15 PHP
生成php程序的php代码
Apr 07 PHP
PHP 抓取网页图片并且另存为的实现代码
Mar 24 PHP
php通过文件头检测文件类型通用代码类(zip,rar等)
Oct 19 PHP
php守护进程 加linux命令nohup实现任务每秒执行一次
Jul 04 PHP
php使用Smarty的相关注意事项及访问变量的几种方式
Dec 08 PHP
PHP读取大文件的类SplFileObject使用介绍
Apr 09 PHP
兼容各大浏览器带关闭按钮的漂浮多组图片广告代码
Jun 05 PHP
php代码架构的八点注意事项
Jan 25 PHP
PHP实现通过URL提取根域名
Mar 31 PHP
postfixadmin忘记密码后的修改密码方法详解
Jul 20 PHP
php调用云片网接口发送短信的实现方法
Oct 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
Php做的端口嗅探器--可以指定网站和端口
2006/10/09 PHP
PHP常用技巧总结(附函数代码)
2012/02/04 PHP
基于php socket(fsockopen)的应用实例分析
2013/06/02 PHP
PHP中time(),date(),mktime()区别介绍
2013/09/28 PHP
PHP调试的强悍利器之PHPDBG
2016/02/22 PHP
PHP模型Model类封装数据库操作示例
2019/03/14 PHP
php实现的数组转xml案例分析
2019/09/28 PHP
关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器
2014/02/11 Javascript
jquery通过load获取文件的内容并跳到锚点的方法
2015/01/29 Javascript
jquery制作多功能轮播图插件
2015/04/02 Javascript
javascript实现验证身份证号的有效性并提示
2015/04/30 Javascript
JavaScript中的对象与JSON
2015/07/03 Javascript
AngularJs directive详解及示例代码
2016/09/01 Javascript
JS高阶函数原理与用法实例分析
2019/01/15 Javascript
node中IO以及定时器优先级详解
2019/05/10 Javascript
[01:18:35]DOTA2-DPC中国联赛 正赛 Elephant vs LBZS BO3 第一场 1月29日
2021/03/11 DOTA
python练习程序批量修改文件名
2014/01/16 Python
Python返回真假值(True or False)小技巧
2015/04/10 Python
使用Python实现一个栈判断括号是否平衡
2018/08/23 Python
Python配置虚拟环境图文步骤
2019/05/20 Python
利用python、tensorflow、opencv、pyqt5实现人脸实时签到系统
2019/09/25 Python
基于python实现计算且附带进度条代码实例
2020/03/31 Python
Pytorch数据拼接与拆分操作实现图解
2020/04/30 Python
解决Keras中CNN输入维度报错问题
2020/06/29 Python
Shopty西班牙:缝纫机在线销售
2018/01/26 全球购物
莫斯科制造商的廉价皮大衣:Fursk
2020/06/09 全球购物
以下的初始化有什么区别
2013/12/16 面试题
财务人员个人自荐信范文
2013/09/26 职场文书
餐饮加盟计划书
2014/01/10 职场文书
药学专业学生的自我评价分享
2014/02/06 职场文书
利群广告词
2014/03/20 职场文书
某某同志考察材料
2014/05/28 职场文书
机械设计及其自动化专业求职信
2014/06/09 职场文书
2016幼儿园新学期寄语
2015/12/03 职场文书
Nginx的gzip相关介绍
2022/05/11 Servers
js面向对象编程OOP及函数式编程FP区别
2022/07/07 Javascript