<?php
/**
 * http头部设置
 */
function headerOutput()
{
	header("Cache-Control:max-age=0");
	header("Content-type: image/png");
}

/**
 * 删除upload中零时文件
 * @param upload文件夹目录地址
 */
function deldir($dir) 
{
	$dh=opendir($dir);
	while ($file=readdir($dh)) 
	{
		if($file!="." && $file!="..") 
		{
			$fullpath=$dir."/".$file;
			if(!is_dir($fullpath)) 
			{
				unlink($fullpath);
			} 
			else 
			{
				deldir($fullpath);
			}
		}
	}
	closedir($dh);
}

/**
 * @param $im 图像句柄
 * 图像输出
 */
function imageOutput($im)
{
	imagepng($im,'output/googlestyle.png');
	imagedestroy($im);
}

function uploadFile()
{
	$pics = array();
	$retval="";
	chmod(ROOT.'/SitestarMaker/upload',0755);
	foreach($_FILES as $k => $v)
	{
		$file_name = iconv("UTF-8", "gb2312", $v['name']);
		$pt=strrpos($file_name, ".");
		$file = substr($file_name, 0,$pt);
		if(file_exists(ROOT.'/SitestarMaker/upload/'.$file.".gif"))
		{
			if ($pt) 
			{
				$retval=substr($file_name, $pt+1, strlen($file_name) - $pt);
			}
			else
			{
				die("无法获取文章的后缀名");
			}
			$str = rand(1,1000);
			$file_name = $str.".$retval";
		}
		move_uploaded_file($v['tmp_name'], ROOT.'/SitestarMaker/upload/'.$file_name);
		$pics[] = ROOT.'/SitestarMaker/upload/'.$file_name;
	}
	return $pics;
}

function convert2Gif($pics = array())
{
	chmod(ROOT.'/SitestarMaker/upload',0755);
	foreach($pics as $k => $v)
	{
		$pt=strrpos($v, ".");
		$retval=substr($v, $pt+1, strlen($v) - $pt);
		$file = substr($v, 0,$pt);
		if($retval != 'gif')
		{
			if($retval == 'png' || $retval == 'jpg' || $retval == 'jpeg')
			{
				if($retval == 'png')
				{
					$im_png = imagecreatefrompng($v);
					$flag = imagegif($im_png,$file.".gif");
				}
				elseif($retval == 'jpg' || $retval == 'jpeg')
				{
					$im_jpg = imagecreatefromjpeg($v);
					$flag = imagegif($im_jpg,$file.".gif");
				}
				
				if(empty($flag))
				{
					die("处理失败！");
				}
				else
				{
					unlink($v);
				}
				$pics[$k] = $file.".gif";
			}
			else
			{
				die("不支持此文件格式! <b><a href='index.php?_m=gifcompose'>返回</a></b>");
			}
		}
	}
	return $pics;
}

/**
 * 仿google样式
 * @return unknown_type
 */
