<?php
  include "../includes/session.php";
  include "../includes/config.php";
  include "../includes/function.php";
  $tmpsiteid=$_SESSION['session_currenttempsiteid'];
  $templateid=$_SESSION['session_currenttemplateid'];
  $userid=$_SESSION["session_userid"];
  $siteid=$_SESSION["session_siteid"];
  $relativepathtoserver="../";
  
  require_once './includes/class.cropinterface.php'; 
  $ci = new cropInterface(); 
  
  //$sitetype="new";
  $imagenamefromparentwindow=$_GET['imagename'];
  echo "imagefromparent==".$imagenamefromparentwindow;
  echo "<br>";
  $sitetype=$_GET['type'];
  $sitetype="new";
 
  $imagetoedit=substr($imagenamefromparentwindow,3,7);
  
  if($imagetoedit=="logoima"){
	   $tp_band="tp_logoimage.jpg";
	   $newimagename="ug_logoimage_";
  }else if($imagetoedit=="company"){
	   $tp_band="tp_company.jpg";
	   $newimagename="ug_company_";
  }
  echo "templateband=".$tp_band;
  if($tp_band !=""){
     //check logo file exist
     if(!is_file($relativepathtoserver."templates/".$templateid."/images/".$tp_band)){
       echo "Image could not found.Please try again";
	   exit;
     }
     $_SESSION['session_imagetoedit']=$tp_band;
	 $actuallogbandloc=$relativepathtoserver."templates/".$templateid."/images/".$tp_band;
	 if($sitetype=="new"){  //tempsite
	     $_SESSION['session_currentimage']=$newimagename.$userid."_t".$templateid.".jpg";
		 copy($actuallogbandloc,$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$tp_band);
		 $templatebandlocation=$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$tp_band;
		 $_SESSION['session_editedimage']=$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$imagenamefromparentwindow;
	 }else{
	     $_SESSION['session_currentimage']="ug_logoband_".$userid."_s".$templateid.".jpg";
	     copy($actuallogbandloc,$relativepathtoserver."workarea/sites/$siteid/images/".$tp_band);
		 $templatebandlocation=$relativepathtoserver."workarea/sites/$siteid/images/".$tp_band;
		 $_SESSION['session_editedimage']=$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$imagenamefromparentwindow;
	 }
	 $_SESSION['session_templatebandlocation']=$templatebandlocation;
	 $_SESSION['session_actualbanlocation']=$actuallogbandloc;
  }
  echo "<br>bandlocation==". $_SESSION['session_templatebandlocation'];
  
 
  $errormessagefileupload="";
  if($_POST['btnuploadtype']=="Upload"){

                         if($_FILES['fileimage']['size'] >0){
							        list($width, $height, $type, $attr) = @getimagesize($_FILES['fileimage']['tmp_name']);
									
							        if($type>3 or $type==""){
									    $errormessagefileupload="Please upload only .jpg,.gif,.png image types";
									}else{
									
								        $imagewidth_height_type_array=explode(":",ImageType( $_SESSION['session_actualbanlocation']));
										
										$imagetype=$imagewidth_height_type_array[0];
										$imagewidth=$imagewidth_height_type_array[1];
										$imageheight=$imagewidth_height_type_array[2];
										$assignedname=$_SESSION['session_currentimage'];
										$assignednameforupload="ug_".time().$_FILES['fileimage']['name'];
										
										if($sitetype=="new"){
										           move_uploaded_file($_FILES['fileimage']['tmp_name'],$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$assignednameforupload);
										           chmod($relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$assignednameforupload,0777);
												   $resizedimage=$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/ug_rez_".$assignednameforupload;
												   $uplodaedimage=$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$assignednameforupload;
												   
												   Resizeimage($uplodaedimage,$imageheight,$imagewidth,$resizedimage);
												   chmod($resizedimage,0777);
												   //copy the actual template
												   copy($_SESSION['session_actualbanlocation'],$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$_SESSION['session_currentimage']);
												   embedimage($relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$_SESSION['session_currentimage'],$resizedimage);
												   unlink($resizedimage);
												   @copy($relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$_SESSION['session_currentimage'],$relativepathtoserver."usergallery/$userid/images/".$_SESSION['session_currentimage']);
												   watermarkimage($relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$assignedname);
												   $_SESSION['session_editedimage']=$relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$_SESSION['session_currentimage'];
									    }else{
										           move_uploaded_file($_FILES['fileimage']['tmp_name'],$relativepathtoserver."workarea/sites/$siteid/images/".$assignednameforupload);
										           chmod($relativepathtoserver."workarea/sites/$siteid/images/".$assignednameforupload,0777);
												   $resizedimage=$relativepathtoserver."workarea/sites/$siteid/images/ug_rez_".$assignednameforupload;
												   $uplodaedimage=$relativepathtoserver."workarea/sites/$siteid/images/".$assignednameforupload;
												   
												   Resizeimage($uplodaedimage,$imageheight,$imagewidth,$resizedimage);
												   chmod($resizedimage,0777);
												   //copy the actual template
												   copy($_SESSION['session_actualbanlocation'],$relativepathtoserver."workarea/sites/$siteid/images/".$_SESSION['session_currentimage']);
												   embedimage($relativepathtoserver."workarea/sites/$siteid/images/".$_SESSION['session_currentimage'],$resizedimage);
												   unlink($resizedimage);
												   @copy($relativepathtoserver."workarea/sites/$siteid/images/".$_SESSION['session_currentimage'],$relativepathtoserver."usergallery/$userid/images/".$_SESSION['session_currentimage']);
												   $_SESSION['session_editedimage']=$relativepathtoserver."workarea/sites/$siteid/images/".$_SESSION['session_currentimage'];
										
										
										}
								}
								
						}		
	
  }
  if(isset($_POST['hfile']) !="" and ($_GET['action']=="addtext" or $_POST['haction']=="addtext") ){

						     $image = NewimageCreatefromtype($_POST['hfile']);
							 //find the actual width
						       list($width, $height, $type, $attr) = @getimagesize($_POST['hfile']);
						       $widthpercentage=$width/450;
							   $heightpercentage=$height/450;
							   if($widthpercentage>1)	  {
							    $_POST['hsx']= $_POST['hsx']*$widthpercentage;
								
								
							   }
							    if($heightpercentage>1)	  {
							  		 $_POST['hsy']=$_POST['hsy']*$heightpercentage;
							
							   }
							$selectedfont=$_POST['cmbfontname'];
						   	$font=$relativepathtoserver."fonts/".$selectedfont;
							$fontcolor=substr($_POST['hfontcolor'],1 );
							//$hexcolor = str_split($fontcolor, 2); 
							$hexcolor[0]=substr($fontcolor,0,2);
							$hexcolor[1]=substr($fontcolor,2,2);
							$hexcolor[2]=substr($fontcolor,4,2);
							// Convert HEX values to DECIMAL
							$bincolor[0] = hexdec("0x{$hexcolor[0]}");
							$bincolor[1] = hexdec("0x{$hexcolor[1]}");
							$bincolor[2] = hexdec("0x{$hexcolor[2]}");
							$textcolor = @ImageColorAllocate($image, $bincolor[0], $bincolor[1], $bincolor[2]);
							$text1 = $_POST['htextval']; 
							// Here is our text
							if (get_magic_quotes_gpc()) {
							 $text1=stripslashes($text1);
    						}
							$fontsize=$_POST['cmbfontsize'];
							$angle=$_POST['textangle'];
							//echo "textcolor==".$textcolor;
							$im = imagecreatetruecolor(400, 30);
							//$grey = imagecolorallocate($im, 255, 0, 0);

							imagettftext($image, $fontsize,$angle, $_POST['hsx'], $_POST['hsy']+$fontsize, $textcolor, $font, $text1);
							//imagettftext($image, $fontsize,$angle, $_POST['hsx'], $_POST['hsy']+$fontsize, $grey, $font, $text1);
							$newfile = "../tmpeditimages/"."testsudeesh".$_SESSION['session_currentimage'];
							
							
							
							//echo
							
							
							if($sitetype=="new"){ 
							  $newfile = $relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$_SESSION['session_currentimage'];
							  $_SESSION['session_editedimage']=$newfile;
							  ReturnImagetype($image,$newfile,$_POST['hfile']);
							  @copy($relativepathtoserver."workarea/tempsites/$tmpsiteid/images/".$_SESSION['session_currentimage'],$relativepathtoserver."usergallery/$userid/images/".$_SESSION['session_currentimage']);
							 //water mark image
							   watermarkimage($newfile);
							}else{
							  $newfile = $relativepathtoserver."workarea/sites/$siteid/images/".$_SESSION['session_currentimage'];
							  $_SESSION['session_editedimage']=$newfile;
							  ReturnImagetype($image,$newfile,$_POST['hfile']);
							  @copy($relativepathtoserver."workarea/sites/$siteid/images/".$_SESSION['session_currentimage'],$relativepathtoserver."usergallery/$userid/images/".$_SESSION['session_currentimage']);
							}
							
	}
 
	 if($_POST['saveimage'] =="save"){
	         
			 $filename=$_SESSION['session_currentimage'];
			 $assignedname=$filename;
			 echo "<script>returnValue = new Object(); returnValue.name=\"".$assignedname."\"; window.close();</script>";
			
	  }		 
