php 生成文字png图片的代码

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?
/*
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实现获取客户端IP并获取IP信息

代码很简洁,功能很实用,这里就不多废话了,直接奉上: 复制代码 代码如下: <?php /**  * 获取客户端IP  * @param  i...

PHP 解决session死锁的方法

今天在开发碰到个棘手的问题 。当异步请求后台处理一个大数据量操作时  请求其他控制器都没返回信息了。。起初以为是Ext 框架设置了ajax同步造成的。后来发现时session...

php数组合并与拆分实例分析

本文实例讲述了php数组合并与拆分的方法。分享给大家供大家参考。具体如下: <?php $array1 = array("A","B","C","D"); $arr...

PHP共享内存使用与信号控制实例分析

本文实例讲述了PHP共享内存使用与信号控制。分享给大家供大家参考,具体如下: 共享内存 共享内存的使用主要是为了能够在同一台机器不同的进程中共享一些数据,比如在多个 php-fpm 进程...

php获取远程图片并下载保存到本地的方法分析

本文实例讲述了php获取远程图片并下载保存到本地的方法。分享给大家供大家参考,具体如下: 远程图片指的是远端服务器上的数据我们可以通过php的许多函数来读取下载了,这里整理了两个可以自动...