<?php

//Generate a folder if it doesn't exist
if (!file_exists('user_images/'))
{
    mkdir('user_images', 0755, true);
}

//Get submitted data by user
	if (isset($_POST['CanvasPic'])) {
	$img1 = $_POST['CanvasPic'];
	} else {
	$img1 = '';
	}
	
		if (isset($_POST['name'])) {
	$name = $_POST['name'];
	} else {
	$name = '';
	}
	
define('UPLOAD_DIR', 'user_images/');
//define('UPLOAD_DIR', '/');

//Remove some useless string before to create image properly
//Decode base64 then generate new name
	if ($img1) { 
	$img1 = str_replace('data:image/jpeg;base64,', '', $img1);
	$img1 = str_replace(' ', '+', $img1);
	$data1 = base64_decode($img1);
	$file1 = UPLOAD_DIR . $name . "_" . uniqid() . '.jpeg';
	$success = file_put_contents($file1, $data1);
	}
	
//Output a URL where is saved.
$locator = $file1;


//Display a text, otherwise if error happened.
print $locator ? $file1 : 'Could not save the file!';


?>