<?php
require_once("../../include/config.php");
require_once("../admin_loginstate.php");
session_start();
if($_GET['action']=='upload'){
	$fileType=array('jpg','gif','bmp','png',".jpeg");//允许上传的文件类型
	if ($_SESSION['imgtypes']=="topic"){
			$upfileDir=S_Timgfolder;
		}
	elseif($_SESSION['imgtypes']=="video"){
			$upfileDir=S_Vimgfolder;
		}
	else{
			$upfileDir=S_Aimgfolder;
		}	
	$maxSize=800; //单位：KB
	if(!in_array(substr($_FILES['file1']['name'],-3,3),$fileType))
		die("<script>alert('不允许上传该类型的文件！-808');window.parent.\$('divProcessing').style.display='none';history.back();</script>");
	if(strpos($_FILES['file1']['type'],'image')===false)
		die("<script>alert('不允许上传该类型的文件！');window.parent.\$('divProcessing').style.display='none';history.back();</script>");
	if($_FILES['file1']['size']> $maxSize*1024)
		die( "<script>alert('文件过大！');window.parent.\$('divProcessing').style.display='none';history.back();</script>");
	if($_FILES['file1']['error'] !=0)
		die("<script>alert('未知错误，文件上传失败！');window.parent.$('divProcessing').style.display='none';history.back();</script>");
        if($_GET['tid']=='hdtop'){
	          $targetDir='../../Upload/'.$upfileDir.'/';
        }else{
	          $targetDir='../../images/'.$upfileDir.'/';
        }
	$targetFile=date('Ymd').time().substr($_FILES['file1']['name'],-4,4);
	$realFile=$targetDir.$targetFile;
	if(function_exists('move_uploaded_file')){
		move_uploaded_file($_FILES['file1']['tmp_name'],$realFile);
		if(isset($_GET['types'])=="single"){
			echo "上传成功!!";
			if(S_WaterMark==1){
				imageWaterMark($targetDir.$targetFile,S_WaterLocation,S_WaterFont,S_WaterFontSize,S_WaterFontColor);
				echo "水印添加成功!";
				}
			           die("<script>parent.document.getElementById('S_Pic').value='".$targetFile."'</script>");
			}
		else{	
			if(S_WaterMark==1){
				imageWaterMark($targetDir.$targetFile,S_WaterLocation,S_WaterFont,S_WaterFontSize,S_WaterFontColor);
				}		
			die("<script>window.parent.LoadIMG('".S_SitePath."images/{$upfileDir}/{$targetFile}');</script>");
		}
	}
	else{
		@copy($_FILES['file1']['tmp_name'],$realFile);
		if(isset($_GET['types'])=="single"){
			if(S_WaterMark==1){
				imageWaterMark($targetDir.$targetFile,S_WaterLocation,S_WaterFont,S_WaterFontSize,S_WaterFontColor);
				echo "水印添加成功!";
				}
			echo "上传成功!!";
			           die("<script>parent.document.getElementById('S_Pic').value='".$targetFile."'</script>");
			}
		else{
			if(S_WaterMark==1){
				imageWaterMark($targetDir.$targetFile,S_WaterLocation,S_WaterFont,S_WaterFontSize,S_WaterFontColor);
				}
			die("<script>window.parent.LoadIMG('".S_SitePath."images/{$upfileDir}/{$targetFile}');</script>");
			}	
	}
}
function imageWaterMark($groundImage,$waterPos=0,$waterText="", $textFont=5,$textColor="#FF0000") 
{ 
//参数分别为：图片文件名,水印位置,水印文字,文字大小,文字颜色
      $isWaterImage = FALSE; 
      $formatMsg = "暂不支持该文件格式，请用图片处理软件将图片转换为GIF、JPG、PNG格式。"; 
    //  读取图片 
      if(!empty($groundImage) && file_exists($groundImage)) { //背景图片存在
          $ground_info = getimagesize($groundImage); //背景图片信息
          $ground_w      = $ground_info[0];//取得背景图片的宽 
          $ground_h      = $ground_info[1];//取得背景图片的高 

          switch($ground_info[2]) {     //取得背景图片的格式  
              case 1:$ground_im = imagecreatefromgif($groundImage);break; 
              case 2:$ground_im = imagecreatefromjpeg($groundImage);break; 
              case 3:$ground_im = imagecreatefrompng($groundImage);break; 
              default:die($formatMsg); 
          } 
      } else { 
          die("需要加水印的图片不存在！"); 
      } 
      //水印位置 
	$temp = imagettfbbox(ceil($textFont*2.5),0,"arial.ttf",$waterText);//取得使用 TrueType 字体的文本的范围 
          $w = $temp[2] - $temp[6]; 
          $h = $temp[3] - $temp[7];
          unset($temp); 
          //销毁$temp变量
          $label = "文字区域"; 
      //} 
      //if( ($ground_w<$w) || ($ground_h<$h) ) { //背景宽度/高度和水印的宽度/高度比较
          	//echo "需要加水印的图片的长度或宽度比水印".$label."还小，无法生成水印！"; 
         // return; 
      //} 
      switch($waterPos) { //水印的显示位置
          case 0://随机 
              $posX = rand(0,($ground_w - $w)); 
              $posY = rand(0,($ground_h - $h)); 
              break; 
          case 1://1为顶端居左 
              $posX = 0; 
              $posY = 0; 
              break; 
          case 2://2为顶端居中 
              $posX = ($ground_w - $w) / 2; 
              $posY = 0; 
              break; 
          case 3://3为顶端居右 
              $posX = $ground_w - $w; 
              $posY = 0; 
              break; 
          case 4://4为中部居左 
              $posX = 0; 
              $posY = ($ground_h - $h) / 2; 
              break; 
          case 5://5为中部居中 
              $posX = ($ground_w - $w) / 2; 
              $posY = ($ground_h - $h) / 2; 
              break; 
          case 6://6为中部居右 
              $posX = $ground_w - $w; 
              $posY = ($ground_h - $h) / 2; 
              break; 
          case 7://7为底端居左 
              $posX = 0; 
              $posY = $ground_h - $h; 
              break; 
          case 8://8为底端居中 
              $posX = ($ground_w - $w) / 2; 
              $posY = $ground_h - $h; 
              break; 
          case 9://9为底端居右 
              $posX = $ground_w - $w; 
              $posY = $ground_h - $h; 
              break; 
          default://随机 
              $posX = rand(0,($ground_w - $w)); 
              $posY = rand(0,($ground_h - $h)); 
              break;     
      } 

      //设定图像的混色模式 
      imagealphablending($ground_im, true); 
          if( !empty($textColor) && (strlen($textColor)==7) ) { 
              $R = hexdec(substr($textColor,1,2)); 
              $G = hexdec(substr($textColor,3,2)); 
              $B = hexdec(substr($textColor,5)); 
          } else { 
              die("水印文字颜色格式不正确！"); 
          } 
          imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));         

      //生成水印后的图片 
      @unlink($groundImage); 
      switch($ground_info[2]) {//取得背景图片的格式 
          case 1:imagegif($ground_im,$groundImage);break; 
          case 2:imagejpeg($ground_im,$groundImage);break; 
          case 3:imagepng($ground_im,$groundImage);break; 
          default:die($errorMsg); 
      } 

      //释放内存 
      if(isset($water_info)) unset($water_info); 
      if(isset($water_im)) imagedestroy($water_im); 
      unset($ground_info); 
      imagedestroy($ground_im); 
} 

?>