function googlestyle()
{
	//----------------变量初始化[start]--------------------
	$font_size = 0;
	$logo_padding = 0;
	$width_info = array();
	$width_offset = 0;
	$height_offset = 0;
	$logo_text = $_POST['logo_text'];
	$arr = array();
	$color_list = array('0x0000ff','0xff0000','0x008000','0xffff00');
	headerOutput();
	//----------------变量初始化[end]----------------------
	
	//-----------------获取字体大小[start]------------------
	if($_POST['font_size'] == 'font_medimun')
	{
		$font_size = 50;
	}
	elseif($_POST['font_size'] == 'font_large')
	{
		$font_size = 100;
	}
	elseif($_POST['font_size'] == 'font_small')
	{
		$font_size = 25;
	}
	//-----------------获取字体大小[end]------------------
	
	//-----------------获取字体间距[start]----------------
	if($_POST['logo_padding'] == 'padding_medimun')
	{
		$logo_padding = 5;
	}
	elseif($_POST['logo_padding'] == 'padding_large')
	{
		$logo_padding = 15;
	}
	elseif($_POST['logo_padding'] == 'padding_small')
	{
		$logo_padding = 0;
	}
	//-----------------获取字体间距[end]-------------------
	
	
	//------------------获取画布大小[start]----------------
	for($i = 0; $i < strlen($logo_text); $i++)
	{
		$arr = imagettfbbox($font_size, 0, "Catull.TTF", $logo_text{$i});
		
		if(preg_match("/^a|c|d|e|k|o|s|x$/",$logo_text{$i}))
		{
			if($height_offset < 3)
			{
				$height_offset = 3;
			}
		}
		if(preg_match("/^g|j|p|q|y$/",$logo_text{$i}))
		{
			$height_offset = 21;
			if($font_size == 100)
			{
				$height_offset = 42;
			}
		}
		if(preg_match("/^6|8|9|0$/",$logo_text{$i}))//6,8,9,0需要增加宽度使其显示完整
		{
			if($height_offset == 0)
			{
				$height_offset = 1;
			}
			$width_info[] = $arr[2] - $arr[6] + 10;
		}
		else
		{
			$width_info[] = $arr[2]-$arr[6];//记录每个字的宽度
		}
	}
	$sum_padding = (strlen($logo_text) - 1) * $logo_padding;//获取所有字符之间的间距之和
	$canvas_width = array_sum($width_info) + $sum_padding;
	
	$arr1 = imagettfbbox($font_size, 0, "Catull.TTF", $logo_text);
	$canvas_height = $arr1[3]-$arr1[7] + $height_offset;
	
	$im = imagecreatetruecolor($canvas_width+2, $canvas_height);
	
	$white = imagecolorallocate($im, hexdec("0xff"), hexdec("0xff"), hexdec("0xff"));
	imagefilledrectangle ($im, 0, 0, $canvas_width+2, $canvas_height, $white);
	//------------------获取画布大小[end]-------------------
	
	//------------------给每个字上色[start]-----------------
	$x = 0;
	for($i = 0; $i < strlen($logo_text); $i++)
	{
		$key = rand(0,count($color_list)-1);
		imagettftext($im, $font_size, 0, $x+3, $arr1[3]-$arr1[7], hexdec($color_list[$key]), 'Catull.TTF', $logo_text{$i});
		$x += $width_info[$i] + $logo_padding;
	}
	//------------------给每个字上色[end]-------------------
	if($_POST['bg_color'] == 'transparent')
	{
		$white = imagecolorallocate($im, 255, 255, 255);
		imagecolortransparent($im,$white);
	}
	//-----------------------修改sitestarmaker.xml文件[start]--------------
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('file_name')->item(0)->nodeValue = 'SitestarMaker/output/googlestyle.png';
	$xml->getElementsByTagName('custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
	//-----------------------修改sitestarmaker.xml文件[end]----------------
	imageOutput($im);
}

/**
 * 静态图片合成动画gif
 */
function gifCompose()
{
	if(empty($_FILES['pic1']) || empty($_FILES['pic2']))
	{
		die("必须要上传两张图片！<b><a href='index.php?_m=gifcompose'>返回</a></b>");
		sleep(1000);
	}
	$pics = array();
	$pics = uploadFile($_FILES);
	$pics1 = convert2Gif($pics);
	
	include_once "lib/GIFDecoder.class.php";
	include_once "lib/GIFEncoder.class.php";
	include_once "lib/lib.php";
	$speed = $_POST['speed'];
	if(empty($speed))
	{
		$speed = 10;
	}
	$i = 1;
	foreach($pics1 as $k => $v)
	{
		$dly[] = $speed;
		$frames[] = $v;
		$key_color = 'pic'.$i.'_color';
		$key_txt = 'pic'.$i.'_txt';
		writeTxt2Img($v, 12, 12, $_POST[$key_txt], "Catull.TTF", 12, $_POST[$key_color]);
		$i++;
	}
	$gifenc = new GIFEncoder($frames, $dly, 0, 2, 255,255,255, "url");
//	$output_file = date('YmdHis',time()).rand(0,100);
	$savefile = "output/gifcompose.gif";
	fwrite (fopen($savefile, 'wb'), $gifenc->GetAnimation());
	
	//------------------随机删除临时文件[start]-------------------
	$token = 8;
	if(rand(0,10) == $token)
	{
		chmod("upload",0755);
		deldir("upload");
	}
	//------------------随机删除临时文件[end]---------------------
	
	//-----------------------修改sitestarmaker.xml文件[start]--------------
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('file_name')->item(0)->nodeValue = 'SitestarMaker/output/gifcompose.gif';
	$xml->getElementsByTagName('custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
	//-----------------------修改sitestarmaker.xml文件[end]----------------
	
	header("Location:index.php?_m=gifcompose");
	die;
}

/**
 * logo明信片样式
 */
function techsir()
{
	//--------------对字体的不同处理[start]------------------
	if($_POST['logo_font'] == 'regular')
	{
		$font_file = 'fzxqjw.tt_.ttf';
	}
	elseif($_POST['logo_font'] == 'bold')
	{
		$font_file = 'bold.ttf';
	}
	elseif($_POST['logo_font'] == 'italic')
	{
		$font_file = 'TitanItalic.ttf';
	}
	else
	{
		$font_file = 'fzkatjw.tt_.ttf';
	}
	//--------------对字体的不同处理[end]--------------------
	
	//--------------对图片大小的不同处理[start]------------------
	if($_POST['logo_size'] == 'small')
	{
		$im = imagecreatefromjpeg('images/logo_small.jpg');
		$font_size = 11;
		$y = 110;
	}
	elseif($_POST['logo_size'] == 'large')
	{
		$im = imagecreatefromjpeg('images/logo_large.jpg');
		$font_size = 29;
		$y = 215;
	}
	elseif($_POST['logo_size'] == 'medium')
	{
		$im = imagecreatefromjpeg('images/logo_medium.jpg');
		$font_size = 15;	
		$y = 155;
	}
	else
	{
		$im = imagecreatefromjpeg('images/logo_medium.jpg');
		$font_size = 11;
		$y = 110;
	}
	
	$_POST['logo_color'] = empty($_POST['logo_color']) ? '0xbfeecc' : str_replace('#','0x',$_POST['logo_color']);
	$_POST['left_padding'] = empty($_POST['left_padding']) ? 0 : $_POST['left_padding'];
	$_POST['logo_text'] = empty($_POST['logo_text']) ? 'cndns' : $_POST['logo_text'];
	//--------------对图片大小的不同处理[end]--------------------
	imagettftext($im,$font_size,0,$_POST['left_padding'],$y,hexdec($_POST['logo_color']),$font_file,$_POST['logo_text']);
	imagejpeg($im,'output/techsir.jpg',100);
	
	//-----------------------修改sitestarmaker.xml文件[start]--------------
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('file_name')->item(0)->nodeValue = 'SitestarMaker/output/techsir.jpg';
	$xml->getElementsByTagName('custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
	//-----------------------修改sitestarmaker.xml文件[end]----------------
}

function chooseLogo()
{
	/*Array
	(
    [id] => chooseLogo
    [logo_text] => cndns
    [logo_color] => #000000
    [font_family] => simsun
    [font_size] => 12
	)*/
	//-----------------------提交参数校验检查[start]----------------------
	$_POST['logo_color'] = empty($_POST['logo_color']) ? '0x000000' : str_replace('#','0x',$_POST['logo_color']);
	$_POST['logo_text'] = empty($_POST['logo_text']) ? 'cndns' : $_POST['logo_text'];
	$_POST['font_family'] = empty($_POST['font_family']) ? 'simsun' : $_POST['font_family'];
	$_POST['font_size'] = empty($_POST['font_size']) ? 30 : $_POST['font_size'];
	$logo_message = simplexml_load_file('SitestarMaker.xml');
	$pic_src = (empty($logo_message->choose_logo->logo_name)) ? 'http://image.sitestar.cn/logo/6.jpg' : $logo_message->choose_logo->logo_name;
	//-----------------------提交参数校验检查[end]-------------------------
	
	//-----------------------获取字体长度[start]---------------------------
	if ( substr(PHP_OS,0,3) == 'WIN')
	{
		$system_path = str_replace('\\','/',$_SERVER['SystemRoot']);
		$font_file = empty($system_path) ? 'C:/Windows' : $system_path;
		$font_file = (file_exists("$font_file/Fonts/{$_POST['font_family']}.ttf")) ? "$font_file/Fonts/{$_POST['font_family']}.ttf" : "$font_file/Fonts/{$_POST['font_family']}.ttc";
		
		if(!file_exists($font_file))
		{
			if(file_exists("../../font/{$_POST['font_family']}.ttf"))
			{
				$font_file = "../../font/{$_POST['font_family']}.ttf";
			}
			elseif(file_exists("../../font/{$_POST['font_family']}.ttc"))
			{
				$font_file = "../../font/{$_POST['font_family']}.ttc";
			}
			else
			{
				$str = "notice提示：系统缺少{$_POST['font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
				die($str);
			}
		}
	}
	else
	{
		if(file_exists("../../font/{$_POST['font_family']}.ttf"))
		{
			$font_file = "../../font/{$_POST['font_family']}.ttf";
		}
		elseif(file_exists("../../font/{$_POST['font_family']}.ttc"))
		{
			$font_file = "../../font/{$_POST['font_family']}.ttc";
		}
		else
		{
			$str = "notice提示：系统缺少{$_POST['font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
			die($str);
		}
	}
	$im_txt = imagecreate(500,500);
	$arr = imagettftext($im_txt,$_POST['font_size'],0,10,10,hexdec($_POST['logo_color']),$font_file,$_POST['logo_text']);
	imagedestroy($im_txt);
	//-----------------------获取字体长度[end]-----------------------------
	
	//-----------------------构造图片[start]-------------------------------
	$text_width = $arr[2] - $arr[6];
	$canvas_width = 10 + 88 + 15 + $text_width + 10;//左内边距+图片宽度+图片与字体边距+字体长度+右边距
	$canvas_height = 10 + 88;//上下边距总和10 + 图片高度
	$des_im = imagecreatetruecolor($canvas_width,$canvas_height);
	$white = imagecolorallocate($des_im, 255, 255, 255);
	
	$src_im = imagecreatefromjpeg($pic_src);
	imagefilledrectangle($des_im, 0, 0, $canvas_width-1, $canvas_width-1, $white);
	imagecopy($des_im,$src_im,10,5,0,0,88,88);
	imagettftext($des_im,$_POST['font_size'],0,10+88+15,$canvas_height/2+$_POST['font_size']/2-2,hexdec($_POST['logo_color']),$font_file,$_POST['logo_text']);
	imagejpeg($des_im,'output/chooselogo.jpg',100);
	imagedestroy($des_im);
	imagedestroy($src_im);
	//-----------------------构造图片[end]---------------------------------
	
	//-----------------------修改sitestarmaker.xml文件[start]--------------
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('file_name')->item(0)->nodeValue = 'SitestarMaker/output/chooselogo.jpg';
	$xml->getElementsByTagName('custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
	
	//-----------------------修改sitestarmaker.xml文件[end]----------------
}

function puretext()
{
//    [id] => puretext
//    [check_website] => true    显示网址
//    [logo_text] => 美橙互联     LOGO文本
//    [font_family] => simsun  logo字体
//    [font_size] => 25        logo字体字号
//    [logo_website] => www.logomaker.cn   logo网址
//    [website_font_family] => simsun      网址字体
//    [website_font_size] => 20            网址字号
//    [logo_border] => 2                   边框宽度
//    [logo_text_color] => 0066cc          文字颜色  
//    [website_color] => 0066cc            网址颜色
//    [border_color] => 0066cc             边框颜色
//    [logo_shadow] => false                  logo字体阴影
//    [website_shadow] => false			        网址字体阴影			
//    [bg_color] => white                  图片背景
//	  [website_shadow_color]
// 	  [logo_shadow_color]
	$_POST['logo_text'] = empty($_POST['logo_text']) ? '美橙互联' : $_POST['logo_text'];
	$_POST['logo_website'] = empty($_POST['logo_website']) ? 'www.sitestar.cn' : $_POST['logo_website'];
	$_POST['logo_text_color'] = empty($_POST['logo_text_color']) ? '0x0066cc' : str_replace('#','0x',$_POST['logo_text_color']);
	$_POST['website_color'] = empty($_POST['website_color']) ? '0x0066cc' : str_replace('#','0x',$_POST['website_color']);
	$_POST['border_color'] = empty($_POST['border_color']) ? '0x0066cc' : str_replace('#','0x',$_POST['border_color']);
	$_POST['website_shadow_color'] = empty($_POST['website_shadow_color']) ? '0x000000' : str_replace('#','0x',$_POST['website_shadow_color']);
	$_POST['logo_shadow_color'] = empty($_POST['logo_shadow_color']) ? '0x000000' : str_replace('#','0x',$_POST['logo_shadow_color']);
	$system_path = str_replace('\\','/',$_SERVER['SystemRoot']);
	if(substr(PHP_OS,0,3) == 'WIN')
	{
		$logo_font_file = empty($system_path) ? 'C:/Windows' : $system_path;
		$logo_font_file = (file_exists("$logo_font_file/Fonts/{$_POST['font_family']}.ttf")) ? "$logo_font_file/Fonts/{$_POST['font_family']}.ttf" : "$logo_font_file/Fonts/{$_POST['font_family']}.ttc";
		
		if(!file_exists($logo_font_file))
		{
			if(file_exists("../../font/{$_POST['font_family']}.ttf"))
			{
				$logo_font_file = "../../font/{$_POST['font_family']}.ttf";
			}
			elseif(file_exists("../../font/{$_POST['font_family']}.ttc"))
			{
				$logo_font_file = "../../font/{$_POST['font_family']}.ttc";
			}
			else
			{
				$str = "notice提示：系统缺少{$_POST['font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
				die($str);
			}
		}
		
		$website_font_file = empty($system_path) ? 'C:/Windows' : $system_path;
		$website_font_file = (file_exists("$website_font_file/Fonts/{$_POST['website_font_family']}.ttf")) ? "$website_font_file/Fonts/{$_POST['website_font_family']}.ttf" : "$website_font_file/Fonts/{$_POST['website_font_family']}.ttc";
		
		if($_POST['check_website'] == 'true')
		{
			if(!file_exists($website_font_file))
			{
				if(file_exists("../../font/{$_POST['website_font_family']}.ttf"))
				{
					$website_font_file = "../../font/{$_POST['website_font_family']}.ttf";
				}
				elseif(file_exists("../../font/{$_POST['website_font_family']}.ttc"))
				{
					$website_font_file = "../../font/{$_POST['website_font_family']}.ttc";
				}
				else
				{
					$str = "notice提示：系统缺少{$_POST['website_font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['website_font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
					die($str);
				}
			}
		}
	}
	else
	{
		if(file_exists("../../font/{$_POST['font_family']}.ttf"))
		{
			$logo_font_file = "../../font/{$_POST['font_family']}.ttf";
		}
		elseif(file_exists("../../font/{$_POST['font_family']}.ttc"))
		{
			$logo_font_file = "../../font/{$_POST['font_family']}.ttc";
		}
		else
		{
			$str = "notice提示：系统缺少{$_POST['font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
			die($str);
		}
		
		if($_POST['check_website'] == 'true')
		{
			if(file_exists("../../font/{$_POST['website_font_family']}.ttf"))
			{
				$website_font_file = "../../font/{$_POST['website_font_family']}.ttf";
			}
			elseif(file_exists("../../font/{$_POST['website_font_family']}.ttc"))
			{
				$website_font_file = "../../font/{$_POST['website_font_family']}.ttc";
			}
			else
			{
				$str = "notice提示：系统缺少{$_POST['website_font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['website_font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
				die($str);
			}
		}
	}
	//-------------------获得画布大小[start]----------------------
	$im_logo_txt = imagecreate(1000,1000);
	$logo_arr =  imagettftext($im_logo_txt,$_POST['font_size'],0,10,10,hexdec($_POST['logo_text_color']),$logo_font_file,$_POST['logo_text']);
	$logo_txt_width = $logo_arr[2] - $logo_arr[6];
	imagedestroy($im_logo_txt);
	
	$website_txt_width = 0;
	if($_POST['check_website'] == 'true')
	{
		$im_website_txt = imagecreate(1000,1000);
		$website_arr = imagettftext($im_website_txt,$_POST['website_font_size'],0,10,10,hexdec($_POST['website_color']),$website_font_file,$_POST['logo_website']);
		$website_txt_width = $website_arr[2] - $website_arr[6];
		imagedestroy($im_website_txt);
	}
	
	$canvas_width = ($logo_txt_width >= $website_txt_width) ? $logo_txt_width + 22 : $website_txt_width + 22;//左右边距各11
	$canvas_width += $_POST['logo_border'] * 2;
	if($_POST['check_website'] == 'true')
	{
		$canvas_height = ($logo_arr[3] - $logo_arr[7]) + ($website_arr[3] - $website_arr[7]) + 6 + 6;
	}
	else
	{
		$canvas_height = ($logo_arr[3] - $logo_arr[7])+ 6;//上下边距3
	}
	$canvas_height += $_POST['logo_border'] * 2;
	//-------------------获得画布大小[end]------------------------
	
	//-------------------构建有边框宽度的矩形[start]--------------
	$im = imagecreatetruecolor($canvas_width+1, $canvas_height+1);
	imagefilledrectangle($im, 0, 0, $canvas_width, $canvas_height, hexdec($_POST['border_color']));
	$white = imagecolorallocate($im, 255, 255, 255);
	imagefilledrectangle($im, $_POST['logo_border'], $_POST['logo_border'], $canvas_width - $_POST['logo_border'] * 2 + $_POST['logo_border'], $canvas_height - $_POST['logo_border'] * 2 + $_POST['logo_border'], $white);
	//-------------------构建有边框宽度的矩形[end]----------------
	
	if($_POST['check_website'] == 'true')//启用网址字样
	{
		if($logo_txt_width >= $website_txt_width)
		{
			$param = ($canvas_width - $website_txt_width) / 2;
			$param = (int)$param;
			//	array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
			//---------------写域名信息[start]-------------------
			imagettftext($im, $_POST['website_font_size'], 0, $param, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7]) + ($website_arr[3] - $website_arr[7])+6, hexdec($_POST['website_color']), $website_font_file, $_POST['logo_website']);
			if($_POST['website_shadow'] == 'true')
			{
				imagettftext($im, $_POST['website_font_size'], 0, $param+1, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7]) + ($website_arr[3] - $website_arr[7])+6+1, hexdec($_POST['website_shadow_color']), $website_font_file, $_POST['logo_website']);
			}
			//---------------写域名信息[end]---------------------
			
			//---------------Logo文字[start]---------------------
			imagettftext($im, $_POST['font_size'], 0, $_POST['logo_border']+11, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7]), hexdec($_POST['logo_text_color']), $logo_font_file, $_POST['logo_text']);
			if($_POST['logo_shadow'] == 'true')
			{
				imagettftext($im, $_POST['font_size'], 0, $_POST['logo_border']+11+1, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7])+1, hexdec($_POST['logo_shadow_color']), $logo_font_file, $_POST['logo_text']);
			}
			//---------------Logo文字[end]---------------------
		}
		else
		{
			$param = ($canvas_width - $logo_txt_width) / 2;
			$param = (int)$param;
			//---------------Logo文字[start]---------------------
			imagettftext($im, $_POST['font_size'], 0, $param, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7]), hexdec($_POST['logo_text_color']), $logo_font_file, $_POST['logo_text']);
			if($_POST['logo_shadow'] == 'true')
			{
				imagettftext($im, $_POST['font_size'], 0, $param+1, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7])+1, hexdec($_POST['logo_shadow_color']), $logo_font_file, $_POST['logo_text']);
			}
			//---------------Logo文字[end]---------------------
			
			//---------------写域名信息[start]-------------------
			imagettftext($im, $_POST['website_font_size'], 0, $_POST['logo_border']+11, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7]) + ($website_arr[3] - $website_arr[7])+6, hexdec($_POST['website_color']), $website_font_file, $_POST['logo_website']);
			if($_POST['website_shadow'] == 'true')
			{
				imagettftext($im, $_POST['website_font_size'], 0, $_POST['logo_border']+11+1, $_POST['logo_border']+3+($logo_arr[3] - $logo_arr[7]) + ($website_arr[3] - $website_arr[7])+6+1, hexdec($_POST['website_shadow_color']), $website_font_file, $_POST['logo_website']);
			}
			//---------------写域名信息[end]---------------------
		}
	}
	else
	{
		//---------------Logo文字[start]---------------------
//		$param = ($canvas_height - ($logo_arr[3] - $logo_arr[7])) / 2;
//		$param = (int)($param);
		$param = 3 + ($logo_arr[3] - $logo_arr[7]);
		imagettftext($im, $_POST['font_size'], 0, $_POST['logo_border']+11, $param, hexdec($_POST['logo_text_color']), $logo_font_file, $_POST['logo_text']);
		if($_POST['logo_shadow'] == 'true')
		{
			imagettftext($im, $_POST['font_size'], 0, $_POST['logo_border']+11+1, $param+1, hexdec($_POST['logo_shadow_color']), $logo_font_file, $_POST['logo_text']);
		}
		//---------------Logo文字[end]---------------------
	}
	
	if($_POST['bg_color'] == 'transparent')
	{
		imagecolortransparent($im,$white);
	}
	imagepng($im,'output/puretext.png');
	//-----------------------修改sitestarmaker.xml文件[start]--------------
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('file_name')->item(0)->nodeValue = 'SitestarMaker/output/puretext.png';
	$xml->getElementsByTagName('custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
	//-----------------------修改sitestarmaker.xml文件[end]----------------
}

