//
//  Copyright (c) 2013 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
//
//  Permission to use, copy, modify, and/or distribute this software for any purpose
//  with or without fee is hereby granted, provided that the above copyright notice
//  and this permission notice appear in all copies.
//
//  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
//  REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
//  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
//  OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
//  DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
//  ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
if ($_MGM['path'][1]=="process") {
	$file = (isset($_REQUEST['file']) ? $_REQUEST['file'] : "");
	$hash = pathinfo($file, PATHINFO_FILENAME);
	
	echo "Hash: ".$hash."
\n";
	
	$descriptorspec = array(
		0 => array("pipe", "r"),
		1 => array("pipe", "w"),
		2 => array("pipe", "w")
	);
	$process = proc_open("./ocr \"".$file."\"", $descriptorspec, $pipes, getcwd());
	
	fclose($pipes[0]);
	$ocr = "";
	while (is_resource($process)) {
		$read = $pipes;
		$write = null;
		$except = null;
		$result = stream_select($read, $write, $except, 30);
		
		if ($result==0) {
			fclose($pipes[1]);
			fclose($pipes[2]);
			proc_terminate($process,9);
			break;
		} else if ($result>0) {
			$line = fread($pipes[1], 8192);
			if (strlen($line)==0) {
				fclose($pipes[1]);
				fclose($pipes[2]);
				proc_close($process);
				break;
			}
			$ocr .= $line;
		}
	}	
	echo "OCR: ".$ocr."
\n";
	
	databaseQuery("UPDATE images SET ocr=%s WHERE hash=%s", $ocr, $hash);
	exit();
}
$files = glob("./data/*");
require_once("header.php");
?>
Processing...