



<?php


include '../config.php';
if($__testMode==true)
{
	return true;
}

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;
}


?>

<?php



ini_set('max_execution_time', 300);
	$inp = file_get_contents('userdata/containts.json',true);
	$tempArray = json_decode($inp)->sticker_packs;
	
	
$keys = array_keys($tempArray );
$last_key = array_pop($keys);
$ActualPresentStickers=0;

$guid = "userdata/". $_POST['identifier'];

$targetAPach=null;

foreach( $tempArray as $currentPack )
{
	if( $currentPack->identifier == $_POST['identifier'] )
	{
		$ActualPresentStickers=count($currentPack->stickers);
		echo $ActualPresentStickers;

		if( isset($_POST['removed']) && count($_POST['removed']) > 0)
		{
			// $currentPack->stickers

			for ($i=0; $i < count($_POST['removed']) ; $i++) 
			{

				$path="userdata/".$currentPack->identifier."/". $currentPack->stickers[$_POST['removed'][$i]]->image_file .".webp";
				unlink($path);

				$path="userdata/".$currentPack->identifier."_small/". $currentPack->stickers[$_POST['removed'][$i]]->image_file .".webp";
				unlink($path);
				$path="userdata/".$currentPack->identifier."_png/". $currentPack->stickers[$_POST['removed'][$i]]->image_file .".webp";
				unlink($path);

				unset($currentPack->stickers[$_POST['removed'][$i]]); 
				
				//$currentRemovableIndex=	$_POST['removed'][$i];
				//foreach ($currentPack->stickers as $key => $value) 
				//{
				//}
			}
		}

		if( isset($_POST['upload64']) && count($_POST['upload64']) > 0)
		{
			for($i=0;$i< count($_POST['upload64']) ; $i++)
			{
				$_i=$i + 1 + $ActualPresentStickers ;
				$filePath = $guid ."_png/". sprintf("%02d", $_i  ) .".png";
					
				copy("userdata/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);
							
							
							
				$sticker=new stdClass();
				//$iparr = explode (".",$entry);
				$sticker->image_file=sprintf("%02d", $_i  );
				$sticker->emojis=array();
				
				array_push($currentPack->stickers,$sticker);
			}
		}

		$rootPath = realpath($guid);
			$zip = new ZipArchive();
			$zip->open($guid. '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

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

			if ($handle = opendir('./'.$guid )){
				foreach ($files as $name => $file)
				{
					if (!$file->isDir())
					{
						$filePath = $file->getRealPath();
						$relativePath = substr($filePath, strlen($rootPath) + 1);
						$zip->addEmptyDir($guid);
						$zip->addFile($filePath, $_POST['identifier'] ."\\". $relativePath);
						
					}
				}	
			}
			closedir($handle);
			$zip->close();
		$existingPacks=json_decode($inp);
		$packs=json_decode($inp)->sticker_packs;
		//array_push($packs,$newObject);
		$existingPacks->sticker_packs=$tempArray;
		
		file_put_contents('userdata/containts.json', json_encode($existingPacks));
		//$existingPacks->sticker_packs=$packs;
		
		//echo "sdf";
		//echo $packs;
		//file_put_contents('containts.json', json_encode($tempArray));
		//
		
	}
}

// echo  json_encode($tempArray);
?>