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学习之运算符相关概念
Jun 09 PHP
PHP实现获取图片颜色值的方法
Jul 11 PHP
php的闭包(Closure)匿名函数详解
Feb 22 PHP
php文件下载处理方法分析
Apr 22 PHP
php获取英文姓名首字母的方法
Jul 13 PHP
PHP程序中的文件锁、互斥锁、读写锁使用技巧解析
Mar 21 PHP
php编程中echo用逗号和用点号连接的区别
Mar 26 PHP
centos+php+coreseek+sphinx+mysql之一coreseek安装篇
Oct 25 PHP
php获取当前月与上个月月初及月末时间戳的方法
Dec 05 PHP
PHP 应用容器化以及部署方法
Feb 12 PHP
Laravel6.18.19如何优雅的切换发件账户
Jun 14 PHP
php中配置文件保存修改操作 如config.php文件的读取修改等操作
May 12 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
114啦源码(114la)不能生成地方房产和地方报刊问题4级页面0字节的解决方法
2012/01/12 PHP
apache php模块整合操作指南
2012/11/16 PHP
PHP数组无限分级数据的层级化处理代码
2012/12/29 PHP
php获取301跳转URL简单实例
2013/12/16 PHP
PHP使用第三方即时获取物流动态实例详解
2017/04/27 PHP
PHP类与对象后期静态绑定操作实例详解
2018/12/20 PHP
Ajax一统天下之Dojo整合篇
2007/03/24 Javascript
fix-ie5.js扩展在IE5下不能使用的几个方法
2007/08/20 Javascript
用js查找法实现当前栏目的高亮显示的代码
2007/11/24 Javascript
jq选项卡鼠标延迟的插件实例
2013/05/13 Javascript
jquery遍历数组与筛选数组的方法
2013/11/05 Javascript
javascript 判断两个日期之差的示例代码
2015/09/05 Javascript
解决angular的$http.post()提交数据时后台接收不到参数值问题的方法
2015/12/10 Javascript
基于jQuery实现的打字机效果
2017/01/16 Javascript
原生JS实现简单放大镜效果
2017/02/08 Javascript
js正则表达式验证密码强度【推荐】
2017/03/03 Javascript
webpack2.0配置postcss-loader的方法
2017/08/17 Javascript
Javascript中 toFixed四舍六入方法
2017/08/21 Javascript
vue实现nav导航栏的方法
2017/12/13 Javascript
Javascript读写cookie的实例源码
2019/03/16 Javascript
Vue2.4+新增属性.sync、$attrs、$listeners的具体使用
2020/03/08 Javascript
antd的select下拉框因为数据量太大造成卡顿的解决方式
2020/10/31 Javascript
vue element el-transfer增加拖拽功能
2021/01/15 Vue.js
读写json中文ASCII乱码问题的解决方法
2016/11/05 Python
python基于http下载视频或音频
2018/06/20 Python
Flask框架通过Flask_login实现用户登录功能示例
2018/07/17 Python
Pytorch之parameters的使用
2019/12/31 Python
Selenium 滚动页面至元素可见的方法
2020/03/18 Python
详解Python之Scrapy爬虫教程NBA球员数据存放到Mysql数据库
2021/01/24 Python
KEETSA环保床垫:更好的睡眠,更好的生活!
2016/11/24 全球购物
圣诞树世界:Christmas Tree World
2019/12/10 全球购物
linux面试题参考答案(6)
2014/08/29 面试题
学生干部的自我评价分享
2014/01/18 职场文书
战略性融资合作协议书范本
2014/10/17 职场文书
户外活动总结
2015/02/04 职场文书
nginx lua 操作 mysql
2022/05/15 Servers