imageReplacement.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <html>
  2. <head>
  3. </head>
  4. <body style="margin: 0px">
  5. <?php
  6. ini_set('display_errors', 1);
  7. ini_set('display_startup_errors', 1);
  8. error_reporting(E_ALL);
  9. //http://www.justin-cook.com/2006/03/31/php-parse-a-string-between-two-strings/
  10. function get_string_between($string, $start, $end){
  11. $string = " ".$string;
  12. $ini = strpos($string,$start);
  13. if ($ini == 0) return "";
  14. $ini += strlen($start);
  15. $len = strpos($string,$end,$ini) - $ini;
  16. return substr($string,$ini,$len);
  17. }
  18. //header('Content-type: image/svg+xml'); //does not play video
  19. $svg_file = file_get_contents(dirname(__DIR__, 1) . "/images/uploads/" . $_GET["___image___"]);
  20. $doc1 = new DOMDocument;
  21. $doc1->loadXML($svg_file);
  22. $xpath1 = new DOMXPath($doc1);
  23. $xpath1->registerNamespace('svg', 'http://www.w3.org/2000/svg');
  24. //prüfen ob exteranlImage dann wird dieses verwendet
  25. if(strlen($svg_file) < 400){
  26. $url = $xpath1->query("/svg:svg/svg:image/@href");
  27. $svg_file = file_get_contents($url[0]->nodeValue);
  28. }
  29. $doc = new DOMDocument;
  30. $doc->loadXML($svg_file);
  31. $xpath = new DOMXPath($doc);
  32. $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');
  33. $root = $xpath->query("/*");
  34. $root[0]->setAttribute("viewBox", "0 0 1280 720");
  35. $widthq = $xpath->query("/*/@width");
  36. $heightq = $xpath->query("/*/@height");
  37. $root = $xpath->query("/*");
  38. $root[0]->setAttribute("viewBox", "0 0 ". $widthq[0]->nodeValue . " " . $heightq[0]->nodeValue);
  39. $widthq[0]->nodeValue = "100%";
  40. $heightq[0]->nodeValue = "100%";
  41. //Get all Text Nodes
  42. $style = $xpath->query('//svg:text');
  43. $keyValueStore=array();
  44. $keyValueStore2=array();
  45. //Get all rect nodes before text nodes and store them in key value store to access them for manipulation
  46. foreach ($style as $result) {
  47. $style2 = $xpath->query('./preceding-sibling::*[1]', $result);
  48. //echo $style2[0]->nodeName;
  49. //echo $result->nodeValue . " ";
  50. //get Rect
  51. foreach ($style2 as $result3) {
  52. //echo $result->nodeValue;
  53. //echo $result3->getAttribute('transform') . "<br>";
  54. $keyValueStore[$result->nodeValue] = $result3;
  55. $keyValueStore2[$result->nodeValue] = $result;
  56. }
  57. }
  58. //$keyValueStore["*imga"]->setAttribute("fill", "#FFFFFF");
  59. //replace text from URL GET within image
  60. foreach($_GET as $key => $value){
  61. if($key != "___image___"){
  62. $alltext = $xpath->query('//svg:text');
  63. foreach ($alltext as $text) {
  64. if($key == $text->nodeValue){
  65. $text->nodeValue = $value;
  66. }
  67. }
  68. }
  69. }
  70. //load image Replacements
  71. $replacements = new DOMDocument();
  72. $newstr = substr( $_GET["___image___"], 0, strpos( $_GET["___image___"], '/', strpos( $_GET["___image___"], '/')+1));
  73. $urlreplacement = dirname('https://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'], 2) . "/server/" . $newstr . "/replacement";
  74. $replacements->loadXML(file_get_contents($urlreplacement));
  75. $elements = $replacements->getElementsByTagName('item');
  76. foreach($elements as $node){
  77. if(array_key_exists($node->getElementsByTagName("abbreviation")[0]->nodeValue, $keyValueStore)){
  78. $extension = pathinfo($node->getElementsByTagName("url")[0]->nodeValue, PATHINFO_EXTENSION);
  79. if($extension == "mp4"){
  80. $element = $doc->createElement('foreignObject');
  81. //Notwendig da ab und zu mit transform exportiert wird liegt an der powerpoint version
  82. if($result3->getAttribute('transform') != null){
  83. $transform = get_string_between($keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('transform'), "matrix(", ")");
  84. $matrix = explode(" ", $transform);
  85. $videox =$matrix[4];
  86. $videoy =$matrix[5];
  87. $element->setAttribute("x", $videox);
  88. $element->setAttribute("y", $videoy);
  89. }else{
  90. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  91. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  92. }
  93. // echo $result->nodeValue;// + result3->getAttribute('x');
  94. // echo $result3->getAttribute('transform') . "<br>";
  95. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  96. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  97. $element1 = $doc->createElement('video');
  98. $element1->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  99. $element1->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  100. $element1->setAttribute("controls", "");
  101. $element2 = $doc->createElement('source');
  102. $element2->setAttribute("type", "video/mp4");
  103. $element2->setAttribute("src", $node->getElementsByTagName("url")[0]->nodeValue);
  104. $element1->appendChild($element2);
  105. $element->appendChild($element1);
  106. $parent = $doc->getElementsByTagName('svg')->item(0);
  107. $parent->appendChild($element);
  108. //override identifier to be empty
  109. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  110. }
  111. else{
  112. //create image element (currently we use background image)
  113. /*
  114. $element = $doc->createElement('image');
  115. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  116. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  117. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  118. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  119. $element->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  120. $doc->appendChild($element);
  121. $parent = $doc->getElementsByTagName('svg')->item(0);
  122. $parent->appendChild($element);
  123. */
  124. //create pattern for rect-filling looks like this:
  125. /*
  126. <defs>
  127. <pattern id="image" x="-32" y="-32" patternUnits="userSpaceOnUse" height="64" width="64">
  128. <image x="0" y="0" height="64" width="64" xlink:href="http://0.gravatar.com/avatar/902a4faaa4de6f6aebd6fd7a9fbab46a?s=64"/>
  129. </pattern>
  130. </defs>
  131. */
  132. $element3 = $doc->createElement('pattern');
  133. $element3->setAttribute("id", $node->getElementsByTagName("abbreviation")[0]->nodeValue);
  134. $element3->setAttribute("x", "0");
  135. $element3->setAttribute("y", "0");
  136. $element3->setAttribute("width", "1");
  137. $element3->setAttribute("height", "1");
  138. $element2 = $doc->createElement('image');
  139. $element2->setAttribute("x", "0");
  140. $element2->setAttribute("y", "0");
  141. $element2->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  142. $element2->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  143. $element2->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  144. $element3->appendChild($element2);
  145. $parent = $doc->getElementsByTagName('defs')->item(0);
  146. $parent->appendChild($element3);
  147. $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->setAttribute("fill", "url(#".$node->getElementsByTagName("abbreviation")[0]->nodeValue.")");
  148. //override identifier to be empty
  149. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  150. }
  151. }
  152. }
  153. echo $doc->saveXML();
  154. ?>
  155. </body>
  156. </html>