<?php
require("commonincludefiles.php");

if($_REQUEST["act"]=="getPicture")
{
	$contactId=$_REQUEST["id"];
	$photoname=$_REQUEST["photoname"];
	
	if($photoname=="")
		$photoname = "default.gif";
	
	$imgfile = $EmployeeImagePath .$photoname;
	
	if(!file_exists($imgfile))
		$imgfile = $SiteImagePath."default.gif";
	
	echo "{success:true,data:{contactId:'".$contactId."',file:'".$imgfile."'}}";	
}

if($_REQUEST["act"]=="setPicture")
{
	$contactId= time();
	$imgfile = $_FILES['newPic']['name'];
	$thumbimg_path = "";
	$thumbimg	   = "";
	
	if (isset($_FILES['newPic']['name']) && !empty($_FILES['newPic']['name'])) {
            if (strlen($_FILES['newPic']['name']) > 0) {
                $FileName = $_FILES['newPic']['name'];
                $Ext = substr($FileName, strrpos($FileName, ".")+1);
                $Ext = strtolower($Ext);
			
                $thumbimg = "employee_" . $contactId . "." . $Ext;
                $thumbimg_path = $EmployeeImagePath . $thumbimg;

                if (file_exists($thumbimg_path)) {
                    unlink($thumbimg_path);
                }

				if (!is_dir($SchoolImagePath)) {
                    mkdir($SchoolImagePath, 0755);
                    chmod($SchoolImagePath, 0755);
                }
				
                if (!is_dir($EmployeeImagePath)) {
                    mkdir($EmployeeImagePath, 0755);
                    chmod($EmployeeImagePath, 0755);
                }
                copy($_FILES['newPic']['tmp_name'], $thumbimg_path);

                $file_path=realpath($thumbimg_path);
                chmod($file_path, 0644);
				
				 //start resizing 
                   if (preg_match("/(jpg|gif|png|jpeg)/",$Ext))	{												
						$objResizeImage = new ResizeImageMgmt();
						$objResizeImage->resize($thumbimg_path,$SiteImageWidth,$SiteImageHeight,true,true);//name,w,h, blnfixheight
				 	}
				 //end resizing 			
				
            } else {
                $thumbimg = "default.gif";
            }            						
        }	
		echo "{success:true,data:{contactId:'".$contactId."',file:'".$thumbimg_path."',photoname:'".$thumbimg."'}}";
}
?>