

<?php
ini_set('max_execution_time', 300);


$inp = file_get_contents('containts.json',true);
$tempArray = json_decode($inp)->sticker_packs;

$keys = array_keys($tempArray );
$last_key = array_pop($keys);

$guid = $tempArray[$last_key]->identifier +1;
$Identifire=$guid;

function resize_image($im, $dst_width, $dst_height) {
     $width = imagesx($im);
    $height = imagesy($im);

    $newImg = imagecreatetruecolor($dst_width, $dst_height);

    imagealphablending($newImg, false);
    imagesavealpha($newImg, true);
    $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
    imagefilledrectangle($newImg, 0, 0, $width, $height, $transparent);
    imagecopyresampled($newImg, $im, 0, 0, 0, 0, $dst_width, $dst_height, $width, $height);

    return $newImg;
}

 if(count($_POST['upload64']) > 0)
 {
	 
	 if (!file_exists($guid )) {
			mkdir($guid, 0777, true);
			//mkdir($guid, 0777, true);
			mkdir($guid ."_png", 0777, true);
			mkdir($guid ."_small", 0777, true);
		}
	
	for($i=0;$i< count($_POST['upload64']) ; $i++)
	{
		
		$_i=$i + 1 ;
		$filePath = $guid ."_png/". sprintf("%02d", $_i) .".png";

		copy("temp/".$_POST['upload64'][$i].".png",$filePath);
		
		$files_[] = $guid. "/". sprintf("%02d", $_i).'.webp';
					
					$img = @imagecreatefrompng( $filePath);
				    imagewebp($img, $guid. "/". sprintf("%02d", $_i).'.webp');
					
					$img = resize_image($img, 100, 100);
					imagewebp($img, $guid. "_small/". sprintf("%02d", $_i).'.webp');
				    imagedestroy($img);
	}
	
	$rootPath = realpath($guid);
		$zip = new ZipArchive();
		$zip->open($guid. '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

		$files = new RecursiveIteratorIterator(
			new RecursiveDirectoryIterator($rootPath),
			RecursiveIteratorIterator::LEAVES_ONLY
		);

		foreach ($files as $name => $file)
		{
			if (!$file->isDir())
			{
				$filePath = $file->getRealPath();
				$relativePath = substr($filePath, strlen($rootPath) + 1);
				$zip->addEmptyDir($guid);
				$zip->addFile($filePath, $guid ."\\". $relativePath);
				
			}
		}
		$zip->close();
		
	$existingPacks=json_decode($inp);

	$newObject = new stdClass;

	$newObject->identifier = "". $Identifire ."";
	$newObject->name = $_POST['StickerPackName'];
	$newObject->publisher = $_POST['publisher'];
	$newObject->tray_image_file = "01.webp";
	$newObject->publisher_email = $_POST['publisher_email'];
	$newObject->publisher_website = $_POST['publisher_website'];
	$newObject->privacy_policy_website = "";
	$newObject->license_agreement_website = "";
	$newObject->stickers =  array();
	
	if ($handle = opendir('./'.$Identifire )) {
    while (false !== ($entry = readdir($handle))) {


        if ($entry != "." && $entry != "..") {

			$sticker=new stdClass();
			

			$iparr = explode (".",$entry);
			$sticker->image_file=$iparr[0];
			$sticker->emojis=array();
			
			array_push($newObject->stickers,$sticker);

            //echo $iparr[0];
        }
    }

		closedir($handle);
	}

	$packs=json_decode($inp)->sticker_packs;
	
	array_push($packs,$newObject);
	
	$existingPacks->sticker_packs=$packs;
	//echo json_encode($existingPacks);
	
	file_put_contents('containts.json', json_encode($existingPacks));
	
	echo "<h1>Uploaded:</h1>";    
    if(is_array($files_)){
        echo "<ul>";
        foreach($files_ as $file){
            //echo "<li>$file</li>";
        }
        echo "</ul>";
    }
		
 }





?>