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

if ($_REQUEST["act"] == "getPicture") {
    $employee_id = $_REQUEST["id"];
    $photoname = $_REQUEST["photoname"];

    if ($photoname == "")
        $photoname = "default.gif";

    $imgfile = $EmployeeImagePath . $photoname;
    //$imgfile = $SiteImagePath .$photoname;

    if (!file_exists($imgfile))
        $imgfile = $SiteImagePath . "default.gif";

    echo "{success:true,data:{contactId:'" . $employee_id . "',file:'" . $imgfile . "'}}";
}

if ($_REQUEST["act"] == "setPicture") {
    $employee_id = $_REQUEST["id"];
    $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_" . $employee_id . ".jpg" ;
            $thumbimg_path = $EmployeeImagePath . $thumbimg;

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

            /*if (!is_dir($SchoolImagePath)) {
                            mkdir($SchoolImagePath, 0777);
                            chmod($SchoolImagePath, 0777);
                        }
                        */
            if (!is_dir($EmployeeImagePath)) {
                mkdir($EmployeeImagePath, 0777);
                chmod($EmployeeImagePath, 0777);
            }
            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:'" . $employee_id . "',file:'" . $thumbimg_path . "',photoname:'" . $thumbimg . "'}}";
}
?>