<?php 


//استدعاء ملف تعديل مقاسات الصور
include('SimpleImage.php');
require('multi_upload/water_mark_adv.php');


 	function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
      return $ext;
	}
$content_split = preg_split('/(<img[^>]+\>)/i', $textarea_content, -1, PREG_SPLIT_DELIM_CAPTURE);

//print_r ( $content_split);

$img_arrange=0;

   /*الصورة الاساسية*/  
  $first_image=0;
for($i=0;$i<count($content_split);$i++){


if(substr($content_split[$i], 0, 4)=='<img'){
	
   /*الصورة الاساسية*/  
  $first_image=1;
	
	if(substr($content_split[$i], 0, 16)!='<img src="../img' && substr($content_split[$i], 0, 31)!='<img loading="lazy" src="../img' ){
		
		if(
   /*الصورة الاساسية*/  
  $first_image==1){
			$img_arrange=1;
		}

	$image_name= date("Ymd").uniqid();
	
	$src=explode('src="',$content_split[$i]);
	$src=explode('">',$src[1]);
	$src=explode(',',$src[0]);
	$src_type=$src[0];
	
	$src_data=explode('" data-filename="',$src[1]);
	$src_value=$src_data[0];
	$src_file_name=$src_data[1];
	
	//echo $src_type .' <br>';
	//echo $src_value .' <br>';
	//echo $src_file_name .' <br>';
	
	//echo "<img loading='lazy' src='$src_type,$src_value' data-filename='$src_file_name'>";


	// Define the Base64 value you need to save as an image


// Obtain the original content (usually binary data)
	$bin = base64_decode($src_value);

	

// Load GD resource from binary data
	$im = imageCreateFromString($bin);
	$percent=0.5;
    $width = imagesx($im);
    $height = imagesy($im);
    $newwidth = $width * $percent;
    $newheight = $height * $percent;

    $thumb = imagecreatetruecolor($newwidth, $newheight);

    // Resize
    imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Make sure that the GD library was able to load the image
// This is important, because you should not miss corrupted or unsupported images
if (!$im) {
  die('Base64 value is not a valid image');
}

// Specify the location where you want to save the image

$targetDir="../img/$table/";
//$targetDir=$src;
$extension='.webp';
$img_file = $targetDir.$image_name.$extension;
//echo($img_file);
// Save the GD resource as PNG in the best possible quality (no compression)
// This will strip any metadata or invalid contents (including, the PHP backdoor)
// To block any possible exploits, consider increasing the compression level

 imagewebp($im,$img_file,100);
 
	//create seo image
	 if($img_arrange==1){
	   $thump_img = new SimpleImage();
	   $thump_img->load($img_file); 
	   $thump_img->resizeToWidth(40);
	   $thump_img->save($targetDir.$img_num.$extension); 
	   $thump_img->save($targetDir.'img'.$img_num.'_tiny'.$extension); 
	 }
 
	if(isset($_POST['water_mark'])){


	   //echo "<img src='$img_file' />";
	   WaterMark($img_file);
	  
	   
	}
 
   $image = new SimpleImage();
   $image->load($img_file); 
 
   //echo($img_file);
   $image->resizeToWidth(1200);
  
   
 
 


$final_content.='<img loading="lazy" src="'.$img_file.'" />';


}else{
	
	$final_content.=$content_split[$i];
}

}else{
	
$final_content.=$content_split[$i];
	
}

}

?>