imageReplacement.php 6.4 KB

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