imageReplacement.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <html>
  2. <head>
  3. </head>
  4. <body style="margin: 0px">
  5. <?php
  6. include 'config.php';
  7. ini_set('display_errors', 1);
  8. ini_set('display_startup_errors', 1);
  9. error_reporting(E_ALL);
  10. //http://www.justin-cook.com/2006/03/31/php-parse-a-string-between-two-strings/
  11. function get_string_between($string, $start, $end){
  12. $string = " ".$string;
  13. $ini = strpos($string,$start);
  14. if ($ini == 0) return "";
  15. $ini += strlen($start);
  16. $len = strpos($string,$end,$ini) - $ini;
  17. return substr($string,$ini,$len);
  18. }
  19. function startsWith ($string, $startString)
  20. {
  21. $len = strlen($startString);
  22. return (substr($string, 0, $len) === $startString);
  23. }
  24. //header('Content-type: image/svg+xml'); //does not play video
  25. $svg_file = file_get_contents(dirname(__DIR__, 1) . "/images/uploads/" . $_GET["___image___"]);
  26. $doc1 = new DOMDocument;
  27. $doc1->loadXML($svg_file);
  28. $xpath1 = new DOMXPath($doc1);
  29. $xpath1->registerNamespace('svg', 'http://www.w3.org/2000/svg');
  30. //prüfen ob exteranlImage dann wird dieses verwendet
  31. if(strlen($svg_file) < 400){
  32. $url = $xpath1->query("/svg:svg/svg:image/@href");
  33. $svg_file = file_get_contents($url[0]->nodeValue);
  34. }
  35. $doc = new DOMDocument;
  36. $doc->loadXML($svg_file);
  37. $xpath = new DOMXPath($doc);
  38. $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');
  39. $root = $xpath->query("/*");
  40. $root[0]->setAttribute("viewBox", "0 0 1280 720");
  41. $widthq = $xpath->query("/*/@width");
  42. $heightq = $xpath->query("/*/@height");
  43. $root = $xpath->query("/*");
  44. $root[0]->setAttribute("viewBox", "0 0 ". $widthq[0]->nodeValue . " " . $heightq[0]->nodeValue);
  45. $widthq[0]->nodeValue = "100%";
  46. $heightq[0]->nodeValue = "100%";
  47. //Get all Text Nodes
  48. $style = $xpath->query('//svg:text');
  49. $keyValueStore=array();
  50. $keyValueStore2=array();
  51. //Get all rect nodes before text nodes and store them in key value store to access them for manipulation
  52. foreach ($style as $result) {
  53. $style2 = $xpath->query('./preceding-sibling::*[1]', $result);
  54. //echo $style2[0]->nodeName;
  55. //echo $result->nodeValue . " ";
  56. //get Rect
  57. foreach ($style2 as $result3) {
  58. //echo $result->nodeValue;
  59. //echo $result3->getAttribute('transform') . "<br>";
  60. $keyValueStore[$result->nodeValue] = $result3;
  61. $keyValueStore2[$result->nodeValue] = $result;
  62. }
  63. }
  64. $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://") . $_SERVER['HTTP_HOST']. dirname($_SERVER['PHP_SELF']) . "/TransformProductCode.php" . "?ProductCode=" . $_GET["ProductCode"];
  65. $json = file_get_contents($url);
  66. $asocarray = json_decode($json, true);
  67. $curstation = $_GET["Station"];
  68. //Einzelbilder vom Universal Storage laden
  69. foreach ($keyValueStore as $key => $value) {
  70. $keyorig = $key;
  71. $key = substr($key, 1);
  72. $keyarray = str_split($key, 2);
  73. if (startsWith($key, 'URL')) {
  74. echo "<iframe src='" . substr($key, 4) . "'style='position: absolute; left: " . $value->getAttribute('x')/12.80 ."%; top: " . $value->getAttribute('y')/7.20 ."%; width: " . $value->getAttribute('width')/12.80 . "%; height: " . $value->getAttribute('height')/7.20 . "%;'>";
  75. echo "</iframe>";
  76. }
  77. $fullfilename = "";
  78. $textentry = "";
  79. foreach($keyarray as $keypart){
  80. //only if all keyparts (Pb Sy Pk Of) can be found within the asociative array (Pb -> E, Sy -> X) the file can be linked otherwise there is a problem.
  81. if($keypart == "ID"){
  82. $fullfilename = $fullfilename . "_ID;" . substr($keyorig, strrpos($keyorig, "ID:") + 3);
  83. $textentry = substr($keyorig, strrpos($keyorig, "ID:") + 3);
  84. break;
  85. }
  86. else if(array_key_exists ($keypart, $asocarray)){
  87. $fullfilename = $fullfilename . "_" . $keypart . ";" . $asocarray[$keypart];
  88. }
  89. else{
  90. continue 2;
  91. }
  92. }
  93. $fullfilename = substr($fullfilename, 1);
  94. $fullfilename = $universalStorageEinzelbilder . "Station" . $curstation . "/Station". $curstation. "_" . $fullfilename . ".jpg";
  95. //if Real image was not found
  96. $file_headers = @get_headers($fullfilename);
  97. if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
  98. //echo $fullfilename;
  99. //echo $key . " ";
  100. //echo json_encode($asocarray);
  101. $onlyUsedParts=array();
  102. foreach($asocarray as $keypart => $keyval){
  103. if(in_array($keypart, $keyarray)){
  104. array_push($onlyUsedParts, $keyval);
  105. }
  106. else{
  107. array_push($onlyUsedParts, "");
  108. }
  109. }
  110. //echo implode (".", $onlyUsedParts);
  111. //echo "<br>";
  112. $fullfilename = $wasUrl . "server/assignments/search?pattern=". implode (".", $onlyUsedParts) ."&station=". $curstation ."&Text=" . $textentry;
  113. $json = json_decode(file_get_contents($fullfilename));
  114. if (isset($json[0])) {
  115. $fullfilename = $json[0];
  116. }
  117. else{
  118. continue;
  119. }
  120. }
  121. else{
  122. //echo $fullfilename;
  123. }
  124. //echo $fullfilename . "<br>";
  125. $element3 = $doc->createElement('pattern');
  126. $element3->setAttribute("id", $fullfilename);
  127. $element3->setAttribute("x", "0");
  128. $element3->setAttribute("y", "0");
  129. $element3->setAttribute("width", "1");
  130. $element3->setAttribute("height", "1");
  131. $element2 = $doc->createElement('image');
  132. $element2->setAttribute("x", "0");
  133. $element2->setAttribute("y", "0");
  134. $element2->setAttribute("width", $value->getAttribute('width'));
  135. $element2->setAttribute("height", $value->getAttribute('height'));
  136. $element2->setAttribute("xlink:href", $fullfilename);
  137. $element3->appendChild($element2);
  138. $parent = $doc->getElementsByTagName('defs')->item(0);
  139. $parent->appendChild($element3);
  140. $value->setAttribute("fill", "url(#". $fullfilename . ")");
  141. //override identifier to be empty
  142. $keyValueStore2[$keyorig]->nodeValue = "";
  143. }
  144. //$keyValueStore["*imga"]->setAttribute("fill", "#FFFFFF");
  145. //replace text from URL GET within image
  146. foreach($_GET as $key => $value){
  147. if($key != "___image___"){
  148. $alltext = $xpath->query('//svg:text');
  149. foreach ($alltext as $text) {
  150. if($key == $text->nodeValue){
  151. $text->nodeValue = $value;
  152. }
  153. }
  154. }
  155. }
  156. //load image Replacements
  157. $replacements = new DOMDocument();
  158. $newstr = substr( $_GET["___image___"], 0, strpos( $_GET["___image___"], '/', strpos( $_GET["___image___"], '/')+1));
  159. $urlreplacement = dirname('https://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'], 2) . "/server/" . $newstr . "/replacement";
  160. $replacements->loadXML(file_get_contents($urlreplacement));
  161. $elements = $replacements->getElementsByTagName('item');
  162. foreach($elements as $node){
  163. if(array_key_exists($node->getElementsByTagName("abbreviation")[0]->nodeValue, $keyValueStore)){
  164. $extension = pathinfo($node->getElementsByTagName("url")[0]->nodeValue, PATHINFO_EXTENSION);
  165. if($extension == "mp4"){
  166. $element = $doc->createElement('foreignObject');
  167. //Notwendig da ab und zu mit transform exportiert wird liegt an der powerpoint version
  168. if($result3->getAttribute('transform') != null){
  169. $transform = get_string_between($keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('transform'), "matrix(", ")");
  170. $matrix = explode(" ", $transform);
  171. $videox =$matrix[4];
  172. $videoy =$matrix[5];
  173. $element->setAttribute("x", $videox);
  174. $element->setAttribute("y", $videoy);
  175. }else{
  176. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  177. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  178. }
  179. // echo $result->nodeValue;// + result3->getAttribute('x');
  180. // echo $result3->getAttribute('transform') . "<br>";
  181. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  182. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  183. $element1 = $doc->createElement('video');
  184. $element1->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  185. $element1->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  186. $element1->setAttribute("controls", "");
  187. $element2 = $doc->createElement('source');
  188. $element2->setAttribute("type", "video/mp4");
  189. $element2->setAttribute("src", $node->getElementsByTagName("url")[0]->nodeValue);
  190. $element1->appendChild($element2);
  191. $element->appendChild($element1);
  192. $parent = $doc->getElementsByTagName('svg')->item(0);
  193. $parent->appendChild($element);
  194. //override identifier to be empty
  195. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  196. }
  197. else{
  198. //create image element (currently we use background image)
  199. /*
  200. $element = $doc->createElement('image');
  201. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  202. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  203. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  204. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  205. $element->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  206. $doc->appendChild($element);
  207. $parent = $doc->getElementsByTagName('svg')->item(0);
  208. $parent->appendChild($element);
  209. */
  210. //create pattern for rect-filling looks like this:
  211. /*
  212. <defs>
  213. <pattern id="image" x="-32" y="-32" patternUnits="userSpaceOnUse" height="64" width="64">
  214. <image x="0" y="0" height="64" width="64" xlink:href="http://0.gravatar.com/avatar/902a4faaa4de6f6aebd6fd7a9fbab46a?s=64"/>
  215. </pattern>
  216. </defs>
  217. */
  218. $element3 = $doc->createElement('pattern');
  219. $element3->setAttribute("id", $node->getElementsByTagName("abbreviation")[0]->nodeValue);
  220. $element3->setAttribute("x", "0");
  221. $element3->setAttribute("y", "0");
  222. $element3->setAttribute("width", "1");
  223. $element3->setAttribute("height", "1");
  224. $element2 = $doc->createElement('image');
  225. $element2->setAttribute("x", "0");
  226. $element2->setAttribute("y", "0");
  227. $element2->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  228. $element2->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  229. $element2->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  230. $element3->appendChild($element2);
  231. $parent = $doc->getElementsByTagName('defs')->item(0);
  232. $parent->appendChild($element3);
  233. $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->setAttribute("fill", "url(#".$node->getElementsByTagName("abbreviation")[0]->nodeValue.")");
  234. //override identifier to be empty
  235. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  236. }
  237. }
  238. }
  239. echo $doc->saveXML();
  240. ?>
  241. </body>
  242. </html>