<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?
@header("Expires: 0");
@header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
define('SD_ROOT', dirname(__FILE__).'/');
include "../../include/conn.php";
include "../../include/function.php";
$pic_id = time();
$pic_path = '../../Upload/userlogo/origin/'.$pic_id.'.jpg';
$url = 'http://'.$_SERVER['HTTP_HOST'];
$pic_abs_path = ''.$url.'/Upload/userlogo/origin/'.$pic_id.'.jpg';
if(empty($_FILES['Filedata'])) {
	echo '<script type="text/javascript">alert("对不起, 图片未上传成功, 请再试一下");</script>';
	exit();
}
$file = @$_FILES['Filedata']['tmp_name'];
file_exists($pic_path) && @unlink($pic_path);
if(@copy($_FILES['Filedata']['tmp_name'], $pic_path) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $pic_path)) 
{
	@unlink($_FILES['Filedata']['tmp_name']);
} else {
	@unlink($_FILES['Filedata']['tmp_name']);
	echo '<script type="text/javascript">alert("对不起, 上传失败");</script>';
}
$targetDir=$pic_path;
if(S_WaterMark==1){
imageWaterMark($targetDir,S_WaterLocation,S_WaterFont,S_WaterFontSize,S_WaterFontColor);
}
//验证不正确图片
$tpyz = file_type($pic_path);
if($tpyz!="gif" and $tpyz!="jpg" and $tpyz!="png" and $tpyz!="bmp") {
//删除不正确图片
$fileurl=$pic_path;
$fileurl="../".substr($fileurl,strlen(S_SitePath));
if(file_exists($fileurl)){
unlink($fileurl);
}
	echo '<script type="text/javascript">alert("上传失败，图片类型'.$tpyz.'不正确!");</script>';
	exit();
}
echo '<script type="text/javascript">window.parent.hideLoading();window.parent.buildAvatarEditor("'.$pic_id.'","'.$pic_abs_path.'","photo");</script>';
//判断图片类型
function file_type($filename)   
{   
    $file = fopen($filename, "rb");   
    $bin = fread($file, 2); //只读2字节   
    fclose($file);   
    $strInfo = @unpack("C2chars", $bin);   
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);   
    $fileType = '';   
    switch ($typeCode)   
    {   
        case 7790:   
            $fileType = 'exe';   
            break;   
       case 7784:   
            $fileType = 'midi';   
            break;   
        case 8297:   
            $fileType = 'rar';   
            break;           
        case 8075:   
            $fileType = 'zip';   
           break;   
        case 255216:   
            $fileType = 'jpg';   
            break;   
        case 7173:   
            $fileType = 'gif';   
            break;   
        case 6677:   
            $fileType = 'bmp';   
            break;   
        case 13780:   
            $fileType = 'png';   
            break;   
        default:   
            $fileType = 'unknown: '.$typeCode;   
    }   
  
    //Fix   
    if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';   
    if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';   
  
    return $fileType;   
} 
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); 
} 
?>
</body>
</html>