An PHP based Image Database
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.2 KiB

  1. <?
  2. //
  3. // Copyright (c) 2013 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
  4. //
  5. // Permission to use, copy, modify, and/or distribute this software for any purpose
  6. // with or without fee is hereby granted, provided that the above copyright notice
  7. // and this permission notice appear in all copies.
  8. //
  9. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  10. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  11. // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  12. // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  14. // ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. //
  16. if ($_MGM['path'][1]=="process") {
  17. $file = (isset($_REQUEST['file']) ? $_REQUEST['file'] : "");
  18. $hash = pathinfo($file, PATHINFO_FILENAME);
  19. echo "Hash: ".$hash."<br />\n";
  20. $descriptorspec = array(
  21. 0 => array("pipe", "r"),
  22. 1 => array("pipe", "w"),
  23. 2 => array("pipe", "w")
  24. );
  25. $process = proc_open("./ocr \"".$file."\"", $descriptorspec, $pipes, getcwd());
  26. fclose($pipes[0]);
  27. $ocr = "";
  28. while (is_resource($process)) {
  29. $read = $pipes;
  30. $write = null;
  31. $except = null;
  32. $result = stream_select($read, $write, $except, 30);
  33. if ($result==0) {
  34. fclose($pipes[1]);
  35. fclose($pipes[2]);
  36. proc_terminate($process,9);
  37. break;
  38. } else if ($result>0) {
  39. $line = fread($pipes[1], 8192);
  40. if (strlen($line)==0) {
  41. fclose($pipes[1]);
  42. fclose($pipes[2]);
  43. proc_close($process);
  44. break;
  45. }
  46. $ocr .= $line;
  47. }
  48. }
  49. echo "OCR: ".$ocr."<br />\n";
  50. databaseQuery("UPDATE images SET ocr=%s WHERE hash=%s", $ocr, $hash);
  51. exit();
  52. }
  53. $files = glob("./data/*");
  54. require_once("header.php");
  55. ?>
  56. Processing...<br />
  57. <div id="result"></div>
  58. <script type="text/javascript">
  59. var files = new Array(<?
  60. $array = "";
  61. for ($i=0; $i<count($files); $i++) {
  62. //if (in_array(pathinfo($files[$i], PATHINFO_EXTENSION), $allowedExtensions)) {
  63. if ($array!="")
  64. $array .= ",";
  65. $array .= "\"".str_replace("\"", "\\\"", $files[$i])."\"";
  66. }
  67. echo $array;
  68. ?>);
  69. var i=0;
  70. function processFiles() {
  71. if (i<files.length) {
  72. var status = document.createElement("p");
  73. status.innerHTML = "Processing "+(i+1)+" of "+files.length+" files.";
  74. document.getElementById("result").appendChild(status);
  75. var request = new XMLHttpRequest;
  76. request.onreadystatechange = function() {
  77. if (request.readyState==4) {
  78. var status = document.createElement("p");
  79. status.innerHTML = request.responseText;
  80. document.getElementById("result").appendChild(status);
  81. processFiles();
  82. }
  83. }
  84. request.open("post", "<?=generateURL("re-ocr/process")?>", true);
  85. request.setRequestHeader("Cache-Control", "no-cache");
  86. request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  87. request.send("file="+encodeURIComponent(files[i]));
  88. i++;
  89. } else {
  90. var completed = document.createElement("p");
  91. completed.innerHTML = "Processing Completed.";
  92. document.getElementById("result").appendChild(completed);
  93. }
  94. }
  95. processFiles();
  96. </script>
  97. <?
  98. require_once("footer.php");
  99. exit();
  100. ?>