<?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_currenttempsiteid"];
$relativepathtoserver = "../";
/* This file will add user input text to image 'session_templatebandlocation' 
*  and save the image in workarea location  'session_editedimage'
*/ 
if ($_POST['btnuploadback'] == "Back") {
				header("location:imageeditor.php");
				exit;
} else if (isset($_POST['hfile']) != "" and ($_GET['action'] == "addtext" or $_POST['haction'] == "addtext")) {
                /* create image resourceid for the image to be edited
								   this function will check the image type and return the resourceid according to the type
								   ie imagegif function will be called for gif images
				*/
				$image = NewimageCreatefromtype($_POST['hfile']); 
				list($width, $height, $type, $attr) = @getimagesize($_POST['hfile']);
				$widthpercentage = $width / 450;
				$heightpercentage = $height / 450;
				/* if image width or height is greater than 450 we should scale x or y cordinate for the crop
				*   bcs we already resized the image that is greater than 700. '450' calculated by trial
				* 
				*/ 
				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); 
				$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']; 
				if (get_magic_quotes_gpc()) {
								$text1 = stripslashes($text1);
				} 
				$fontsize = $_POST['cmbfontsize'];
				$angle = $_POST['textangle']; 
				$im = imagecreatetruecolor(400, 30); 
				/* imagettftext ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text)
				*  $image->resoure image(ie image to be edit)
				*  $fontsize->font size
				*  $angle -> default  0
				* $_POST['hsx'] ->  starting xcordinate 
				* $_POST['hsy'] ->starting ycordinate
				* $textcolor ->font color
				* $font->font name
				* $text1 ->text to be added on image
				*/ 
				imagettftext($image, $fontsize, $angle, $_POST['hsx'], $_POST['hsy'] + $fontsize, $textcolor, $font, $text1); 
				if ($_SESSION['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']); 
								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']);
				} 
				header("location:imageeditor.php");
				exit;
} 
require_once './includes/class.cropinterface.php';
$ci = new cropInterface();
$currentloc = "../";
include "../includes/applicationheader.php";
?> 
<script>
	 window.name="targ";
</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>
								  
								     <tr>
									   <td align=center>
									   <?php
									     /* By calling this function template band will loaded with a text area layer.
										 *  and the default value in text area will be 'session_currentsamplename'
										 */ 
									     $ci->loadInterfaceText($_SESSION['session_templatebandlocation'],$_SESSION['session_currentsamplename']); 
									    
									  ?>
									   </td>
									   <td>
									             <FIELDSET>
												    
												    <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>";
																$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 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.php";
																			   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   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="Save" >
	                                                        </form>
															</tr>
															</table>
												</FIELDSET>
									   </td>
									   
									 </tr>
								  
								 
							    <tr><td colspan=2>&nbsp;</td></tr>
								<tr><td colspan=2 align=center >
							    <form name=frmImageback target="targ" method=post>
										   
								  <input id=btnuploadback  name=btnuploadback class=button  type=submit value="Back" >
										 
								</form>
								</td></tr>
								</table>   
								
							  </FIELDSET>  
							 
	  </td>
   </tr>
  
		  
</table>


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