imageReplacement.php 12 KB

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