imageReplacement.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. //header('Content-type: image/svg+xml'); //does not play video
  10. $svg_file = file_get_contents(dirname(__DIR__, 1) . "/images/uploads/" . $_GET["___image___"]);
  11. $doc1 = new DOMDocument;
  12. $doc1->loadXML($svg_file);
  13. $xpath1 = new DOMXPath($doc1);
  14. $xpath1->registerNamespace('svg', 'http://www.w3.org/2000/svg');
  15. //prüfen ob exteranlImage dann wird dieses verwendet
  16. if(strlen($svg_file) < 400){
  17. $url = $xpath1->query("/svg:svg/svg:image/@href");
  18. $svg_file = file_get_contents($url[0]->nodeValue);
  19. }
  20. $doc = new DOMDocument;
  21. $doc->loadXML($svg_file);
  22. $xpath = new DOMXPath($doc);
  23. $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');
  24. $root = $xpath->query("/*");
  25. $root[0]->setAttribute("viewBox", "0 0 1280 720");
  26. $widthq = $xpath->query("/*/@width");
  27. $heightq = $xpath->query("/*/@height");
  28. $root = $xpath->query("/*");
  29. $root[0]->setAttribute("viewBox", "0 0 ". $widthq[0]->nodeValue . " " . $heightq[0]->nodeValue);
  30. $widthq[0]->nodeValue = "100%";
  31. $heightq[0]->nodeValue = "100%";
  32. //Get all Text Nodes
  33. $style = $xpath->query('//svg:text');
  34. $keyValueStore=array();
  35. //Get all rect nodes before text nodes and store them in key value store to access them for manipulation
  36. foreach ($style as $result) {
  37. $style2 = $xpath->query('./preceding-sibling::*[1]', $result);
  38. //echo $style2[0]->nodeName;
  39. foreach ($style2 as $result3) {
  40. //echo $result3->nodeName;
  41. $keyValueStore[$result->nodeValue] = $result3;
  42. }
  43. }
  44. //$keyValueStore["*imga"]->setAttribute("fill", "#FFFFFF");
  45. //replace text from URL GET within image
  46. foreach($_GET as $key => $value){
  47. if($key != "___image___"){
  48. $alltext = $xpath->query('//svg:text');
  49. foreach ($alltext as $text) {
  50. if($key == $text->nodeValue){
  51. $text->nodeValue = $value;
  52. }
  53. }
  54. }
  55. }
  56. //load image Replacements
  57. $replacements = new DOMDocument();
  58. $newstr = substr( $_GET["___image___"], 0, strpos( $_GET["___image___"], '/', strpos( $_GET["___image___"], '/')+1));
  59. $urlreplacement = dirname('https://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'], 2) . "/server/" . $newstr . "/replacement";
  60. $replacements->loadXML(file_get_contents($urlreplacement));
  61. $elements = $replacements->getElementsByTagName('item');
  62. foreach($elements as $node){
  63. if(array_key_exists($node->getElementsByTagName("abbreviation")[0]->nodeValue, $keyValueStore)){
  64. $extension = pathinfo($node->getElementsByTagName("url")[0]->nodeValue, PATHINFO_EXTENSION);
  65. if($extension == "mp4"){
  66. $element = $doc->createElement('foreignObject');
  67. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  68. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  69. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  70. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  71. $element1 = $doc->createElement('video');
  72. $element1->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  73. $element1->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  74. $element1->setAttribute("controls", "");
  75. $element2 = $doc->createElement('source');
  76. $element2->setAttribute("type", "video/mp4");
  77. $element2->setAttribute("src", $node->getElementsByTagName("url")[0]->nodeValue);
  78. $element1->appendChild($element2);
  79. $element->appendChild($element1);
  80. $parent = $doc->getElementsByTagName('svg')->item(0);
  81. $parent->appendChild($element);
  82. }
  83. else{
  84. //create image element (currently we use background image)
  85. /*
  86. $element = $doc->createElement('image');
  87. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  88. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  89. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  90. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  91. $element->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  92. $doc->appendChild($element);
  93. $parent = $doc->getElementsByTagName('svg')->item(0);
  94. $parent->appendChild($element);
  95. */
  96. //create pattern for rect-filling looks like this:
  97. /*
  98. <defs>
  99. <pattern id="image" x="-32" y="-32" patternUnits="userSpaceOnUse" height="64" width="64">
  100. <image x="0" y="0" height="64" width="64" xlink:href="http://0.gravatar.com/avatar/902a4faaa4de6f6aebd6fd7a9fbab46a?s=64"/>
  101. </pattern>
  102. </defs>
  103. */
  104. $element3 = $doc->createElement('pattern');
  105. $element3->setAttribute("id", $node->getElementsByTagName("abbreviation")[0]->nodeValue);
  106. $element3->setAttribute("x", "0");
  107. $element3->setAttribute("y", "0");
  108. $element3->setAttribute("width", "1");
  109. $element3->setAttribute("height", "1");
  110. $element2 = $doc->createElement('image');
  111. $element2->setAttribute("x", "0");
  112. $element2->setAttribute("y", "0");
  113. $element2->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  114. $element2->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  115. $element2->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  116. $element3->appendChild($element2);
  117. $parent = $doc->getElementsByTagName('defs')->item(0);
  118. $parent->appendChild($element3);
  119. $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->setAttribute("fill", "url(#".$node->getElementsByTagName("abbreviation")[0]->nodeValue.")");
  120. }
  121. }
  122. }
  123. echo $doc->saveXML();
  124. ?>
  125. </body>
  126. </html>