<?php
 $tmpsiteid=1;
 $userid=1;
 function ImageType($image){
//echo "<br>image==".$image;
  list($width, $height, $type, $attr) = @getimagesize($image);
  switch($type){
          case 1 :
                  $returntype="gif";
                  break;
          case 2 :
                  $returntype="jpg";
                  break;
        case 3 :
                  $returntype="png";
                  break;
          case 4 :
                  $returntype="swf";
                  break;
        case 5 :
                  $returntype="psd";
                  break;
          case 6 :
                  $returntype="bmp";
                  break;
        case 7 :
                  $returntype="tiff";
                  break;
          case 8 :
                  $returntype="tiff";
                  break;
        default :
            $returntype= "notsupportted" ;
                break;

  }
  
   return $returntype.":$width:$height";
}
 
 function NewimageCreatefromtype($image){
  $newimage=$image;
  list($width, $height, $type, $attr) = @getimagesize($image);
  if($type=="1"){  //gif
    $returnimage=@imagecreatefromgif($image);
  }else if($type=="2"){ //jpeg
    $returnimage=@imagecreatefromjpeg($image);
  }else if($type=="3"){ //png
    $returnimage=@imagecreatefrompng($image);
  }else{
    $returnimage="Not Supported";
     
  }
  return $returnimage;
}
function ReturnImagetype($newImage,$newfile,$editimagefile){
  list($width, $height, $type, $attr) = @getimagesize($editimagefile);
  $jpgCompression="60";
  if($type=="1"){  //gif
    $returnimage= @imagegif($newImage, $newfile);
  }else if($type=="2"){ //jpeg
    $returnimage=@imagejpeg($newImage, $newfile, $jpgCompression);;
  }else if($type=="3"){ //png
    $returnimage= @imagepng($newImage, $newfile);
  }else{
    $returnimage="Not Supported";
  }
  return $returnimage;
}
 function watermarkimage($watermarkimage){

    $newimagetostore=$watermarkimage;
    $watermark = imagecreatefromgif('./images/watermarkflipNEW.gif');
	$watermark_small =imagecreatefromgif('./images/watermarkflipsmallnew.gif');
	$watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);
	$watermark_widthsmall = imagesx($watermark_small);
    $watermark_heightsmall = imagesy($watermark_small);
	$image = NewimageCreatefromtype($watermarkimage);
    $size = @getimagesize($watermarkimage);
   // $dest_x = $size[0] - $watermark_width - 5;
    //$dest_y = $size[1] - $watermark_height - 5;
	$dest_x = $size[0]/2;
    $dest_y = 10;
	  
    $rgb = @ImageColorAt($image, $dest_x, $dest_y);
	
	if($size[1]>600){
	 	   @imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 30);
		
	}else if ($size[1]>300) {
	       @imagecopymerge($image, $watermark_small, $dest_x, $dest_y, 0, 0, $watermark_widthsmall, $watermark_heightsmall, 30);
	}else{
	  		@imagecopymerge($image, $watermark_small, $dest_x, $dest_y, 0, 0, $watermark_widthsmall, $watermark_heightsmall, 30);
	}
	ReturnImagetype($image,$newimagetostore,$watermarkimage);  
}
 
 // watermarkimage("./gallery/images/".$assignedname);
 if($_POST['btnuploadtype']=="Upload"){
         //echo "heree";
		$filename=$_FILES['imgfile']['name'];
		$imagewidth_height_type_array=explode(":",ImageType($_FILES['imgfile']['tmp_name']));
		$imagetype=$imagewidth_height_type_array[0];
		$assignedname=$userid."_".time().".".$imagetype;
		
        move_uploaded_file($_FILES['imgfile']['tmp_name'],"../../usergallery/$userid/images/".$assignedname);
		chmod("../../usergallery/$userid/images/".$assignedname,0777);
		copy("../../usergallery/$userid/images/".$assignedname,"../../workarea/tempsites/$tmpsiteid/images/".$assignedname);
		chmod("../../workarea/tempsites/$tmpsiteid/images/".$assignedname,0777);
		// move_uploaded_file($_FILES['imgfile']['tmp_name'],"../workarea/tempsites/$tmpsiteid/images/".$_FILES['imgfile']['name']);
		watermarkimage("../../workarea/tempsites/$tmpsiteid/images/".$assignedname);
		//add the file to resources
		$fp=fopen("../../tempsites/$tmpsiteid/resource.txt","a");
		//$filenameu="workarea/tempsites/$tmpsiteid/images/".$filename;
		$filenameu="usergallery/$userid/images/".$assignedname;
		fwrite($fp,$filenameu."\n");
		fclose($fp);
	    echo "<script>opener.insertimage('./images/".$assignedname."');window.close();</script>";
  }		 
?>
<html>
<head>
	<title>SiteBuilder::Upload Image</title>
	<link href="../style/site.css" rel="stylesheet" type="text/css">
	<SCRIPT>
	 function canceltblpro(){
	        window.close();
	  }
	  function savetblpro(){
			document.frmImageUpload.submit();
	        //opener.settablervalue(tborder,tborderclr,bgclr,cells,cellp,talign,twidth);
            //window.close();
	  }
	
	  
	</SCRIPT>
</head>
<body>
<table width="100%" cellpadding=2 cellpadding=2 align=center>
   <tr>
      <td>
	                  <FIELDSET>
					      <legend class=maintextbold>Upload Image</legend>
						 <form method="post" enctype="multipart/form-data"  name="frmImageUpload">
						    <table>
							      <tr>
								     <td class=maintext>File</td>
								     <td class=maintext>
									   <input class=button type=file name=imgfile>
									 </td>
								  </tr>
							      <tr>
								    <td colspan=4 align=center><input id=btnuploadtype  name=btnuploadtype class=button  type=submit value="Upload" >
										<input class=button  type=button value="Cancel" onclick="canceltblpro();"></td>
								    </tr>								 
							  </table>						  
						  </form>
					  </FIELDSET>
	  </td>
	</tr>
</table>	  				  	  

</body>
</html>
<script>
//window.reset();
/*document.getElementById("trows").value="";
document.getElementById("tcolumns").value="";
document.getElementById("tbgclr").value="";
document.getElementById("tcellpading").value="";
document.getElementById("tcellspacing").value="";
document.getElementById("tborder").value="";
document.getElementById("tborderclr").value="";*/
</script>
