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.

253 lines
8.1 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]=="complete") {
  17. if ($_MGM['path'][2]=="process") {
  18. $file = (isset($_REQUEST['file']) ? $_REQUEST['file'] : "");
  19. $filename = pathinfo($file, PATHINFO_FILENAME);
  20. $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  21. echo "Processing ".basename($file)."<br />\n";
  22. if (!file_exists($file)) {
  23. echo "Error: <span style=\"color: #ff0000;\">File does not exist.</span>";
  24. exit();
  25. }
  26. $allowedExtensions = array("png", "jpg", "jpeg", "gif", "tif", "tiff", "bmp");
  27. if (!in_array($extension, $allowedExtensions)) {
  28. echo "Error: <span style=\"color: #ff0000;\">Extension is not allowed.</span>";
  29. unlink($file);
  30. exit();
  31. }
  32. $fileSize = filesize($file);
  33. echo "Size: ".$fileSize."<br />\n";
  34. list($width, $height, $type, $attr) = getimagesize($file);
  35. if (!isset($width) || !isset($height)) {
  36. echo "Error: <span style=\"color: #ff0000;\">Cannot read image.</span>";
  37. unlink($file);
  38. exit();
  39. }
  40. echo "Width: ".$width." Height: ".$height."<br />\n";
  41. $hash = md5_file($file);
  42. if ($hash==NULL) {
  43. echo "Error: <span style=\"color: #ff0000;\">Unable to hash image.</span>";
  44. unlink($file);
  45. exit();
  46. }
  47. echo "Hash: ".$hash."<br />\n";
  48. $newFile = "./data/".$hash.".".$extension;
  49. if (file_exists($newFile)) {
  50. echo "Error: <span style=\"color: #ff0000;\">File already uploaded.</span>";
  51. unlink($file);
  52. exit();
  53. }
  54. $ocr = "";
  55. if (file_exists("./ocr")) {
  56. $descriptorspec = array(
  57. 0 => array("pipe", "r"),
  58. 1 => array("pipe", "w"),
  59. 2 => array("pipe", "w")
  60. );
  61. $process = proc_open("./ocr \"".$file."\"", $descriptorspec, $pipes, getcwd());
  62. fclose($pipes[0]);
  63. while (is_resource($process)) {
  64. $read = $pipes;
  65. $write = null;
  66. $except = null;
  67. $result = stream_select($read, $write, $except, 30);
  68. if ($result==0) {
  69. fclose($pipes[1]);
  70. fclose($pipes[2]);
  71. proc_terminate($process,9);
  72. break;
  73. } else if ($result>0) {
  74. $line = fread($pipes[1], 8192);
  75. if (strlen($line)==0) {
  76. fclose($pipes[1]);
  77. fclose($pipes[2]);
  78. proc_close($process);
  79. break;
  80. }
  81. $ocr .= $line;
  82. }
  83. }
  84. echo "OCR: ".htmlspecialchars($ocr, ENT_COMPAT | ENT_HTML401, 'UTF-8', true)."<br />\n";
  85. }
  86. $tags = array();
  87. $external_data = "";
  88. $plugins = glob("./external_data_plugins/*.php");
  89. for ($pluginIndex=0; $pluginIndex<count($plugins); $pluginIndex++) {
  90. require($plugins[$pluginIndex]);
  91. }
  92. echo "External Data: ".htmlspecialchars($external_data, ENT_COMPAT | ENT_HTML401, 'UTF-8', true)."<br />\n";
  93. $thumbFile = "./thumbs/".$hash.".".$extension;
  94. $target = 150;
  95. $newWidth = $width;
  96. $newHeight = $height;
  97. if ($width>$target || $height>$target) {
  98. $widthFactor = $target/$width;
  99. $heightFactor = $target/$height;
  100. $scaleFactor = 1;
  101. if ($widthFactor<$heightFactor)
  102. $scaleFactor = $widthFactor;
  103. else
  104. $scaleFactor = $heightFactor;
  105. $newWidth = round($width*$scaleFactor);
  106. $newHeight = round($height*$scaleFactor);
  107. }
  108. if ($type==IMAGETYPE_GIF) {
  109. $tmp = "./thumbs/coalesce".rand().".gif";
  110. system($_MGM['imagemagick']."convert \"".$file."\" -coalesce \"".$tmp."\"");
  111. system($_MGM['imagemagick']."convert -size ".$width."x".$height." \"".$tmp."\" -resize ".$newWidth."x".$newHeight." \"".$thumbFile."\"");
  112. unlink($tmp);
  113. } else {
  114. system($_MGM['imagemagick']."convert -size ".$width."x".$height." \"".$file."\" -resize ".$newWidth."x".$newHeight." \"".$thumbFile."\"");
  115. }
  116. chmod($thumbFile, 0666);
  117. echo "Saved thumbnail.<br />\n";
  118. rename($file, $newFile);
  119. echo "Moved Original.<br />\n";
  120. databaseQuery("INSERT INTO images (user_id,hash,extension,name,file_size,width,height,thumb_file_size,thumb_width,thumb_height,tags,external_data,ocr,time) VALUES (%s,%s,%s,%s,%d,%d,%d,%d,%d,%d,%s,%s,%s,%d)", $_MGM['user']['docid'], $hash, $extension, $filename, $fileSize, $width, $height, filesize($thumbFile), $newWidth, $newHeight, implode(" ", $tags), $external_data, $ocr, filemtime($newFile));
  121. echo "Complete.<br />\n";
  122. exit();
  123. }
  124. $files = glob("./load/*");
  125. require_once("header.php");
  126. ?>
  127. Processing...<br />
  128. <div id="result"></div>
  129. <script type="text/javascript">
  130. var files = new Array(
  131. <?
  132. $array = "";
  133. for ($i=0; $i<count($files); $i++) {
  134. //if (in_array(pathinfo($files[$i], PATHINFO_EXTENSION), $allowedExtensions)) {
  135. if ($array!="")
  136. $array .= ",\n\t\t";
  137. $array .= "\"".str_replace("\"", "\\\"", $files[$i])."\"";
  138. }
  139. echo $array;
  140. ?>
  141. );
  142. var i=0;
  143. function processFiles() {
  144. if (i<files.length) {
  145. var status = document.createElement("p");
  146. status.innerHTML = "Processing "+(i+1)+" of "+files.length+" files.";
  147. document.getElementById("result").appendChild(status);
  148. var request = new XMLHttpRequest;
  149. request.onreadystatechange = function() {
  150. if (request.readyState==4) {
  151. var status = document.createElement("p");
  152. status.innerHTML = request.responseText;
  153. document.getElementById("result").appendChild(status);
  154. processFiles();
  155. }
  156. }
  157. request.open("post", "<?=generateURL("upload/complete/process")?>", true);
  158. request.setRequestHeader("Cache-Control", "no-cache");
  159. request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  160. request.send("file="+encodeURIComponent(files[i]));
  161. i++;
  162. } else {
  163. var completed = document.createElement("p");
  164. completed.innerHTML = "Processing Completed.";
  165. document.getElementById("result").appendChild(completed);
  166. }
  167. }
  168. processFiles();
  169. </script>
  170. <?
  171. require_once("footer.php");
  172. exit();
  173. }
  174. $file = $_SERVER['HTTP_X_FILENAME'];
  175. if (isset($file)) {
  176. $input = fopen("php://input", "r");
  177. if (file_exists("./load/".$file))
  178. unlink("./load/".$file);
  179. $output = fopen("./load/".$file, "w");
  180. while ($data = fread($input, 1024))
  181. fwrite($output, $data);
  182. fclose($output);
  183. fclose($input);
  184. echo "uploaded";
  185. exit();
  186. }
  187. require_once("header.php");
  188. ?>
  189. <script type="text/javascript">
  190. function upload() {
  191. var files = document.getElementById("files").files;
  192. document.getElementById("files").setAttribute("disabled", "true");
  193. document.getElementById("uploadButton").setAttribute("disabled", "true");
  194. var i=0;
  195. function setupFile() {
  196. if (i<files.length) {
  197. document.getElementById("progress").innerHTML = "Uploading "+(i+1)+" of "+files.length+" files.";
  198. var file = files[i];
  199. if (file.name==undefined) {
  200. document.getElementById("progress").innerHTML = "Error: Browser unsupported.";
  201. return;
  202. }
  203. var request = new XMLHttpRequest;
  204. request.onreadystatechange = function() {
  205. if (request.readyState==4)
  206. setupFile();
  207. }
  208. request.open("post", "<?=generateURL("upload")?>", true);
  209. request.setRequestHeader("Cache-Control", "no-cache");
  210. request.setRequestHeader("X-FILENAME", file.name);
  211. request.setRequestHeader("Content-Type", "multipart/form-data");
  212. request.send(file);
  213. i++;
  214. } else {
  215. document.getElementById("progress").innerHTML = "Upload Complete.";
  216. document.getElementById("files").removeAttribute("disabled");
  217. document.getElementById("uploadButton").removeAttribute("disabled");
  218. document.getElementById("files").form.reset();
  219. window.location = "<?=generateURL("upload/complete")?>";
  220. }
  221. }
  222. setupFile();
  223. }
  224. </script>
  225. <form>
  226. <input type="file" multiple="true" id="files" />
  227. <input type="button" id="uploadButton" onclick="upload()" value="Upload" class="btn" />
  228. </form>
  229. <div id="progress"></div>
  230. <?
  231. require_once("footer.php");
  232. exit();
  233. ?>