imageReplacement.php 11 KB

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