<?php
error_reporting(0);
if(!empty($_GET))
{
	
	$font = 'skft/'.$_GET['font'];	//字体
	$font = iconv('UTF-8','GB2312',$font);
	$text = $_GET['text'];  //文字
	$size = $_GET['size'];  //字体大小
	$jiaodu = $_GET['jiaodu'];  //旋转角度
	$lpx = $_GET['lpx'];  //左边距
	$tpx = $_GET['tpx'];  //上边距
	$ztys = '#'.$_GET['ztys'];	//字体颜色
	$bjys = '#'.$_GET['bjys'];	//背景颜色
	$widthg = $_GET['width'];	//设置宽度
	$heightg = $_GET['height'];	//设置高度
	$touying = $_GET['touying'];	//投影
	$jiacu = $_GET['jiacu'];	//加粗

	function charwidth($fontsize,$fontangle,$ttfpath,$char){
		$box = @imagettfbbox($fontsize,$fontangle,$ttfpath,$char);
		$width = max($box[2], $box[4]) - min($box[0], $box[6]);
		return $width;
	}

	function charheight($fontsize,$fontangle,$ttfpath,$char){
		$box = @imagettfbbox($fontsize,$fontangle,$ttfpath,$char);
		$height = max($box[3], $box[5]) - min($box[1], $box[7]);
		return $height;
	}

	function autowrap($fontsize, $angle, $fontface, $string, $width) {
	// 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
	 $content = "";

	 // 将字符串拆分成一个个单字 保存到数组 letter 中
	 for ($i=0;$i<mb_strlen($string);$i++) {
	  $letter[] = mb_substr($string, $i, 1);
	 }

	 foreach ($letter as $l) {
	  $teststr = $content." ".$l;
	  $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
	  // 判断拼接后的字符串是否超过预设的宽度
	  if (($testbox[2] > $width) && ($content !== "")) {
	   $content .= "\n";
	  }
	  $content .= $l;
	 }
	 return $content;
	}
	//旋转后的大小
	function calculateTextBox($font_size, $font_angle, $font_file, $text) {
		$box = imagettfbbox($font_size, $font_angle, $font_file, $text);
		$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
		$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
		$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
		$max_y = max(array($box[1], $box[3], $box[5], $box[7]));
		return array(
			'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2),
			'top' => abs($min_y),
			'width' => $max_x - $min_x,
			'height' => $max_y - $min_y,
			'box' => $box
		);
	}
	//颜色转换
	function hex2rgb($hexColor) {
		$color = str_replace('#', '', $hexColor);
		if (strlen($color) > 3) {
			$rgb = array(
				'r' => hexdec(substr($color, 0, 2)),
				'g' => hexdec(substr($color, 2, 2)),
				'b' => hexdec(substr($color, 4, 2))
			);
		} else {
			$color = $hexColor;
			$r = substr($color, 0, 1) . substr($color, 0, 1);
			$g = substr($color, 1, 1) . substr($color, 1, 1);
			$b = substr($color, 2, 1) . substr($color, 2, 1);
			$rgb = array(
				'r' => hexdec($r),
				'g' => hexdec($g),
				'b' => hexdec($b)
			);
		}
		return $rgb;
	}
	$box = imagettfbbox($size, 0, $font, $text);
    $text = autowrap($size,$jiaodu,$font,$text,360); // 自动换行处理
	$bbox = calculateTextBox($size, $jiaodu, $font, $text);	//旋转画布
	if($jiaodu == 0){
		$btop = $size;
		$bleft = $size;
	}else{
		$btop = $size/2;
		$bleft = $size/2;
	}
	//print_r($bbox);
	$width1 = abs($box[2] - $box[0]) + $bleft;
	$height1 = abs($box[7] - $box[1]) + $btop;
	$width = $bbox[width] + $bleft;
	$height = $bbox[height] + $btop;
	//自定义宽度
	if(!empty($widthg)){
		$width = $widthg;
	}
	if(!empty($heightg)){
		$height = $heightg;
	}
	//echo $width;exit;
	//echo "<br>";
	//echo $height;
	$image = imagecreatetruecolor($width, $height);
	//颜色设置
	$ztys = hex2rgb($ztys);
	$bjys = hex2rgb($bjys);
	$color = imagecolorallocate($image, $ztys[r],$ztys[g],$ztys[b]); //字体颜色
	$bgcolor = imagecolorallocate($image, $bjys[r],$bjys[g],$bjys[b]); //背景颜色
	$grey = imagecolorallocate($image, 128, 128, 128); //投影颜色
			
	if(!empty($lpx)){
		$x = $lpx;
		$y = $tpx + $size;
	}else{
		$x = $box[0] + ($width1 / 2) - ($box[4] / 2);
		$y = $box[1] + ($height1 / 2) - ($box[5] / 2);
	}
	if($jiaodu == 0){
		imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
		imagettftext($image, $size, $jiaodu, $x, $y, $color, $font, $text);
	}
	$last_pixel= imagecolorat($image, 0, 0);
	for ($j = 0; $j < $height; $j++)
	{
		for ($i = 0; $i < $width; $i++)
		{
			if (isset($blank_left) && $i >= $blank_left)
			{
				break;
			}
			if (imagecolorat($image, $i, $j) !== $last_pixel)
			{
				if (!isset($blank_top))
				{
					$blank_top = $j;
				}
				$blank_left = $i;
				break;
			}
			$last_pixel = imagecolorat($image, $i, $j);
		}
	}
	//$x -= $blank_left;
	//$y -= $blank_top;
	//自定义边距
	if(!empty($lpx)){
		$x = $lpx;
		$y = $tpx + $size;
	}else{
		if($jiaodu == 0){
			$x -= $blank_left - $bleft/2;
			$y -= $blank_top - $btop/2;
		}else if($jiaodu > 20){
			$x = $size/2;
			$y = $height - $size/2;
		}else{
			if(strpos( $text , "\n" )){
				$x = $size/1.5;
				$y = $size*3.5;
			}else{
				$x = 20;
				$y = $height - 15;
			}
		}
	}
	
	imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
	
	//加粗
	if($jiacu == 1){
		imagettftext($image, $size, $jiaodu, $x+1, $y, $color, $font, $text);
	}
	//投影
	if($touying == 1){
		imagettftext($image, $size, $jiaodu, $x + 1, $y + 1, $grey, $font, $text);
	}

	imagettftext($image, $size, $jiaodu, $x, $y, $color, $font, $text);
	
	header('Content-type: image/png');
	imagepng($image);
	imagedestroy($image);
}
?>