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字符串截取问题
Nov 28 PHP
解决GD中文乱码问题
Feb 14 PHP
php ftp文件上传函数(基础版)
Jun 03 PHP
php存储过程调用实例代码
Feb 03 PHP
学习php过程中的一些注意点的总结
Oct 25 PHP
PHP统计当前在线用户数实例讲解
Oct 21 PHP
简单的pgsql pdo php操作类实现代码
Aug 25 PHP
thinkphp跨库操作的简单代码实例
Sep 22 PHP
PHP读取word文档的方法分析【基于COM组件】
Aug 01 PHP
PHP生成指定范围内的N个不重复的随机数
Mar 18 PHP
php报错502badgateway解决方法
Oct 11 PHP
php多进程并发编程防止出现僵尸进程的方法分析
Feb 28 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启用zlib压缩文件的配置方法
2013/06/12 PHP
laravel 5 实现模板主题功能
2015/03/02 PHP
PHP7正式版测试,性能惊艳!
2015/12/08 PHP
js实现iframe动态调整高度的代码
2008/01/06 Javascript
不懂JavaScript应该怎样学
2008/04/16 Javascript
很可爱的输入框
2008/08/03 Javascript
Javascript 页面模板化很多人没有使用过的方法
2012/06/05 Javascript
jQuery动态设置form表单的enctype值(实现代码)
2013/07/04 Javascript
jquery中attr和prop的区别分析
2015/03/16 Javascript
JS模式之简单的订阅者和发布者模式完整实例
2015/06/30 Javascript
基于javascript实现右下角浮动广告效果
2016/01/08 Javascript
Vue中在新窗口打开页面及Vue-router的使用
2018/06/13 Javascript
JavaScript实现星级评价效果
2019/05/17 Javascript
利用Vue-draggable组件实现Vue项目中表格内容的拖拽排序
2019/06/07 Javascript
vue-video-player实现实时视频播放方式(监控设备-rtmp流)
2020/08/10 Javascript
vue集成openlayers加载geojson并实现点击弹窗教程
2020/09/24 Javascript
[47:46]完美世界DOTA2联赛 Magma vs GXR 第三场 11.07
2020/11/10 DOTA
[52:20]DOTA2-DPC中国联赛正赛 SAG vs XGBO3 第一场 3月5日
2021/03/11 DOTA
Python去除列表中重复元素的方法
2015/03/20 Python
深入浅析python 中的匿名函数
2018/05/21 Python
Django使用Jinja2模板引擎的示例代码
2019/08/09 Python
基于MATLAB和Python实现MFCC特征参数提取
2019/08/13 Python
详解在Python中以绝对路径或者相对路径导入文件的方法
2019/08/30 Python
Python paramiko模块使用解析(实现ssh)
2019/08/30 Python
python识别验证码的思路及解决方案
2020/09/13 Python
css3闪亮进度条效果实现思路及代码
2013/04/17 HTML / CSS
英国巧克力贸易公司:Chocolate Trading Company
2017/03/21 全球购物
怎样在程序里获得一个空指针
2015/01/24 面试题
董事长秘书工作职责
2014/06/10 职场文书
战略合作意向书
2014/07/29 职场文书
研究生论文答辩开场白
2015/05/27 职场文书
2016年社会主义核心价值观心得体会
2016/01/21 职场文书
中学教代会开幕词
2016/03/04 职场文书
竞选稿之小学班干部
2019/10/31 职场文书
详解python网络进程
2021/06/15 Python
Nginx内网单机反向代理的实现
2021/11/07 Servers