<?php
function CreateTxt2Img($title,$width=690,$height=40,$r=50,$g=81,$b=161)
{
	$strat=0;
	$n = $tn = $noc = 0;
	$char ="";
	$font_spacing=3;
	$font_size=26;
	$text = array();
	$iii =0;
	$StartX   = 0;
	$WidthTwo = 0;
	$font_width_max=0;
	$wordStr='1234567890-=~!@#$%^&*()_+{}[]<>,.:;?|\/"\'abcdefghijklmnopqrstuvwxyz';
	
	$font= "fonts/fzdhj2.ttf";
	$title  = str_replace(array('&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array(' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $title);	
	$strlen = strlen($title);
	$font_coordinate0 = imagettfbbox ( $font_size, 0, $font, "点" );
	$font_width0 = $font_coordinate0[2] - $font_coordinate0[0];
	$font_height0 = $font_coordinate0[3] - $font_coordinate0[5];
	
	while($n < $strlen)
		{
			$t = ord($title[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { $tn = 1; $n++; $noc++;
			} elseif(194 <= $t && $t <= 223) { $tn = 2; $n += 2; $noc += 2;$strat +=34.2;
			} elseif(224 <= $t && $t <= 239) { $tn = 3; $n += 3; $noc += 2;
			} elseif(240 <= $t && $t <= 247) { $tn = 4; $n += 4; $noc += 2;
			} elseif(248 <= $t && $t <= 251) { $tn = 5; $n += 5; $noc += 2;
			} elseif($t == 252 || $t == 253) { $tn = 6; $n += 6; $noc += 2;
			} else { $n++; }
			$s = 0;
			if($n-$tn >0)$s = $n-$tn;
			$char = substr($title, $s , $tn);
			$font_coordinate = imagettfbbox ( $font_size,0, $font, $char );
			$font_width = $font_coordinate[2] - $font_coordinate[0]+$font_spacing;
			
			$text[$iii] =$char;
			$iii ++;
			$strat +=$font_width + $font_spacing/3;
		}
		if($strat<$width)
		{
			$StartX   = ($width-$strat)/2;
			$WidthTwo = $width;
		}else if($strat == $width)
		{
			$StartX   = 0;
			$WidthTwo = $strat;
		}
		else {$WidthTwo = $strat;}	
		$im = imagecreate ($WidthTwo ,$height);
  		$col_oth = imagecolorallocate($im, 255,255,255);
  		$col_wit = imagecolorallocate($im, $r,$g,$b);

		$strat = $StartX;
		foreach($text as $k=>$v)
		{
			$font_coordinate = imagettfbbox ( $font_size,0, $font, $v);
			//echo $v."　左下(".$font_coordinate[0].",".$font_coordinate[1].")　右下(".$font_coordinate[2].",".$font_coordinate[3].")　右上(".$font_coordinate[4].",".$font_coordinate[5].")　左上(".$font_coordinate[6].",".$font_coordinate[7].")";
			$font_width = $font_coordinate[2] - $font_coordinate[0];
			$font_width_max=$font_width;
			$pos1 = stripos($wordStr, $v);
			if ($pos1 === false)
			{
				if ($font_width_max < $font_width0)
				{
					$font_width_max = $font_width0;
				}
				$position_x_write = $strat + ($font_width_max + $font_spacing - $font_width)*0.6;
			}
			else
			{
				$position_x_write = $strat + ($font_width_max + $font_spacing - $font_width)*0.5;
			}
			$font_width = $font_width_max + $font_spacing;
			
			$font_height = abs($font_coordinate[5]) - $font_coordinate[3];

			//echo " W=".$font_width_max."　H=".$font_height."<br>";

			imagettftext($im,$font_size,0,$position_x_write,32,$col_wit,$font, $v);//写标题
			$strat +=$font_width ;	
		}
		//imagejpeg($im,"Temp.jpg",100);
		imagepng($im);
    imagedestroy($im);
		/*$imagecontent = @file_get_contents("Temp.jpg");
		$source = @imagecreatefromstring($imagecontent);
		$setw = $width;
		$seth = 40;
		$thumb = @imagecreatetruecolor($setw, $seth);
		imagecopyresized($thumb, $source, 0, 0, 0, 0, $setw, $seth, $width, 40);
		return "/include/temp.jpg";*/
}
$r1 = '50';
$g1 = '81';
$b1 = '161';
$h1 = '40';
$w1 = '690';
$t1 = '';
if(isset($_GET['t'])) $t1 =$_GET['t'];
if(isset($_GET['w'])) $w1 =$_GET['w'];
if(isset($_GET['h'])) $h1 =$_GET['h'];
if(isset($_GET['r'])) $r1 =$_GET['r'];
if(isset($_GET['g'])) $g1 =$_GET['g'];
if(isset($_GET['b'])) $b1 =$_GET['b'];
CreateTxt2Img($t1,$w1,$h1,$r1,$g1,$b1);
?>