0
<surface>  
    <graphic url="Z:\home\ashutosh\Desktop\imgdone\0015 Dalai       Lama\0001.jpeg" width="208px" height="242px"/>
    <zone xml:id="imtArea_0" rendition="_0015" ulx="47" uly="68" lrx="143" lry="172" rend="visible"/>

</surface>

My objective is to load the image given by the url attribute of graphic and get it annotated by the coordinates given as ulx, uly, lrx and lry. When I run the above the code below, I get graphic as a child node of surface but I am not able to get as to how to extract the image from the url which is the attribute of the graphic.

Therefore, firstly can someone tell me how to load the image from the given url using the DOM parsing and secondly when my image gets displayed, how can I draw a bounding box around the face of the image using the coordinates ulx, uly....lry.

Edited, I think this will help understanding the question

Please help !!

<?php

$xmlDoc = new DOMDocument();
$xmlDoc->load("0001.xml");

$elements = $xmlDoc->getElementsByTagName('surface');
foreach ($elements as $node) 
{
     if($node->childNodes->length)
     {
        foreach($node->childNodes as $child)
{   
        echo $child->nodeName,',';
        echo $child->nodeName,','->nodeValue;
}   

  }

 }
?>
  • `$node->childNodes-length`? What is this? Typo or do you really substract something from `childNodes`? – u_mulder Jan 30 '16 at 09:35
  • Typo error !! Ignore it please – Ashutosh Mishra Jan 30 '16 at 09:41
  • Can you give an example of the expected result? – The fourth bird Jan 30 '16 at 10:53
  • Possible duplicate of [How do I iterate through DOM elements in PHP?](http://stackoverflow.com/questions/191923/how-do-i-iterate-through-dom-elements-in-php) – michi Jan 30 '16 at 10:54
  • I want that from the url image should be loaded and then it should be annotated. By annotated, I mean that a square should be generated with the coordinates given by ulx uly lrx lry around the face of the image. – Ashutosh Mishra Jan 30 '16 at 13:09
  • your code doesn't reflect that. How do you plan to generate the square? What specific problem is your question about? – michi Jan 31 '16 at 15:57

1 Answers1

0

Fetching the data from the XML is simple with Xpath expressions.

$xml = <<<'XML'
<surface>  
    <graphic url="Z:\home\ashutosh\Desktop\imgdone\0015 Dalai       Lama\0001.jpeg" width="208px" height="242px"/>
    <zone xml:id="imtArea_0" rendition="_0015" ulx="47" uly="68" lrx="143" lry="172" rend="visible"/>
</surface>
XML;

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('//surface') as $surface) {
  var_dump(
    [
      'url' => $xpath->evaluate('string(graphic/@url)', $surface),
      'ulx' => $xpath->evaluate('number(zone/@ulx)', $surface),
      'uly' => $xpath->evaluate('number(zone/@uly)', $surface),
      'lrx' => $xpath->evaluate('number(zone/@lrx)', $surface),
      'lry' => $xpath->evaluate('number(zone/@lry)', $surface),
    ] 
  );
}

Output:

array(5) {
  ["url"]=>
  string(64) "Z:\home\ashutosh\Desktop\imgdone\0015 Dalai       Lama\0001.jpeg"
  ["ulx"]=>
  float(47)
  ["uly"]=>
  float(68)
  ["lrx"]=>
  float(143)
  ["lry"]=>
  float(172)
}

How you use the data depends on the expected result. You could use image functions to generate an image with the rectangle. Another possibility would be to generate an img tag with data attributes and generate the rectangle as an div with JavaScript (in the browser). That would allow for easy interaction.

ThW
  • 19,120
  • 3
  • 22
  • 44
  • Actually, the value of ulx, uly, lrx, lry are the coordinates around the face through which the bounding box will pass. I want to know that how to take these output values as input and then display a bounding box passing through the above output values. – Ashutosh Mishra Jan 31 '16 at 09:00