将文字生成png图片

将指定的文字生成png图片,可自行定义文字,字体,字体颜色,背景颜色等

<?
/*
php生成文字png图片,可以使用如下方式调用函数:

*/
Header("Content-type: image/png");
$msg="测试文字生成图片";//$_POST['msg'];   //文字
$fil="a.png";//$_POST['fil'];   //生成图片的名称

//$fontS=$_POST['fontS']; //字体
$fontC="#00aeef";//$_POST['fontC'];  //字体颜色#000或#000000
//$font = 'FONTS/'.$fontS;

$fontColor=hex2rgb($fontC);

$red = $fontColor['red']; // 字体颜色
$grn = $fontColor['green'];
$blu = $fontColor['blue'];

function hex2rgb( $colour ) { 
    if ( $colour[0] == '#' ) { 
        $colour = substr( $colour, 1 ); 
    } 
    if ( strlen( $colour ) == 6 ) { 
        list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] ); 
    } elseif ( strlen( $colour ) == 3 ) { 
        list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] ); 
    } else { 
        return false; 
    } 
    $r = hexdec( $r ); 
    $g = hexdec( $g ); 
    $b = hexdec( $b ); 
    return array( 'red' => $r, 'green' => $g, 'blue' => $b ); 
} 
//测试 
//var_dump(hex2rgb("#eeeeee"));

class textPNG { 
	var $font = 'FONTS/88.otf';//默认字体. 相对于脚本存放目录的相对路径.
	var $msg = "undefined"; // 默认文字.
	var $size = 40;
	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;
	var $fil = "1.png";
	function draw() {
		$width = 0;
		$height = 0;
		$offset_x = 10;
		$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+20;
			$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+20;
			$offset_x = abs($bounds[0]-$bounds[6]);
		} else {
			$width = abs($bounds[4]-$bounds[6])+20;
			$height = abs($bounds[7]-$bounds[1])+40;
			$offset_y = $font_height+5+20;
			$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,$this->fil);
	}
}
$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).
if (isset($fil)) $text->fil = $fil; 
$text->draw();
?> 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值