imageReplacement.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://") . $_SERVER['HTTP_HOST']. dirname($_SERVER['PHP_SELF']) . "/TransformProductCode.php" . "?ProductCode=" . $_GET["ProductCode"];
  59. $json = file_get_contents($url);
  60. $asocarray = json_decode($json, true);
  61. //Einzelbilder vom Universal Storage laden
  62. foreach ($keyValueStore as $key => $value) {
  63. $keyorig = $key;
  64. $key = substr($key, 1);
  65. $keyarray = str_split($key, 2);
  66. $fullfilename = "";
  67. foreach($keyarray as $keypart){
  68. //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.
  69. if(array_key_exists ($keypart, $asocarray)){
  70. $fullfilename = $fullfilename . "_" . $keypart . ";" . $asocarray[$keypart];
  71. }
  72. else{
  73. continue 2;
  74. }
  75. }
  76. $fullfilename = substr($fullfilename, 1);
  77. $fullfilename = "https://centurio.work/customers/evva/universal-storage/storage/Einzelbilder/" . $fullfilename . ".jpg";
  78. //echo $fullfilename;
  79. $element3 = $doc->createElement('pattern');
  80. $element3->setAttribute("id", $fullfilename);
  81. $element3->setAttribute("x", "0");
  82. $element3->setAttribute("y", "0");
  83. $element3->setAttribute("width", "1");
  84. $element3->setAttribute("height", "1");
  85. $element2 = $doc->createElement('image');
  86. $element2->setAttribute("x", "0");
  87. $element2->setAttribute("y", "0");
  88. $element2->setAttribute("width", $value->getAttribute('width'));
  89. $element2->setAttribute("height", $value->getAttribute('height'));
  90. $element2->setAttribute("xlink:href", $fullfilename);
  91. $element3->appendChild($element2);
  92. $parent = $doc->getElementsByTagName('defs')->item(0);
  93. $parent->appendChild($element3);
  94. $value->setAttribute("fill", "url(#". $fullfilename . ")");
  95. //override identifier to be empty
  96. $keyValueStore2[$keyorig]->nodeValue = "";
  97. }
  98. //$keyValueStore["*imga"]->setAttribute("fill", "#FFFFFF");
  99. //replace text from URL GET within image
  100. foreach($_GET as $key => $value){
  101. if($key != "___image___"){
  102. $alltext = $xpath->query('//svg:text');
  103. foreach ($alltext as $text) {
  104. if($key == $text->nodeValue){
  105. $text->nodeValue = $value;
  106. }
  107. }
  108. }
  109. }
  110. //load image Replacements
  111. $replacements = new DOMDocument();
  112. $newstr = substr( $_GET["___image___"], 0, strpos( $_GET["___image___"], '/', strpos( $_GET["___image___"], '/')+1));
  113. $urlreplacement = dirname('https://' . $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'], 2) . "/server/" . $newstr . "/replacement";
  114. $replacements->loadXML(file_get_contents($urlreplacement));
  115. $elements = $replacements->getElementsByTagName('item');
  116. foreach($elements as $node){
  117. if(array_key_exists($node->getElementsByTagName("abbreviation")[0]->nodeValue, $keyValueStore)){
  118. $extension = pathinfo($node->getElementsByTagName("url")[0]->nodeValue, PATHINFO_EXTENSION);
  119. if($extension == "mp4"){
  120. $element = $doc->createElement('foreignObject');
  121. //Notwendig da ab und zu mit transform exportiert wird liegt an der powerpoint version
  122. if($result3->getAttribute('transform') != null){
  123. $transform = get_string_between($keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('transform'), "matrix(", ")");
  124. $matrix = explode(" ", $transform);
  125. $videox =$matrix[4];
  126. $videoy =$matrix[5];
  127. $element->setAttribute("x", $videox);
  128. $element->setAttribute("y", $videoy);
  129. }else{
  130. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  131. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  132. }
  133. // echo $result->nodeValue;// + result3->getAttribute('x');
  134. // echo $result3->getAttribute('transform') . "<br>";
  135. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  136. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  137. $element1 = $doc->createElement('video');
  138. $element1->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  139. $element1->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  140. $element1->setAttribute("controls", "");
  141. $element2 = $doc->createElement('source');
  142. $element2->setAttribute("type", "video/mp4");
  143. $element2->setAttribute("src", $node->getElementsByTagName("url")[0]->nodeValue);
  144. $element1->appendChild($element2);
  145. $element->appendChild($element1);
  146. $parent = $doc->getElementsByTagName('svg')->item(0);
  147. $parent->appendChild($element);
  148. //override identifier to be empty
  149. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  150. }
  151. else{
  152. //create image element (currently we use background image)
  153. /*
  154. $element = $doc->createElement('image');
  155. $element->setAttribute("x", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('x'));
  156. $element->setAttribute("y", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('y'));
  157. $element->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  158. $element->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  159. $element->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  160. $doc->appendChild($element);
  161. $parent = $doc->getElementsByTagName('svg')->item(0);
  162. $parent->appendChild($element);
  163. */
  164. //create pattern for rect-filling looks like this:
  165. /*
  166. <defs>
  167. <pattern id="image" x="-32" y="-32" patternUnits="userSpaceOnUse" height="64" width="64">
  168. <image x="0" y="0" height="64" width="64" xlink:href="http://0.gravatar.com/avatar/902a4faaa4de6f6aebd6fd7a9fbab46a?s=64"/>
  169. </pattern>
  170. </defs>
  171. */
  172. $element3 = $doc->createElement('pattern');
  173. $element3->setAttribute("id", $node->getElementsByTagName("abbreviation")[0]->nodeValue);
  174. $element3->setAttribute("x", "0");
  175. $element3->setAttribute("y", "0");
  176. $element3->setAttribute("width", "1");
  177. $element3->setAttribute("height", "1");
  178. $element2 = $doc->createElement('image');
  179. $element2->setAttribute("x", "0");
  180. $element2->setAttribute("y", "0");
  181. $element2->setAttribute("width", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('width'));
  182. $element2->setAttribute("height", $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->getAttribute('height'));
  183. $element2->setAttribute("xlink:href", $node->getElementsByTagName("url")[0]->nodeValue);
  184. $element3->appendChild($element2);
  185. $parent = $doc->getElementsByTagName('defs')->item(0);
  186. $parent->appendChild($element3);
  187. $keyValueStore[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->setAttribute("fill", "url(#".$node->getElementsByTagName("abbreviation")[0]->nodeValue.")");
  188. //override identifier to be empty
  189. $keyValueStore2[$node->getElementsByTagName("abbreviation")[0]->nodeValue]->nodeValue = "";
  190. }
  191. }
  192. }
  193. echo $doc->saveXML();
  194. ?>
  195. </body>
  196. </html>