imageReplacement.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 $key . " ";
  90. echo json_encode($asocarray);
  91. $onlyUsedParts=array();
  92. foreach($asocarray as $keypart => $keyval){
  93. if(in_array($keypart, $keyarray)){
  94. array_push($onlyUsedParts, $keyval);
  95. }
  96. else{
  97. array_push($onlyUsedParts, "");
  98. }
  99. }
  100. echo implode (".", $onlyUsedParts);
  101. echo "<br>";
  102. $fullfilename = $wasUrl . "server/assignments/search?pattern=". implode (".", $onlyUsedParts) ."&station=". $curstation ."&Text=" . $textentry;
  103. $json = json_decode(file_get_contents($fullfilename));
  104. if (isset($json[0])) {
  105. $fullfilename = $json[0];
  106. }
  107. else{
  108. continue;
  109. }
  110. }
  111. //echo $fullfilename . "<br>";
  112. $element3 = $doc->createElement('pattern');
  113. $element3->setAttribute("id", $fullfilename);
  114. $element3->setAttribute("x", "0");
  115. $element3->setAttribute("y", "0");
  116. $element3->setAttribute("width", "1");
  117. $element3->setAttribute("height", "1");
  118. $element2 = $doc->createElement('image');
  119. $element2->setAttribute("x", "0");
  120. $element2->setAttribute("y", "0");
  121. $element2->setAttribute("width", $value->getAttribute('width'));
  122. $element2->setAttribute("height", $value->getAttribute('height'));
  123. $element2->setAttribute("xlink:href", $fullfilename);
  124. $element3->appendChild($element2);
  125. $parent = $doc->getElementsByTagName('defs')->item(0);
  126. $parent->appendChild($element3);
  127. $value->setAttribute("fill", "url(#". $fullfilename . ")");
  128. //override identifier to be empty
  129. $keyValueStore2[$keyorig]->nodeValue = "";
  130. }
  131. //$keyValueStore["*imga"]->setAttribute("fill", "#FFFFFF");
  132. //replace text from URL GET within image
  133. foreach($_GET as $key => $value){
  134. if($key != "___image___"){
  135. $alltext = $xpath->query('//svg:text');
  136. foreach ($alltext as $text) {
  137. if($key == $text->nodeValue){
  138. $text->nodeValue = $value;
  139. }
  140. }
  141. }
  142. }
  143. //load image Replacements
  144. $replacements = new DOMDocument();
  145. $newstr = substr( $_GET["___image___"], 0, strpos( $_GET["___image___"], '/', strpos( $_GET["___image___"], '/')+1));
  146. $urlreplacement = dirname('https://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'], 2) . "/server/" . $newstr . "/replacement";
  147. $replacements->loadXML(file_get_contents($urlreplacement));
  148. $elements = $replacements->getElementsByTagName('item');
  149. foreach($elements as $node){
  150. if(array_key_exists($node->getElementsByTagName("abbreviation")[0]->nodeValue, $keyValueStore)){
  151. $extension = pathinfo($node->getElementsByTagName("url")[0]->nodeValue, PATHINFO_EXTENSION);
  152. if($extension == "mp4"){
  153. $element = $doc->createElement('foreignObject');
  154. //Notwendig da ab und zu mit transform exportiert wird liegt an der powerpoint version
  155. if($result3->getAttribute('transform') != null){
  156. $transform = get_string_between($keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('transform'), "matrix(", ")");
  157. $matrix = explode(" ", $transform);
  158. $videox =$matrix[4];
  159. $videoy =$matrix[5];
  160. $element->setAttribute("x", $videox);
  161. $element->setAttribute("y", $videoy);
  162. }else{
  163. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  164. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  165. }
  166. // echo $result->nodeValue;// + result3->getAttribute('x');
  167. // echo $result3->getAttribute('transform') . "<br>";
  168. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  169. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  170. $element1 = $doc->createElement('video');
  171. $element1->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  172. $element1->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  173. $element1->setAttribute("controls", "");
  174. $element2 = $doc->createElement('source');
  175. $element2->setAttribute("type", "video/mp4");
  176. $element2->setAttribute("src", $node->getElementsByTagName("url")[0]->nodeValue);
  177. $element1->appendChild($element2);
  178. $element->appendChild($element1);
  179. $parent = $doc->getElementsByTagName('svg')->item(0);
  180. $parent->appendChild($element);
  181. //override identifier to be empty
  182. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  183. }
  184. else{
  185. //create image element (currently we use background image)
  186. /*
  187. $element = $doc->createElement('image');
  188. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  189. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  190. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  191. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  192. $element->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  193. $doc->appendChild($element);
  194. $parent = $doc->getElementsByTagName('svg')->item(0);
  195. $parent->appendChild($element);
  196. */
  197. //create pattern for rect-filling looks like this:
  198. /*
  199. <defs>
  200. <pattern id="image" x="-32" y="-32" patternUnits="userSpaceOnUse" height="64" width="64">
  201. <image x="0" y="0" height="64" width="64" xlink:href="http://0.gravatar.com/avatar/902a4faaa4de6f6aebd6fd7a9fbab46a?s=64"/>
  202. </pattern>
  203. </defs>
  204. */
  205. $element3 = $doc->createElement('pattern');
  206. $element3->setAttribute("id", $node->getElementsByTagName("abbreviation")[0]->nodeValue);
  207. $element3->setAttribute("x", "0");
  208. $element3->setAttribute("y", "0");
  209. $element3->setAttribute("width", "1");
  210. $element3->setAttribute("height", "1");
  211. $element2 = $doc->createElement('image');
  212. $element2->setAttribute("x", "0");
  213. $element2->setAttribute("y", "0");
  214. $element2->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  215. $element2->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  216. $element2->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  217. $element3->appendChild($element2);
  218. $parent = $doc->getElementsByTagName('defs')->item(0);
  219. $parent->appendChild($element3);
  220. $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->setAttribute("fill", "url(#".$node->getElementsByTagName("abbreviation")[0]->nodeValue.")");
  221. //override identifier to be empty
  222. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  223. }
  224. }
  225. }
  226. echo $doc->saveXML();
  227. ?>
  228. </body>
  229. </html>