imageReplacement.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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"] . "&Semantic=true";
  65. $json = file_get_contents($url);
  66. $asocarray = json_decode($json, true);
  67. //echo "Test für Perfomance:<br>";
  68. $curstation = $_GET["Station"];
  69. $allAssignmentsPattern=array();
  70. $allAssignmentsStation=array();
  71. $allAssignmentsText=array();
  72. //Einzelbilder vom Universal Storage laden
  73. foreach ($keyValueStore as $key => $value) {
  74. $keyorig = $key;
  75. $key = substr($key, 1);
  76. $keyarray = str_split($key, 2);
  77. if (startsWith($key, 'URL')) {
  78. 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 . "%;'>";
  79. echo "</iframe>";
  80. }
  81. $fullfilename = "";
  82. $textentry = "";
  83. foreach($keyarray as $keypart){
  84. //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.
  85. if($keypart == "ID"){
  86. $fullfilename = $fullfilename . "_ID;" . substr($keyorig, strrpos($keyorig, "ID:") + 3);
  87. $textentry = substr($keyorig, strrpos($keyorig, "ID:") + 3);
  88. break;
  89. }
  90. else if(array_key_exists ($keypart, $asocarray)){
  91. $fullfilename = $fullfilename . "_" . $keypart . ";" . $asocarray[$keypart];
  92. }
  93. else{
  94. continue 2;
  95. }
  96. }
  97. $fullfilename = substr($fullfilename, 1);
  98. $fullfilename2 = $fullfilename;
  99. $fullfilename = $universalStorageEinzelbilder . "Station" . $curstation . "/Station". $curstation. "_" . $fullfilename . ".jpg";
  100. $url = realpath("./../../universal-storage/storage/Stationsbilder/Einzelbilder/" . "Station" . $curstation . "/Station". $curstation. "_" . $fullfilename2 . ".jpg");
  101. //if url does not exist check if symlink exists
  102. if(!$url) {
  103. $fullurl2 = "./../../universal-storage/storage/Stationsbilder/Einzelbilder/Symlinks/Station". $curstation. "_" . $fullfilename2 . ".jpg";
  104. $url = realpath($fullurl2);
  105. //echo "URL: " . $fullurl2;
  106. $fullfilename = $universalStorageEinzelbilder . "Symlinks/Station". $curstation. "_" . $fullfilename2 . ".jpg";
  107. }
  108. //echo "URL: " . $url . " filename" . $fullfilename . "<br>";
  109. //if Real image was not found search for Assignments
  110. //$file_headers = @get_headers($fullfilename);
  111. //if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
  112. if(!$url) {
  113. //echo $fullfilename;
  114. //echo " Does not Exist <br>";
  115. //echo $key . " ";
  116. //echo json_encode($asocarray);
  117. $onlyUsedParts=array();
  118. foreach($asocarray as $keypart => $keyval){
  119. if(in_array($keypart, $keyarray)){
  120. array_push($onlyUsedParts, $keyval);
  121. }
  122. else{
  123. array_push($onlyUsedParts, "");
  124. }
  125. }
  126. //echo implode (".", $onlyUsedParts);
  127. //echo "<br>";
  128. array_push($allAssignmentsPattern, implode (".", $onlyUsedParts));
  129. array_push($allAssignmentsStation, $curstation);
  130. array_push($allAssignmentsText, $textentry);
  131. $fullfilename = $wasUrl . "server/assignments/search?pattern=". implode (".", $onlyUsedParts) ."&station=". $curstation ."&Text=" . $textentry;
  132. $json = json_decode(file_get_contents($fullfilename));
  133. if (isset($json[0])) {
  134. $fullfilename = $json[0];
  135. }
  136. else{
  137. continue;
  138. }
  139. }
  140. else{
  141. //echo $fullfilename;
  142. }
  143. //echo $fullfilename . "<br>";
  144. $element3 = $doc->createElement('pattern');
  145. $element3->setAttribute("id", $fullfilename);
  146. $element3->setAttribute("x", "0");
  147. $element3->setAttribute("y", "0");
  148. $element3->setAttribute("width", "1");
  149. $element3->setAttribute("height", "1");
  150. $element2 = $doc->createElement('image');
  151. $element2->setAttribute("x", "0");
  152. $element2->setAttribute("y", "0");
  153. $element2->setAttribute("width", $value->getAttribute('width'));
  154. $element2->setAttribute("height", $value->getAttribute('height'));
  155. $element2->setAttribute("xlink:href", $fullfilename);
  156. $element3->appendChild($element2);
  157. $parent = $doc->getElementsByTagName('defs')->item(0);
  158. $parent->appendChild($element3);
  159. $value->setAttribute("fill", "url(#". $fullfilename . ")");
  160. //override identifier to be empty
  161. $keyValueStore2[$keyorig]->nodeValue = "";
  162. }
  163. //$keyValueStore["*imga"]->setAttribute("fill", "#FFFFFF");
  164. /*
  165. $url = $wasUrl . "server/assignments/searchMultiple";
  166. $data = array('key1' => json_encode($allAssignmentsPattern), 'key2' => json_encode($allAssignmentsStation), 'key3' => json_encode($allAssignmentsText));
  167. // use key 'http' even if you send the request to https://...
  168. $options = array(
  169. 'http' => array(
  170. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  171. 'method' => 'POST',
  172. 'content' => http_build_query($data)
  173. )
  174. );
  175. $context = stream_context_create($options);
  176. $result = file_get_contents($url, false, $context);
  177. if ($result === FALSE) {}
  178. else{
  179. //var_dump($result);
  180. }
  181. */
  182. //replace text from URL GET within image
  183. foreach($_GET as $key => $value){
  184. if($key != "___image___"){
  185. $alltext = $xpath->query('//svg:text');
  186. foreach ($alltext as $text) {
  187. if($key == $text->nodeValue){
  188. $text->nodeValue = $value;
  189. }
  190. }
  191. }
  192. }
  193. //load image Replacements
  194. $replacements = new DOMDocument();
  195. $newstr = substr( $_GET["___image___"], 0, strpos( $_GET["___image___"], '/', strpos( $_GET["___image___"], '/')+1));
  196. $urlreplacement = dirname('https://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'], 2) . "/server/" . $newstr . "/replacement";
  197. $replacements->loadXML(file_get_contents($urlreplacement));
  198. $elements = $replacements->getElementsByTagName('item');
  199. foreach($elements as $node){
  200. if(array_key_exists($node->getElementsByTagName("abbreviation")[0]->nodeValue, $keyValueStore)){
  201. $extension = pathinfo($node->getElementsByTagName("url")[0]->nodeValue, PATHINFO_EXTENSION);
  202. if($extension == "mp4"){
  203. $element = $doc->createElement('foreignObject');
  204. //Notwendig da ab und zu mit transform exportiert wird liegt an der powerpoint version
  205. if($result3->getAttribute('transform') != null){
  206. $transform = get_string_between($keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('transform'), "matrix(", ")");
  207. $matrix = explode(" ", $transform);
  208. $videox =$matrix[4];
  209. $videoy =$matrix[5];
  210. $element->setAttribute("x", $videox);
  211. $element->setAttribute("y", $videoy);
  212. }else{
  213. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  214. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  215. }
  216. // echo $result->nodeValue;// + result3->getAttribute('x');
  217. // echo $result3->getAttribute('transform') . "<br>";
  218. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  219. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  220. $element1 = $doc->createElement('video');
  221. $element1->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  222. $element1->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  223. $element1->setAttribute("controls", "");
  224. $element2 = $doc->createElement('source');
  225. $element2->setAttribute("type", "video/mp4");
  226. $element2->setAttribute("src", $node->getElementsByTagName("url")[0]->nodeValue);
  227. $element1->appendChild($element2);
  228. $element->appendChild($element1);
  229. $parent = $doc->getElementsByTagName('svg')->item(0);
  230. $parent->appendChild($element);
  231. //override identifier to be empty
  232. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  233. }
  234. else{
  235. //create image element (currently we use background image)
  236. /*
  237. $element = $doc->createElement('image');
  238. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  239. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  240. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  241. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  242. $element->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  243. $doc->appendChild($element);
  244. $parent = $doc->getElementsByTagName('svg')->item(0);
  245. $parent->appendChild($element);
  246. */
  247. //create pattern for rect-filling looks like this:
  248. /*
  249. <defs>
  250. <pattern id="image" x="-32" y="-32" patternUnits="userSpaceOnUse" height="64" width="64">
  251. <image x="0" y="0" height="64" width="64" xlink:href="http://0.gravatar.com/avatar/902a4faaa4de6f6aebd6fd7a9fbab46a?s=64"/>
  252. </pattern>
  253. </defs>
  254. */
  255. $element3 = $doc->createElement('pattern');
  256. $element3->setAttribute("id", $node->getElementsByTagName("abbreviation")[0]->nodeValue);
  257. $element3->setAttribute("x", "0");
  258. $element3->setAttribute("y", "0");
  259. $element3->setAttribute("width", "1");
  260. $element3->setAttribute("height", "1");
  261. $element2 = $doc->createElement('image');
  262. $element2->setAttribute("x", "0");
  263. $element2->setAttribute("y", "0");
  264. $element2->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  265. $element2->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  266. $element2->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  267. $element3->appendChild($element2);
  268. $parent = $doc->getElementsByTagName('defs')->item(0);
  269. $parent->appendChild($element3);
  270. $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->setAttribute("fill", "url(#".$node->getElementsByTagName("abbreviation")[0]->nodeValue.")");
  271. //override identifier to be empty
  272. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  273. }
  274. }
  275. }
  276. echo $doc->saveXML();
  277. ?>
  278. </body>
  279. </html>