<?php 
/**
 *		Grab frames from the demo and save them to disk to create a video with. Run this and the demo from a local webserver.
 *		See the GRABBER_MODE and other variables at the start of demo.html how it can post to this php file. 
 */

die('disabled by default, for security reasons.');

define ( 'SAVEDIR', 'framegrabber' );
define ( 'FILEPREPAD', 'ex' );
define ( 'FILENUMNUMS', 8 );

if ( ! isset($_POST['frame']) ) trigger_error('Frame not posted');
if ( ! isset($_POST['data']) ) trigger_error('Data not posted');
$frame = $_POST['frame'];
if ( (string)(int)$frame !== $frame ) trigger_error('Posted frame not legal');
$data = $_POST['data'] ;

$data = substr($data,strlen('data:image/png;base64,'));
$data = str_replace(' ','+',$data);
$png = base64_decode($data);

if ( !file_exists(SAVEDIR) ) mkdir(SAVEDIR);
if ( !file_exists(SAVEDIR) ) trigger_error('Could not create directory '.SAVEDIR, E_USER_ERROR); 
file_put_contents(SAVEDIR.'/'.FILEPREPAD.str_pad($frame,FILENUMNUMS,'0',STR_PAD_LEFT).'.png',$png);

