imageReplacement.php 13 KB

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