<?php
session_start();


// ضبط اعدادات اللغة العربية للصفحة 
header("content-type:text/html;charset=utf-8"); 
		
 		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]/";

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);  
 $allowed = array('pdf', 'png', 'jpg','jpeg'); 
 if (!in_array($extension, $allowed)) { 
 echo 'error'; 
 exit(); 
 } 

	$uniqid=uniqid();
	$fileName="$_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);
   
   /*
   if (isset($_SESSION["image_width"])){
	   $image->resizeToWidth($_SESSION["image_width"]); 
   }else{
	   $image->resizeToWidth(900);   
   }
  
  */
   
   $image->resizeToWidth(1200);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_1200.webp");
   $image->resizeToWidth(800);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_800.webp");
   //$image->resizeToWidth(800);
   //$image->save($targetDir."$_SESSION[page_id]_".$uniqid."_800_none.".$extension);
   $image->resizeToWidth(480);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_480.webp");
   //$image->resizeToWidth(375);
   //$image->save($targetDir."$_SESSION[page_id]_".$uniqid."_480.".$extension);
   //$image->resizeToWidth(100);
   //$image->save($targetDir."$_SESSION[page_id]_".$uniqid."_small.".$extension);
   $image->resize(50,50);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_tiny.webp");	
   //echo $_SESSION['watermark'];
   
   if( $_SESSION['watermark']==1){
	   include('water_mark_adv.php');
	   //WaterMark($targetFile);
	   WaterMark($targetDir."$_SESSION[page_id]_".$uniqid."_1200.webp");
	   WaterMark($targetDir."$_SESSION[page_id]_".$uniqid."_800.webp");
	   WaterMark($targetDir."$_SESSION[page_id]_".$uniqid."_480.webp");
   }
   
   }
   $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->resizeToWidth(1200);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_1200.".$extension);
   $image->resizeToWidth(800);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid.".".$extension);
   $image->resizeToWidth(800);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_800_none.".$extension);
   $image->resizeToWidth(480);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_480.".$extension);
   $image->resizeToWidth(375);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_480.".$extension);
   $image->resizeToWidth(50);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_small.".$extension);
   $image->resize(50,50);
   $image->save($targetDir."$_SESSION[page_id]_".$uniqid."_tiny.".$extension);
	
   //water_mark
   //echo $_SESSION['watermark'];
   if( $_SESSION['watermark']==1){
	   include('water_mark_adv.php');
	   WaterMark($targetFile);
	}
   //	
		
	}
	  	$ret[]= $fileName;

	  
	  }
	
	}
	//echo 'saad';
    echo json_encode($ret);
 }
 ?>