<?

function image_getfilename($pimagepkey) {
  global $db;
  if ($pimagepkey==NULL) { $pimagepkey=""; }
  $filename="";
  $myquery="Select imagefilename from general_images where imagepkey='".$pimagepkey."'"; 
  $myresult =& $db->query($myquery);
  $mycount=$myresult->numRows();
  if ($mycount!=0) {
    while($myrow=$myresult->fetchRow()) {
      $filename=$myrow['imagefilename'];
    }
  } else {
  }
  return $filename;
}

function image_resizeimage($pimagefile,$halfcut) {
  $imagedir = 'images/';
  if (($pimagefile!=NULL)&&($pimagefile!="")) {
    $pinfo = getimagesize($imagedir.$pimagefile);
    $pwidth = $pinfo[0];
    $pheight = $pinfo[1];
    if ($halfcut==false) {
      if ($pwidth>($pheight*3)) {
        // banner..
        $realwidth=590;
        $realheight=intval((590/$pwidth)*$pheight);
      } else {
        // other..
        $realwidth=200;
        $realheight=intval((200/$pwidth)*$pheight);
      }
    } else {
      // force to half-size..
      $realwidth=intval($pwidth/2);
      $realheight=intval($pheight/2);
    }
    return "width=".$realwidth." height=".$realheight;
  } else {
    return "";
  }
}

function image_uploadimage($puserfile,$ptempfile,$ptitle,$pdescription) {
  // note: $puserfile for $_FILES['<inputname>']['name']
  //       $ptempfile for $_FILES['<inputname>']['tmp_name']
  global $db;
  $uploaddir = 'images/';
  if (($puserfile!=NULL)&&($puserfile!="")) {
    $mystamp = gettimestamp();
    // get filenames and extension..
    $wordarray = explode("/",$puserfile);
    $lastword = count($wordarray) - 1;
    $originalname = $wordarray[$lastword];
    $namearray = explode(".",$originalname);
    $lastname = count($namearray) - 1;
    $fileext = $namearray[$lastname];
    $hashedname = md5(microtime());
    $newfilename = $hashedname.".".$fileext;
    $uploadfile = $uploaddir.$newfilename;
    // upload (lib_general)..
    $tmpuploadfile = $ptempfile;
    $resultupload = uploadFile($tmpuploadfile, $uploadfile);
    if($resultupload==TRUE) {
      $sql_insert = "insert into general_images (imagepkey, imagefilename, imageoriginalname, imagetitle, imagedescription, imageuploaddate, imageuploadedby) values ('".$hashedname."','".$newfilename."','".$originalname."','".$ptitle."','".$pdescription."','".$mystamp."','".$_SESSION['Xuserpkey']."')";
      $result_insert =& $db->query($sql_insert);
      return $hashedname;
    } else {
      return "";
    }
  } else {
    return "";
  }
}

/*
function image_uploadphoto($puserfile) {
	$uploaddir = 'photo/';
	if ($_FILES['userfile']['name']!=NULL) 
	{
		$tbl_compPro=$tbl_employee;
		$column_compPro="employeephotograph";
		$condition_compPro="where employeePkey = '".$epk."'";
	
		$sql_compPro="select $column_compPro from $tbl_compPro $condition_compPro"; 
		$result_compPro =& $db->query($sql_compPro);
	
		$data_compPro=$result_compPro->fetchRow();
		$filename = $data_compPro['employeephotograph'];	
			if ($data_compPro['employeephotograph'] !=NULL)
			{
				delUploadFile($uploaddir,$filename); 
			}
				
		//$filename3=explode(".",$_FILES['userfile']['name']);
		// avoid special character
		$filename3 = str_replace(".", "",$_FILES['userfile']['name']);
		$filename3 = str_replace("%", "",$filename3);
		$filename3 = str_replace(" ", "",$filename3);
		$filename3 = str_replace("!", "",$filename3);
		$filename3 = str_replace("@", "",$filename3);
		$filename3 = str_replace("#", "",$filename3);
		$filename3 = str_replace("$", "",$filename3);
		$filename3 = str_replace("^", "",$filename3);
		$filename3 = str_replace("&", "",$filename3);
		$filename3 = str_replace("*", "",$filename3);
		$filename3 = str_replace("(", "",$filename3);
		$filename3 = str_replace(")", "",$filename3);
		//$filename4=$epk.$filename3[0].".jpg";
		$filename4=$epk.$filename3.".jpg";
	
		$uploadfile = $uploaddir .$filename4;
		$tmpuploadfile = $_FILES['userfile']['tmp_name'];
		$resultupload = uploadFile($tmpuploadfile, $uploadfile);
		
		if($resultupload==TRUE) 
		{
		$column_compPro2="employeephotograph ='".$filename4."'";
		$sql_update = "update $tbl_employee set $column_compPro2 $condition_compPro";
		$result_compPro2 =& $db->query($sql_update);
		
		$msg = msg("Photo successfully uploaded");
		echo $msg; 
		} else {
		$msg = msg("File upload failed .\n");
		echo $msg;
		}
		
		$page = "empviewphoto.php?ep=".$epk."&cp=".$cpk;
		  $goto = goto($page);
          print $goto;
	}
	
	
}
*/

?>