<?php
session_start();

// ضبط اعدادات اللغة العربية للصفحة 
header("content-type:text/html;charset=utf-8"); 
if(!isset($_SESSION["page_id"]) || !intval($_SESSION["page_id"]) || empty($_SESSION["page_id"])){die();}
		
 		function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
include('../SimpleImage.php');
$targetDir = "../../img/$_SESSION[upload_folder]/icon/";
if(isset($_FILES["myfile"]))
{
	$ret = array();
	
//	This is for custom errors;	
/*	$custom_error= array();
	$custom_error['jquery-upload-file-error']="File already exists";
	echo json_encode($custom_error);
	die();
*/
	$error =$_FILES["myfile"]["error"];
	//You need to handle  both cases
	//If Any browser does not support serializing of multiple files using FormData() 
	$fileName = $_FILES['myfile']['name'];
	$extension = getExtension($fileName);
 	$extension = strtolower($extension);
	$uniqid=uniqid();
	$fileName="$_SESSION[image_category]_$_SESSION[page_id]_".$uniqid.".".$extension;
	if(!is_array($_FILES["myfile"]["name"])) //single file
	{
 	 
	
	$targetFile = $targetDir.$fileName;
	
   if(move_uploaded_file($_FILES["myfile"]["tmp_name"],$targetFile)){
   $image = new SimpleImage();
   $image->load($targetFile);
  
   
   
//تحجيم الصورة
$image->resizeToHeight(72);
   $image->save($targetDir."android/drawable-hdpi-icon.png");
  
   
   //water_mark
   if(isset($_SESSION['watermark'])){
   if($_SESSION['watermark']=="1"){	
   include('../water_mark_adv.php');
   WaterMark($targetFile);
   }}
   //
   
   
   }
   $ret[]= $fileName;
  
   
   
	}
	else  //Multiple files, file[]
	{
	  $fileCount = count($_FILES["myfile"]["name"]);
	  for($i=0; $i < $fileCount; $i++)
	  {
	  	$targetFile = $targetDir."$_SESSION[page_id]_".$uniqid.".".$extension;
		if(move_uploaded_file($_FILES["myfile"]["tmp_name"],$targetFile)){
   $image = new SimpleImage();
   $image->load($targetFile);
   
//تحجيم الصورة
$image->resizeToHeight(300);
   $image->save($targetDir."$_SESSION[image_category]_$_SESSION[page_id]_".$uniqid."_small.".$extension);
   $image->resize(20,20);
   $image->save($targetDir."$_SESSION[image_category]_$_SESSION[page_id]_".$uniqid."_tiny.".$extension);	
	
   //water_mark
   if(isset($_SESSION['watermark'])){
   if($_SESSION['watermark']=="1"){	
   include('../water_mark_adv.php');
   WaterMark($targetFile);
   }}
   //	
		
	}
	  	$ret[]= $fileName;
	  
	  	
	
	

	  
	  
	  }
	
	}
    echo json_encode($ret);
 }
 ?>