imageReplacement.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. //get Rect
  50. foreach ($style2 as $result3) {
  51. //echo $result->nodeValue;
  52. //echo $result3->getAttribute('transform') . "<br>";
  53. $keyValueStore[$result->nodeValue] = $result3;
  54. $keyValueStore2[$result->nodeValue] = $result;
  55. }
  56. }
  57. //$keyValueStore["*imga"]->setAttribute("fill", "#FFFFFF");
  58. //replace text from URL GET within image
  59. foreach($_GET as $key => $value){
  60. if($key != "___image___"){
  61. $alltext = $xpath->query('//svg:text');
  62. foreach ($alltext as $text) {
  63. if($key == $text->nodeValue){
  64. $text->nodeValue = $value;
  65. }
  66. }
  67. }
  68. }
  69. //load image Replacements
  70. $replacements = new DOMDocument();
  71. $newstr = substr( $_GET["___image___"], 0, strpos( $_GET["___image___"], '/', strpos( $_GET["___image___"], '/')+1));
  72. $urlreplacement = dirname('https://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'], 2) . "/server/" . $newstr . "/replacement";
  73. $replacements->loadXML(file_get_contents($urlreplacement));
  74. $elements = $replacements->getElementsByTagName('item');
  75. foreach($elements as $node){
  76. if(array_key_exists($node->getElementsByTagName("abbreviation")[0]->nodeValue, $keyValueStore)){
  77. $extension = pathinfo($node->getElementsByTagName("url")[0]->nodeValue, PATHINFO_EXTENSION);
  78. if($extension == "mp4"){
  79. $element = $doc->createElement('foreignObject');
  80. //Notwendig da ab und zu mit transform exportiert wird liegt an der powerpoint version
  81. if($result3->getAttribute('transform') != null){
  82. $transform = get_string_between($keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('transform'), "matrix(", ")");
  83. $matrix = explode(" ", $transform);
  84. $videox =$matrix[4];
  85. $videoy =$matrix[5];
  86. $element->setAttribute("x", $videox);
  87. $element->setAttribute("y", $videoy);
  88. }else{
  89. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  90. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  91. }
  92. // echo $result->nodeValue;// + result3->getAttribute('x');
  93. // echo $result3->getAttribute('transform') . "<br>";
  94. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  95. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  96. $element1 = $doc->createElement('video');
  97. $element1->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  98. $element1->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  99. $element1->setAttribute("controls", "");
  100. $element2 = $doc->createElement('source');
  101. $element2->setAttribute("type", "video/mp4");
  102. $element2->setAttribute("src", $node->getElementsByTagName("url")[0]->nodeValue);
  103. $element1->appendChild($element2);
  104. $element->appendChild($element1);
  105. $parent = $doc->getElementsByTagName('svg')->item(0);
  106. $parent->appendChild($element);
  107. //override identifier to be empty
  108. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  109. }
  110. else{
  111. //create image element (currently we use background image)
  112. /*
  113. $element = $doc->createElement('image');
  114. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  115. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  116. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  117. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  118. $element->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  119. $doc->appendChild($element);
  120. $parent = $doc->getElementsByTagName('svg')->item(0);
  121. $parent->appendChild($element);
  122. */
  123. //create pattern for rect-filling looks like this:
  124. /*
  125. <defs>
  126. <pattern id="image" x="-32" y="-32" patternUnits="userSpaceOnUse" height="64" width="64">
  127. <image x="0" y="0" height="64" width="64" xlink:href="http://0.gravatar.com/avatar/902a4faaa4de6f6aebd6fd7a9fbab46a?s=64"/>
  128. </pattern>
  129. </defs>
  130. */
  131. $element3 = $doc->createElement('pattern');
  132. $element3->setAttribute("id", $node->getElementsByTagName("abbreviation")[0]->nodeValue);
  133. $element3->setAttribute("x", "0");
  134. $element3->setAttribute("y", "0");
  135. $element3->setAttribute("width", "1");
  136. $element3->setAttribute("height", "1");
  137. $element2 = $doc->createElement('image');
  138. $element2->setAttribute("x", "0");
  139. $element2->setAttribute("y", "0");
  140. $element2->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  141. $element2->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  142. $element2->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  143. $element3->appendChild($element2);
  144. $parent = $doc->getElementsByTagName('defs')->item(0);
  145. $parent->appendChild($element3);
  146. $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->setAttribute("fill", "url(#".$node->getElementsByTagName("abbreviation")[0]->nodeValue.")");
  147. //override identifier to be empty
  148. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  149. }
  150. }
  151. }
  152. echo $doc->saveXML();
  153. ?>
  154. </body>
  155. </html>