include "./includes/codeheader.php";
?>
	<script>
	 window.name="targ";
	function loading()
   {
		window.setTimeout("loadAfter()",200);
   }

function loadAfter() {
	if(document.readyState != "complete") {
		window.setTimeout("loadAfter()",200);
		return;
	}else{
	  loaddhtml();
	
	}
	
}

	</script>
</head>
<body >
<table width="100%" cellpadding=2 cellpadding=2 align=center>
   <tr>
      <td>
	     <FIELDSET>
							    <LEGEND class=maintextbigbold>Image Manager</LEGEND>
								<table width="97%" border="0">
								  <tr><td colspan=6>&nbsp;</td></tr>
								  <?php if($_SESSION['session_editedimage'] !=""){?>
								   <tr>
									   <td colspan=6 align=center><img src="<?php echo $_SESSION['session_editedimage']; ?>"></td>
									 </tr>
								  
								  <?php } ?>
								  <tr>
								      <td>
									  <?php
									     $ci->loadInterfaceText($_SESSION['session_templatebandlocation'],$_SESSION['currentimage']); 
									    
									  ?>
									  </td>
									  <td width="60%">
									     <table >
										        <tr><td>
												   <FIELDSET>
												    <legend class=maintextbold>Add your text</legend>
												    <table>
										                 <?php 
																																	 
															    $fontsize="<select name=cmbfontsize id=cmbfontsize class=selectbox>";
																for($i=10;$i<50;$i++){
																 $fontsize .="<option value=$i>$i</option>";
																}
																$fontname="<select name=cmbfontname id=cmbfontname class=selectbox>";
																//select the font from fonts directory
																
																$fontname .="<option value='times.ttf'>Times</option>";
																$fontname .="<option value='timesi.ttf'>Times Italic</option>";
																$fontname .="<option value='timesbd.ttf'>Times Bold</option>";
																$fontname .="<option value='timesbi.ttf'>Times Bold Italic</option>";
																$fontname .="<option value='arial.ttf'>Arial</option>";
																$fontname .="<option value='ariali.ttf'>Arial Italic</option>";
																$fontname .="<option value='arialbd.ttf'>Arial Bold</option>";
																$fontname .="<option value='arialbi.ttf'>Arial Bold Italic</option>";
																$fontname .="</select>";
															 
															 ?>
															<tr>
																	 <td colspan=2><b>Add Text Options</b></td>
															</tr>
															<tr>
	                                                          <td colspan=2 align="left" >.................</td>
	                                                       </tr>
														   <tr>
														   	<td colspan=2 align=left>
														  			'hold down either <br>the 'shift' or 'control'<br> button to resize the text area'
														 	</td>
														     </tr>
															<tr>
	                                                          		<td align="left" colspan=2>.....................................</td>
	                                                        </tr>
															<tr>
															 <td valign=top align=left>Font color</b></td>
															<td valign=top align=left>
															<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
																	function changecolor(currentid){
																	          
																			  
																	           winname="Easycreate";
																			   winurl="logochnagetextcolor.htm";
																			   window.open(winurl,winname,'width=200,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no,minimize=no');
																   } 
																   function setclrvalue(x){
																	   	 document.getElementById("bgtd").style.backgroundColor=x;
																	  	 document.getElementById("yourcol").value=x;
																  }
															</SCRIPT>
																<div id="main">
																<form name="picker">
																<table width="80%" align=center border=0 cellpadding=0 cellspacing=0 >
																  <tr>
																     <td ><input class=textbox id=bgtd size=5 readonly style="background-color:black">
																     <input class=button type=button value=".." onclick="changecolor(this.id);"></td>
															     </tr>
																<input type="hidden" name="yourcol" id="yourcol" size="7" id=yourcol value="#000000">
																
																
																
																
																</table>
																</form>
																</div>
													        <form name=frmtxtval target="targ" method=post action=imageeditor.php   onsubmit="return my_Submittext(this);"> 
															
															<tr>
															  <td align=left>Selct Font</td>
															  <td align=left><?php echo $fontname;?></td>
															</tr>
															<tr>
															  <td align=left>Font Size</td>
															  <td align=left><?php echo $fontsize;?></td>
															</tr>	
															<tr>
															<tr>
															 <td align=left>
															   Select Angle
															 </td>
															 <td  align=left>
															   <select name=textangle id=textangle class=selectbox>
															   <?php
															    for($i=0;$i<360;$i=$i+30){
																$angle="<option value=".$i.">$i</option>";
																echo $angle;
																}
																?>
																			
															   </select>
															 </td>
															</tr>
															<tr>
															  <td colspan=2>&nbsp;</td>
															</tr>
															<tr><td colspan=2 align=center>
															        <input type=hidden name=htextval id=htextval>
															        <input type=hidden name=hsx id=hsx>
																	<input type=hidden name=hsy id=hsy>
																	<input type=hidden name=hfile id=hfile>
																	<input type=hidden name=haction id=haction>
																	<input type=hidden name=hfontcolor id=hfontcolor>
	                                                    			<input name="Submit" class="button" type="submit" class="textbox" value="Add text" >
	                                                        </form>
															</tr>
															</table></FIELDSET>
															</td></tr>
														     <tr>
																	   <td align=center>
																	   
																	     OR
																	   </td>
																	</tr>
																	<tr>
																	   <td>
																	      <FIELDSET>
																		      <legend class=maintextbold>Upload your Image</legend>
																			    <table width="50%">
																				      <form  method="post" enctype="multipart/form-data"  action="./imageeditor.php" name="frmImageUpload" target="targ">
																					  <tr>
																					     <td class=errormessage colspan=2><?php echo $errormessagefileupload; ?></td>
																					  </tr>
																				      <tr>
																					     <td class=maintext>File</td>
																					     <td class=maintext>
																						   <input class=button type=file name=fileimage>
																						 </td>
																					  </tr>
																				      <tr>
																					    <td colspan=4 align=center><input id=btnuploadtype  name=btnuploadtype class=button  type=submit value="Upload" >
																							</td>
																					    </tr>
																					 
																					  </form>
																				  </table>
																			  
																			  
																		  </FIELDSET>
																	   
																	   </td>
																	
																	</tr>
																																	
									   </table>
									</td>
								  
								  </tr>
								  
								  
								   <tr><td colspan=2>&nbsp;</td></tr>
								   <tr><td colspan=2 align=center >
								         <form name=frmImageSave target="targ" method=post>
										   
									     <input type=submit name=saveimage value="save" class=button>		
										 
										 </form>
								   
								   </td></tr>
								 </table>  
								
							  </FIELDSET>       
	  </td>
   </tr>
  
		  
</table>

<?php 		
if($Invalidimage !="TRUE"){
  $ci->loadJavaScript();
}
?>	
			  	  
</body>
</html>

