<?php
	$dir = "images/saved/";
	if(!is_dir($dir)) {
	    mkdir($dir, 0777, true);
	}
    if(isset($_POST['data']))
    {
        $filename = preg_replace('/([^a-zа-я0-9_%]*)/iu', '', $_POST['filename']);
        $data = $_POST['data'];
        $data = str_replace(array("\n", "\r"), '', $data);
        file_put_contents($dir.$filename, $data);

        $img = str_replace(' ', '+', $_POST['img']);
        $img = base64_decode($img);
 
        require_once("lib.php"); 
        savePreview($img, $filename, 0.5, 0);
        savePreview($img, $filename, 1, "_big");                 
        echo "Сохранено";
    }
?>