function TextScroll()
{
//	  [id] => TextScroll
//    [logo_pic] => logo006
//    [logo_text] => Logo在线制作12
//    [ScrollType] => top_bottom
//    [font_size] => 14
//    [font_family] => simkai
//    [text_color] => #ed03ed

	include_once("lib/GIFEncoder.class.php");
	ob_end_clean();
	
	$system_path = str_replace('\\','/',$_SERVER['SystemRoot']);
	$font_file = empty($system_path) ? 'C:/Windows' : $system_path;
	$font_file = (file_exists("$font_file/Fonts/{$_POST['font_family']}.ttf")) ? "$font_file/Fonts/{$_POST['font_family']}.ttf" : "$font_file/Fonts/{$_POST['font_family']}.ttc";
	$_POST['text_color'] = empty($_POST['text_color']) ? '0xFFFFFF' : str_replace('#','0x',$_POST['text_color']);
	$count=0;
	while(true)
	{
		$str = empty($_POST['logo_text']) ? $_POST['logo_text']:"CNDNS";
		$length=strlen($str)*9;
		$im=imagecreatefromgif("images/{$_POST['logo_pic']}.gif");
//		$str = iconv('GB2312','UTF-8"',$str);
		$now=220-$count*5;
		imagettftext($im,$_POST['font_size'],0,$now,20,hexdec($_POST['text_color']),$font_file,$str);
		
		imagegif($im); 
		imagedestroy($im);
		$imagedata[] = ob_get_contents();
		ob_clean();
		$count++;
		if ($now+$length<0)
		{
			break;
		}
	}
	$diy[]=0;
	$gif = new GIFEncoder($imagedata,$diy,0,2,0,0,0,"bin");
	
	ob_start();
//	header ('Content-type:image/gif');
	$data = $gif->GetAnimation();
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('pic_data')->item(0)->nodeValue = $data;
	$xml->getElementsByTagName('file_name')->item(0)->nodeValue = 'SitestarMaker/output/chooselogo.jpg';
	$xml->getElementsByTagName('custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
}

function logoPopUp()
{
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('logo_name')->item(0)->nodeValue = $_POST['src'];
	$xml->save('SitestarMaker.xml');
}

function snowPopup()
{
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('swfbanner_name')->item(0)->nodeValue = $_POST['banner_background'];
	$xml->save('SitestarMaker.xml');
}

function noPopup()
{
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('swfbanner_name')->item(0)->nodeValue = '';
	$xml->save('SitestarMaker.xml');
}

function chooseBanner()
{
//	 [id] => chooseBanner
//    [logo_text] => ddfff2
//    [logo_color] => #f705f7
//    [font_family] => simhei
//    [font_size] => 18
//    [bannertext_left] => 50px
//    [bannertext_top] => 50px
	if( substr(PHP_OS,0,3) == 'WIN')
	{
		$system_path = str_replace('\\','/',$_SERVER['SystemRoot']);
		$logo_font_file = empty($system_path) ? 'C:/Windows' : $system_path;
		$logo_font_file = (file_exists("$logo_font_file/Fonts/{$_POST['font_family']}.ttf")) ? "$logo_font_file/Fonts/{$_POST['font_family']}.ttf" : "$logo_font_file/Fonts/{$_POST['font_family']}.ttc";
		if(!file_exists($logo_font_file))
		{
			if(file_exists("../../font/{$_POST['font_family']}.ttf"))
			{
				$logo_font_file = "../../font/{$_POST['font_family']}.ttf";
			}
			elseif(file_exists("../../font/{$_POST['font_family']}.ttc"))
			{
				$logo_font_file = "../../font/{$_POST['font_family']}.ttc";
			}
			else
			{
				$str = "notice提示：系统缺少{$_POST['font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
				die($str);
			}
		}
	}
	else
	{
		if(file_exists("../../font/{$_POST['font_family']}.ttf"))
		{
			$logo_font_file = "../../font/{$_POST['font_family']}.ttf";
		}
		elseif(file_exists("../../font/{$_POST['font_family']}.ttc"))
		{
			$logo_font_file = "../../font/{$_POST['font_family']}.ttc";
		}
		else
		{
			$str = "notice提示：系统缺少{$_POST['font_family']}字体文件，请您下载字体文件解压后把ttc或ttf文件上传到建站之星程序所在目录下font目录中  <a href='http://download.sitestar.cn/font/{$_POST['font_family']}.zip'><font style='color:red;'>请点击此处下载</font></a>";
			die($str);
		}
	}
	$_POST['logo_text'] = empty($_POST['logo_text']) ? '请输入文字' : $_POST['logo_text'];
	$_POST['logo_color'] = empty($_POST['logo_color']) ? '0x000000' : str_replace('#','0x',$_POST['logo_color']);
	$left = substr($_POST['bannertext_left'], 0, -2);
	$top = substr($_POST['bannertext_top'], 0, -2);
	$banner_message = simplexml_load_file('SitestarMaker.xml');
	
	$im_logo_txt = imagecreate(1000,1000);
	$logo_arr =  imagettftext($im_logo_txt,$_POST['font_size'],0,10,10,hexdec($_POST['logo_color']),$logo_font_file,$_POST['logo_text']);
	$logo_txt_height = $logo_arr[3] - $logo_arr[7];
	imagedestroy($im_logo_txt);
	
	if($banner_message->choose_banner->output_file == 'no')
	{
		$imgname = empty($banner_message->choose_banner->banner_name) ? 'http://image.sitestar.cn/banner/1.png' : $banner_message->choose_banner->banner_name;
	}
	else
	{
		$imgname = 'output/choosebanner.png';
	}
	$im = imagecreatefrompng($imgname);
//	array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )

	imagettftext($im,$_POST['font_size']*3/4,0,$left,$top+$logo_txt_height,hexdec($_POST['logo_color']),$logo_font_file,$_POST['logo_text']);
	imagepng($im,'output/choosebanner.png');
	imagedestroy($im);
	
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('output_file')->item(0)->nodeValue = 'yes';
	$xml->getElementsByTagName('banner1_name')->item(0)->nodeValue = 'SitestarMaker/output/choosebanner.png';
	$xml->getElementsByTagName('banner1_custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
}

function snowSwf()
{
//	[id] => snowSwf
//    [BackgroundImage] => logo001
//    [maxCellsize] => 11
//    [NumberOfCells] => 100
//    [CellsDirection] => MoveUp
//    [CellsSpeed] => 6
//    [text_n] => ddd
//    [font_size_n] => 32
//    [color_n] => #05f6af
//    [falign_n] => left
//    [a] => 2
	$banner_message = simplexml_load_file('SitestarMaker.xml');
	if(($banner_message->swf_banner->swfbanner_name) == '')
	{
		$pos = strpos($_POST['BackgroundImage'], 'banner00');
		if($pos === false)
		{
			list($width, $height, $type, $attr) = getimagesize("upload/banner/{$_POST['BackgroundImage']}");
			$somecontent = "<?xml version='1.0' encoding='UTF-8'?><Content TheBannerWidth = '$width' TheBannerHeight = '$height' maxCellsize = '{$_POST['maxCellsize']}' NumberOfCells = '{$_POST['NumberOfCells']}' CellsSpeed = '{$_POST['CellsSpeed']}' CellsDirection = '{$_POST['CellsDirection']}' BackgroundImage = 'upload/banner/{$_POST['BackgroundImage']}'>";
		}
		else
		{
			list($width, $height, $type, $attr) = getimagesize("images/{$_POST['BackgroundImage']}.jpg");
			$somecontent = "<?xml version='1.0' encoding='UTF-8'?><Content TheBannerWidth = '$width' TheBannerHeight = '$height' maxCellsize = '{$_POST['maxCellsize']}' NumberOfCells = '{$_POST['NumberOfCells']}' CellsSpeed = '{$_POST['CellsSpeed']}' CellsDirection = '{$_POST['CellsDirection']}' BackgroundImage = 'images/{$_POST['BackgroundImage']}.jpg'>";
		}
		
	}
	else
	{
		list($width, $height, $type, $attr) = getimagesize($banner_message->swf_banner->swfbanner_name);
		$somecontent = "<?xml version='1.0' encoding='UTF-8'?><Content TheBannerWidth = '$width' TheBannerHeight = '$height' maxCellsize = '{$_POST['maxCellsize']}' NumberOfCells = '{$_POST['NumberOfCells']}' CellsSpeed = '{$_POST['CellsSpeed']}' CellsDirection = '{$_POST['CellsDirection']}' BackgroundImage = '{$banner_message->swf_banner->swfbanner_name}'>";
	}

	for($i = 1;$i < 21;$i++)
	{
		$key_txt = "text_$i";
		$key_color = "color_$i";
		$key_font_size = "font_size_$i";
		$key_falign = "falign_$i";
		if(empty($_POST[$key_txt]))
		{
			continue;
		}
		$_POST[$key_color] = empty($_POST[$key_color]) ? '#05f6af' : $_POST[$key_color];
		$_POST[$key_font_size] = empty($_POST[$key_font_size]) ? '32' : $_POST[$key_font_size];
		$somecontent .= "<title PauseText='1.5'><![CDATA[<p align='{$_POST[$key_falign]}'><font size='$_POST[$key_font_size]' color='{$_POST[$key_color]}'>$_POST[$key_txt]</font></p>]]></title>";
	}
	$somecontent .= "</Content>";
	//---------------写入xml文件[start]--------------
	$filename = 'Banner_Content_3.xml';
	if (is_writable($filename)) 
	{
	    if (!$handle = fopen($filename, 'w')) 
	    {
	         die("不能打开文件 $filename");
	    }
	
	    if (fwrite($handle, $somecontent) === FALSE) 
	    {
	        die("不能写入到文件 $filename");
	    }
//	    echo "成功地将 $somecontent 写入到文件$filename";
	    fclose($handle);
	} 
	else 
	{
	    die("文件 $filename 不可写");
	}
	
	//-----------------------修改sitestarmaker.xml文件[start]--------------
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('banner1_name')->item(0)->nodeValue = 'SitestarMaker/SnowSwfPlay.php';
	$xml->getElementsByTagName('banner1_custom')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
	//-----------------------修改sitestarmaker.xml文件[end]----------------
	
	//---------------写入xml文件[end]----------------
}

function rotateBanner()
{
//	( [id] => rotateBanner [play]=> auto [pic_heading1] => rfrfrfr [pic_paragraph1] => erwerwer 
//	[pic_link1] => www.sina.com [pic_heading2] => ererer [pic_paragraph2] => werwerew 
//	[pic_link2] => www.sohu.com ) 
//Array ( [pic1] => Array ( [name] => 007.31.gif [type] => image/gif [tmp_name] => D:\MyPhpConfig\temp\php2CE.tmp [error] => 0 [size] => 4644 ) [pic2] => Array ( [name] => 2010-06-13_124741.jpg [type] => image/jpeg [tmp_name] => D:\MyPhpConfig\temp\php2CF.tmp [error] => 0 [size] => 4016 ) )
	
	foreach($_FILES as $k => $v)
	{
		if($v['size'] == 0)
		{
			echo "图片上传过大，已超过服务器配置值&nbsp;&nbsp;<a href='index1.php?_m=rotatebanner'>返回</a>";die;
		}
	}
	//-----------------------提交图片进行处理[start]---------------------
	$pics = array();
	$effect = '';
	$str = '';
	$autoplay = '';
	$link = '';
	chmod(ROOT.'/SitestarMaker/cu3er/images',0755);
	foreach($_FILES as $k => $v)
	{
		$file_name = iconv("UTF-8", "gb2312", $v['name']);
		if(file_exists(ROOT.'/SitestarMaker/cu3er/images/'.$file_name))
		{
			chmod(ROOT.'/SitestarMaker/cu3er/images/'.$file_name,0755);
			unlink(ROOT.'/SitestarMaker/cu3er/images/'.$file_name);
		}
		move_uploaded_file($v['tmp_name'], ROOT.'/SitestarMaker/cu3er/images/'.$file_name);
		$pics[] = ROOT.'/SitestarMaker/cu3er/images/'.$file_name;
	}
	//-----------------------提交图片进行处理[end]----------------------
	
	//-----------------------构造xml代码[start]--------------------
	if($_POST['play'] == 'auto')
	{
		$autoplay = '<auto_play><defaults symbol="circular" time="3" /><tweenIn x="500" y="50" width="35" height="35" tint="0xFFFFFF" /></auto_play>';
	}
	
	for($i = 1;$i < 21;$i++)
	{
		$key = 'pic'.$i;
		$key_pic_link = 'pic_link'.$i;
		$key_pic_heading = 'pic_heading'.$i;
		$key_pic_paragraph = 'pic_paragraph'.$i;
		if(empty($_FILES[$key]))
		{
			continue;
		}
		else
		{
			$rand = rand(1,7);
			if((4 == $rand) && ($i != 1))//随机选择图片变换效果
			{
				$effect = '<transition num="3" slicing="vertical" direction="down"/>';
			}
			elseif((5 == $rand) && ($i != 1))
			{
				$effect = '<transition num="4" direction="right" shader="flat" />';
			}
			elseif((6 == $rand) && ($i != 1))
			{
				$effect = '<transition num="6" slicing="vertical" direction="up" shader="flat" delay="0.05" z_multiplier="4" />';
			}
			$str .= "$effect<slide><url>images/{$_FILES[$key]['name']}</url>";
			if(!(empty($_POST[$key_pic_link]) && empty($_POST[$key_pic_heading]) && empty($_POST[$key_pic_paragraph])))
			{
				if(!empty($_POST[$key_pic_link]))
				{
					$link = "<link>http://$_POST[$key_pic_link]</link>";
				}
				$str .= "<description>$link<heading>$_POST[$key_pic_heading]</heading><paragraph>$_POST[$key_pic_paragraph]</paragraph></description></slide>";
			}
			else
			{
				$str .= '</slide>';
			}
		}
	}
	
	$somecontent = <<<str1
<?xml version="1.0" encoding="utf-8" ?>
<cu3er>
	<settings>
    	<prev_button>
			<defaults round_corners="5,5,5,5"/>
			<tweenOver tint="0xFFFFFF" scaleX="1.1" scaleY="1.1"/>
			<tweenOut tint="0x000000" />
		</prev_button>
    	<prev_symbol>
			<tweenOver tint="0x000000" />			
		</prev_symbol>
    	<next_button>
			<defaults round_corners="5,5,5,5"/>			
			<tweenOver tint="0xFFFFFF"  scaleX="1.1" scaleY="1.1"/>
			<tweenOut tint="0x000000" />
		</next_button>
    	<next_symbol>
			<tweenOver tint="0x000000" />
		</next_symbol>	
		$autoplay
	</settings>    
	<slides>	
		$str
	</slides>
</cu3er>
str1;
	//-----------------------构造xml代码[end]--------------------
	
	//---------------写入xml文件[start]--------------
	$filename = ROOT.'/SitestarMaker/cu3er/config.xml';
//	chmod($filename,0775);
	if (is_writable($filename)) 
	{
	    if (!$handle = fopen($filename, 'w')) 
	    {
	         die("不能打开文件");
	    }
	
	    if (fwrite($handle, $somecontent) === FALSE) 
	    {
	        die("不能写入到文件 $filename");
	    }
//	    echo "成功地将 $somecontent 写入到文件$filename";
	    fclose($handle);
	} 
	else 
	{
	    die("文件 $filename 不可写");
	}
	//---------------写入xml文件[end]----------------
	
	//---------------根据上传图片的长宽取最小值设置画布长宽[start]----------------
	if(!empty($pics))
	{
		$min_width = array();
		$min_height = array();
		foreach($pics as $k => $v)
		{
			list($width, $height, $type, $attr) = getimagesize($v);
			$min_width[] = $width;
			$min_height[] = $height;
		}
		arsort($min_width);
		arsort($min_height);
		$m_width = array_pop($min_width);
		$m_height = array_pop($min_height);
		$xml = new DOMDocument('1.0','utf-8');
		$xml->load('SitestarMaker.xml');
		$xml->getElementsByTagName('width')->item(0)->nodeValue = $m_width;
		$xml->getElementsByTagName('height')->item(0)->nodeValue = $m_height;
		$xml->getElementsByTagName('banner1_name')->item(0)->nodeValue = 'SitestarMaker/cu3er/index.php';
		$xml->getElementsByTagName('banner1_custom')->item(0)->nodeValue = 'yes';
		$xml->save('SitestarMaker.xml');
	}
	header("Location:index1.php?_m=RotateBanner");
	die;
	//---------------根据上传图片的长宽取最小值设置画布长宽[end]-----------------
}

function bannerPopUp()
{
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('banner_name')->item(0)->nodeValue = $_POST['banner_background'];
	$xml->getElementsByTagName('output_file')->item(0)->nodeValue = 'no';
	$xml->save('SitestarMaker.xml');
}

function useOutput()
{
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('output_file')->item(0)->nodeValue = 'yes';
	$xml->save('SitestarMaker.xml');
}

function getSize()
{
	$xml = new DOMDocument('1.0','utf-8');
	$xml->load('SitestarMaker.xml');
	$xml->getElementsByTagName('banner_width')->item(0)->nodeValue = $_POST['banner_width'];
	$xml->getElementsByTagName('banner_height')->item(0)->nodeValue = $_POST['banner_height'];
	$xml->getElementsByTagName('logo_height')->item(0)->nodeValue = $_POST['logo_height'];
	$xml->getElementsByTagName('logo_width')->item(0)->nodeValue = $_POST['logo_width'];
	$xml->save('SitestarMaker.xml');
}

function snowUpload()
{
	if($_FILES['custom_pic']['size'] == 0)
	{
		echo "上传文件可能过大<a href='index1.php?_m=snowswf'>返回</a>";die;
	}
	$file_name = $_FILES['custom_pic']['name'];
	$ext_name = substr($file_name, -3);
	if($ext_name == 'gif' || $ext_name == 'jpg' || $ext_name == 'png' || $ext_name == 'bmp')
	{
		$random = rand(1,1000);
		$random1 = rand(1,1000);
		$str = 'f';
		if($random == 1) $str = 'a';
		elseif($random == 5) $str = 'b';
		elseif($random == 10) $str = 'c';
		$str .= $random.$random1;
		$flag = move_uploaded_file($_FILES['custom_pic']['tmp_name'], ROOT."/SitestarMaker/upload/banner/$str.$ext_name");
		if($flag)
		{
			header("Location:index1.php?_m=SnowSwf");
			die;
		}
		else
		{
			echo "上传文件失败<a href='index1.php?_m=SnowSwf'>返回</a>";die;
		}
	} 
	else
	{
		echo "请上传图片文件<a href='index1.php?_m=SnowSwf'>返回</a>";die;
	}
}

try
{
	error_reporting(0);
	header("Content-type: text/html; charset=utf-8");
	define('ROOT', realpath(dirname(__FILE__).'/..'));
	$_POST['id']();
}
catch(Exception $ex)
{
	die("Error!");
}
?>